Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新支持两个控制函数 #3

Merged
merged 1 commit into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,27 @@ type test struct {
Accept string `header:"Accept"`
ContentType string `header:"Content-Type"`
}
```

### 2.3 option.WithOutputFmtBefore 支持(json/yaml/http header/query string)
获取格式代码之前的buf输出,方便debug
```go

obj := `{"a":"b"}`
var out bytes.Buffer
all, err := json.Marshal([]byte(obj), option.WithStructName("reqName"), option.WithTagName("json"), option.WithOutputFmtBefore(&out))
_ = all
_ = err
```

```
### 2.4 option.WithGetValue 支持(json/yaml)
把json串序列化成struct字符串的时候,顺带提取json字符串里面指定的key值
```go
obj := `{"a":"b"}`
getValue := map[string]string{
".a": "",
}

_, err := json.Marshal([]byte(obj), option.WithStructName("reqName"), option.WithTagName("json"), option.WithGetValue(getValue))
fmt.Println(getValue[".a"], "b"))
```
4 changes: 4 additions & 0 deletions internal/map2struct/map2struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func MapGenStruct(m map[string][]string, opt option.Option) (res []byte, err err

fmt.Fprint(&out, "}")

if opt.OutputFmtBefore != nil {
opt.OutputFmtBefore.Write(out.Bytes())
}

src, err := format.Source(out.Bytes())
if err != nil {
return nil, err
Expand Down
13 changes: 13 additions & 0 deletions json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ func (f *JSON) marshal() (b []byte, err error) {
}
}

if f.OutputFmtBefore != nil {
f.OutputFmtBefore.Write(f.buf.Bytes())
}

if b, err = format.Source(f.buf.Bytes()); err != nil {
fmt.Printf("%s\n", f.buf.String())
return nil, err
Expand Down Expand Up @@ -266,6 +270,7 @@ func (f *JSON) marshalValue(key string, obj interface{}, fromArray bool, depth i

fieldName, tagName := name.GetFieldAndTagName(key)

// 重写key的类型
if f.TypeMap != nil {
fieldType, ok := f.TypeMap[keyPath]
if ok {
Expand All @@ -274,6 +279,14 @@ func (f *JSON) marshalValue(key string, obj interface{}, fromArray bool, depth i
}
}

if f.GetValue != nil {
_, ok := f.GetValue[keyPath]
if ok {
f.GetValue[keyPath] = fmt.Sprintf("%s", obj)
}

}

tmpFieldName := strings.ToUpper(fieldName)
if tab.InitialismsTab[tmpFieldName] {
fieldName = tmpFieldName
Expand Down
19 changes: 19 additions & 0 deletions json/json_get_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package json

import (
"testing"

"github.com/antlabs/tostruct/option"
"github.com/stretchr/testify/assert"
)

func Test_GetVal(t *testing.T) {
obj := `{"a":"b"}`
getValue := map[string]string{
".a": "",
}

_, err := Marshal([]byte(obj), option.WithStructName("reqName"), option.WithTagName("json"), option.WithGetValue(getValue), option.WithOutputFmtBefore(nil))
assert.NoError(t, err)
assert.Equal(t, getValue[".a"], "b")
}
19 changes: 19 additions & 0 deletions json/json_outputFmtBefore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package json

import (
"bytes"
"testing"

"github.com/antlabs/tostruct/option"
"github.com/stretchr/testify/assert"
)

func Test_OutputFmtBefore(t *testing.T) {

obj := `{"a":"b"}`
var out bytes.Buffer
all, err := Marshal([]byte(obj), option.WithStructName("reqName"), option.WithTagName("json"), option.WithOutputFmtBefore(&out))
assert.NoError(t, err)
assert.NotEqual(t, out.Len(), 0)
assert.NotEqual(t, 0, len(all))
}
36 changes: 31 additions & 5 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
// apache 2.0
package option

import (
"io"
"os"
)

type OptionFunc func(c *Option)

type Option struct {
Inline bool
Tag string
StructName string
TypeMap map[string]string
TagNameFromKey bool
Inline bool
Tag string
StructName string
TypeMap map[string]string
GetValue map[string]string
TagNameFromKey bool
OutputFmtBefore io.Writer //需要format之前的数据
}

// 控制生成的结构体是否内联
Expand Down Expand Up @@ -64,10 +71,29 @@ func WithSpecifyType(typeMap map[string]string) OptionFunc {
}
}

// 目前只支持json/yaml
func WithGetValue(getValue map[string]string) OptionFunc {
return func(c *Option) {
c.GetValue = getValue
}
}

// tag使用变量名, http header特殊一点
// 目前仅仅支持http header marshal
func WithTagNameFromKey() OptionFunc {
return func(c *Option) {
c.TagNameFromKey = true
}
}

// WithOutputFmtBefore(nil) 日志数据打印到控制台
// WithOutputFmtBefore(w) 日志数据打印到w里
func WithOutputFmtBefore(w io.Writer) OptionFunc {
return func(c *Option) {
if w == nil {
c.OutputFmtBefore = os.Stdout
} else {
c.OutputFmtBefore = w
}
}
}