Skip to content

Commit

Permalink
添加一些注释
Browse files Browse the repository at this point in the history
  • Loading branch information
JessonChan committed Dec 14, 2022
1 parent ea6e724 commit 345424d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion jsun.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ func Marshal(v interface{}, styles ...JsonNameStyle) ([]byte, error) {
})
}
}
// 此时可以直接使用原来的包
if style == UpperCamelStyle {
return json.Marshal(v)
}
// 此时需要动态的生成新的结构
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Ptr {
rv = reflect.Indirect(rv)
Expand All @@ -84,7 +86,7 @@ func Marshal(v interface{}, styles ...JsonNameStyle) ([]byte, error) {
}
key := fmt.Sprintf("%s%d", rv.Type(), style)
typ, find := typeCache.Load(key)
if find == false {
if !find {
typ = buildType(rv.Type(), style)
typeCache.Store(key, typ)
}
Expand Down
12 changes: 12 additions & 0 deletions jsun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ type renamedByte byte
type renamedByteSlice []byte
type renamedRenamedByteSlice []renamedByte

func TestMap(t *testing.T) {
m := map[string]interface{}{
"data": struct {
AgeValue int `json:"age_value"`
}{
AgeValue: 100,
},
}
bs, _ := Marshal(m, LowerCamelStyle)
fmt.Println(string(bs))
}

func TestEncodeRenamedByteSlice(t *testing.T) {
s := renamedByteSlice("abc")
result, err := json.Marshal(s)
Expand Down

0 comments on commit 345424d

Please sign in to comment.