更新文档 (#7)

This commit is contained in:
guonaihong
2023-03-13 23:40:42 +08:00
committed by GitHub
parent f7486f581c
commit a77c5a61a6
3 changed files with 29 additions and 3 deletions

View File

@@ -321,3 +321,27 @@ getValue := map[string]any{
_, err := json.Marshal([]byte(obj), option.WithStructName("reqName"), option.WithTagName("json"), option.WithGetRawValue(getValue))
fmt.Println(getValue[".a"], "b"))
```
### 2.6 option.WithUsePtrType 支持(json/yaml)
```go
obj := `
{
"string": "",
"int": 0,
"float64": 3.1415,
"bool": false
}
`
ptrField := []string{".string", ".int", ".float64", ".bool"} //指定字段名使用指针类型
all, err := json.Marshal([]byte(obj), option.WithStructName("reqName"), option.WithGetRawValue(ptrField))
fmt.Printf("%s\n", all)
// 输出:
/*
type reqName struct {
Bool *bool `json:"bool"`
Float64 *float64 `json:"float64"`
Int *int `json:"int"`
String *string `json:"string"`
}
*/
```

View File

@@ -64,11 +64,11 @@ func Test_Gen_Obj_JSONEx(t *testing.T) {
assert.Equal(t, need1, got1)
assert.Equal(t, need2, got2)
gotPtr1, err := Marshal(data, option.WithStructName("reqName"), option.WithOutputFmtBefore(&out), option.WithUsePtrField(keys))
gotPtr1, err := Marshal(data, option.WithStructName("reqName"), option.WithOutputFmtBefore(&out), option.WithUsePtrType(keys))
assert.NoError(t, err, out.String())
out.Reset()
gotPtr2, err := Marshal(data, option.WithStructName("reqName"), option.WithNotInline(), option.WithOutputFmtBefore(&out), option.WithUsePtrField(keys))
gotPtr2, err := Marshal(data, option.WithStructName("reqName"), option.WithNotInline(), option.WithOutputFmtBefore(&out), option.WithUsePtrType(keys))
assert.NoError(t, err, out.String())
needPtr1 = bytes.TrimSpace(needPtr1)

View File

@@ -115,8 +115,10 @@ func WithProtobuf() OptionFunc {
}
}
// 传入的字段使用指针类型
// 入参[]string{".字段名1", ".字段名2"}
// json/yaml有效
func WithUsePtrField(fieldList []string) OptionFunc {
func WithUsePtrType(fieldList []string) OptionFunc {
return func(c *Option) {
if c.UsePtr == nil {
c.UsePtr = make(map[string]bool)