Skip to content

Commit 6b73804

Browse files
authored
Merge pull request #2 from matsuri-tech/feat/issue-1
feat: hidden tag
2 parents 39fa57b + 5b606bc commit 6b73804

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
11
# json-hidden-marshal
2+
3+
## Example
4+
5+
```go
6+
type User struct {
7+
Name string `json:"name"`
8+
Password string `json:"password" hidden:"mask"` // -> masked like "*****", preserving the string length
9+
Hidden int `json:"hidden" hidden:"-"`
10+
Hidden2 int `json:"hidden" hidden:"true"` // hidden:"-" or hidden:"true" to skip
11+
}
12+
```

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/matsuri-tech/json-hidden-marshal
22

33
go 1.15
4+
5+
require github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

marshal.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package json_hidden_marshal
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"reflect"
8+
"strings"
9+
)
10+
11+
func scanToMap(val interface{}) (map[string]interface{}, error) {
12+
strct := reflect.TypeOf(val)
13+
values := reflect.ValueOf(val)
14+
15+
// pointerであればderefする
16+
// 一度しかderefしないのよくわからなくない?という気持ちもあるけど…
17+
if values.Kind() == reflect.Ptr {
18+
strct = strct.Elem()
19+
values = values.Elem()
20+
}
21+
22+
m := map[string]interface{}{}
23+
for i := 0; i < strct.NumField(); i++ {
24+
field := strct.Field(i)
25+
hidden := field.Tag.Get("hidden")
26+
27+
// Marshal時にmap[string]interface{}に置き換えるため、失われるjsonタグの対応をする必要がある
28+
// 現状は名前の変更のみ対応
29+
jsonTags := strings.Split(field.Tag.Get("json"), ",")
30+
fieldName := field.Name
31+
if len(jsonTags) >= 1 {
32+
if jsonTags[0] != "" {
33+
fieldName = jsonTags[0]
34+
}
35+
}
36+
37+
if hidden == "" {
38+
ival := values.Field(i)
39+
if ival.Kind() == reflect.Struct {
40+
// PERF: 再帰をやめる
41+
v, err := scanToMap(ival.Interface())
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
m[fieldName] = v
47+
} else {
48+
// 構造体でないときはbase typeなのでそのまま
49+
m[fieldName] = ival.Interface()
50+
}
51+
} else if hidden == "-" || hidden == "true" {
52+
continue
53+
} else if hidden == "mask" {
54+
m[fieldName] = strings.Repeat("*", len(values.Field(i).String()))
55+
} else {
56+
return nil, errors.New(fmt.Sprintf("unsupported hidden tag: %v", hidden))
57+
}
58+
}
59+
60+
return m, nil
61+
}
62+
63+
func Marshal(val interface{}) ([]byte, error) {
64+
m, err := scanToMap(val)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
return json.Marshal(m)
70+
}

marshal_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package json_hidden_marshal
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestMarshal(t *testing.T) {
9+
cases := []struct {
10+
in interface{}
11+
expected string
12+
}{
13+
{
14+
// hidden:- or hidden:true to skip, hidden:mask to mask
15+
in: struct {
16+
Name string `json:"name"`
17+
Name2 string `json:"name2" hidden:"-"` // skip
18+
Name3 string `json:"name3" hidden:"true"` // skip
19+
Password string `json:"password" hidden:"mask"` // masked
20+
}{
21+
Name: "foo",
22+
Password: "password",
23+
},
24+
expected: `{"name":"foo","password":"********"}`,
25+
},
26+
{
27+
// nested type
28+
in: struct {
29+
Nested struct {
30+
Open string `json:"open"`
31+
Hidden string `json:"hidden" hidden:"-"`
32+
} `json:"nested"`
33+
}{
34+
Nested: struct {
35+
Open string `json:"open"`
36+
Hidden string `json:"hidden" hidden:"-"`
37+
}{
38+
Open: "open",
39+
Hidden: "hidden",
40+
},
41+
},
42+
expected: `{"nested":{"open":"open"}}`,
43+
},
44+
{
45+
// Without json tags
46+
in: struct {
47+
Name string
48+
Password string `hidden:"-"`
49+
}{
50+
Name: "foo",
51+
Password: "password",
52+
},
53+
expected: `{"Name":"foo"}`,
54+
},
55+
{
56+
// Pass pointer
57+
in: &struct {
58+
Name string `json:"name"`
59+
Password string `json:"password" hidden:"mask"`
60+
}{
61+
Name: "foo",
62+
Password: "password",
63+
},
64+
expected: `{"name":"foo","password":"********"}`,
65+
},
66+
}
67+
68+
for _, c := range cases {
69+
out, err := Marshal(c.in)
70+
if err != nil {
71+
t.Errorf("%+v\n", err)
72+
}
73+
74+
assert.JSONEq(t, string(out), c.expected)
75+
}
76+
}

0 commit comments

Comments
 (0)