Skip to content

Commit 8702425

Browse files
committed
fix: support string tag opt
fixes #36
1 parent 22ec9d5 commit 8702425

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

sheriff.go

+15
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {
106106
continue
107107
}
108108

109+
quoted := false
110+
if jsonOpts.Contains("string") {
111+
switch val.Kind() {
112+
case reflect.Bool,
113+
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
114+
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
115+
reflect.Float32, reflect.Float64,
116+
reflect.String:
117+
quoted = true
118+
}
119+
}
120+
109121
// if there is an anonymous field which is a struct
110122
// we want the childs exposed at the toplevel to be
111123
// consistent with the embedded json marshaller
@@ -179,6 +191,9 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {
179191
if err != nil {
180192
return nil, err
181193
}
194+
if quoted {
195+
v = fmt.Sprintf("%v", v)
196+
}
182197

183198
// when a composition field we want to bring the child
184199
// nodes to the top

sheriff_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -754,3 +754,26 @@ func TestMarshal_NilPointer(t *testing.T) {
754754
assert.Nil(t, v)
755755
assert.NoError(t, err)
756756
}
757+
758+
func TestMarshal_User(t *testing.T) {
759+
type JsonStringTag struct {
760+
Test int64 `json:"test,string"`
761+
TestB bool `json:"testb,string"`
762+
TestF float64 `json:"testf,string"`
763+
TestS string `json:"tests,string"`
764+
}
765+
j := JsonStringTag{
766+
Test: 12,
767+
TestB: true,
768+
TestF: 12.0,
769+
TestS: "test",
770+
}
771+
772+
v, err := Marshal(&Options{}, j)
773+
assert.NoError(t, err)
774+
assert.Equal(t, map[string]interface{}{"test": "12", "testb": "true", "testf": "12", "tests": "test"}, v)
775+
776+
d, err := json.Marshal(j)
777+
assert.NoError(t, err)
778+
assert.Equal(t, `{"test":"12","testb":"true","testf":"12","tests":"\"test\""}`, string(d))
779+
}

tags.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ type tagOptions string
1313
// parseTag splits a struct field's json tag into its name and
1414
// comma-separated options.
1515
func parseTag(tag string) (string, tagOptions) {
16-
if idx := strings.Index(tag, ","); idx != -1 {
17-
return tag[:idx], tagOptions(tag[idx+1:])
18-
}
19-
return tag, tagOptions("")
16+
tag, opt, _ := strings.Cut(tag, ",")
17+
return tag, tagOptions(opt)
2018
}
2119

2220
// Contains reports whether a comma-separated list of options
@@ -28,15 +26,11 @@ func (o tagOptions) Contains(optionName string) bool {
2826
}
2927
s := string(o)
3028
for s != "" {
31-
var next string
32-
i := strings.Index(s, ",")
33-
if i >= 0 {
34-
s, next = s[:i], s[i+1:]
35-
}
36-
if s == optionName {
29+
var name string
30+
name, s, _ = strings.Cut(s, ",")
31+
if name == optionName {
3732
return true
3833
}
39-
s = next
4034
}
4135
return false
4236
}

0 commit comments

Comments
 (0)