Skip to content

Commit dbf54fc

Browse files
aerthaerth
and
aerth
authored
skip func fields in marshalling Options (json,yaml,toml) (#97)
Co-authored-by: aerth <aerth@localhost>
1 parent 9bedcaa commit dbf54fc

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

secure.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ type Options struct {
9898
// AllowedHostsAreRegex determines, if the provided `AllowedHosts` slice contains valid regular expressions. If this flag is set to true, every request's host will be checked against these expressions. Default is false.
9999
AllowedHostsAreRegex bool
100100
// AllowRequestFunc is a custom function that allows you to determine if the request should proceed or not based on your own custom logic. Default is nil.
101-
AllowRequestFunc AllowRequestFunc
101+
AllowRequestFunc AllowRequestFunc `json:"-" yaml:"-" toml:"-"`
102102
// HostsProxyHeaders is a set of header keys that may hold a proxied hostname value for the request.
103103
HostsProxyHeaders []string
104104
// SSLHostFunc is a function pointer, the return value of the function is the host name that has same functionality as `SSHost`. Default is nil.
105105
// If SSLHostFunc is nil, the `SSLHost` option will be used.
106-
SSLHostFunc *SSLHostFunc
106+
SSLHostFunc *SSLHostFunc `json:"-" yaml:"-" toml:"-"`
107107
// SSLProxyHeaders is set of header keys with associated values that would indicate a valid https request. Useful when using Nginx: `map[string]string{"X-Forwarded-Proto": "https"}`. Default is blank map.
108108
SSLProxyHeaders map[string]string
109109
// STSSeconds is the max-age of the Strict-Transport-Security header. Default is 0, which would NOT include the header.

secure_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package secure
33
import (
44
"context"
55
"crypto/tls"
6+
"encoding/json"
67
"net/http"
78
"net/http/httptest"
89
"reflect"
@@ -1374,6 +1375,41 @@ func TestBadRequestHandler(t *testing.T) {
13741375
expect(t, strings.TrimSpace(res.Body.String()), `custom error`)
13751376
}
13761377

1378+
func TestMarshal(t *testing.T) {
1379+
// func cant be marshalled
1380+
var t1 = struct {
1381+
A string
1382+
F func()
1383+
}{}
1384+
_, err := json.Marshal(t1) //lint:ignore SA1026 ignore marshal error
1385+
if err == nil {
1386+
t.Error("expected error got none")
1387+
} else if !strings.Contains(err.Error(), "unsupported type: func()") {
1388+
t.Error("unexpected error:", err)
1389+
}
1390+
1391+
// struct field tags omits func
1392+
var t2 = struct {
1393+
A string
1394+
F func() `json:"-"`
1395+
}{}
1396+
_, err = json.Marshal(t2)
1397+
if err != nil {
1398+
t.Error("unexpected error:", err)
1399+
}
1400+
1401+
// Options has struct field tags to omit func fields
1402+
var o1 Options
1403+
b, err := json.Marshal(o1)
1404+
if err != nil {
1405+
t.Errorf("unexpected error marshal: %v", err)
1406+
}
1407+
err = json.Unmarshal(b, &o1)
1408+
if err != nil {
1409+
t.Errorf("unexpected error unmarshal: %v", err)
1410+
}
1411+
}
1412+
13771413
// Test Helper.
13781414
func expect(t *testing.T, a interface{}, b interface{}) {
13791415
t.Helper()

0 commit comments

Comments
 (0)