mirror of
https://github.com/antlabs/tostruct.git
synced 2025-12-24 12:58:04 +08:00
更新文档 (#7)
This commit is contained in:
24
README.md
24
README.md
@@ -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"`
|
||||
}
|
||||
*/
|
||||
```
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user