Skip to content

WithGetRawValue #4

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

Merged
merged 1 commit into from
Jan 30, 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,15 @@ getValue := map[string]string{
_, err := json.Marshal([]byte(obj), option.WithStructName("reqName"), option.WithTagName("json"), option.WithGetValue(getValue))
fmt.Println(getValue[".a"], "b"))
```

### 2.5 option.WithGetRawValue 支持(json/yaml/header/query string)
把json串序列化成struct字符串的时候,顺带提取json字符串里面指定的key值, value是any(int/string/float64)类型
```go
obj := `{"a":"b"}`
getValue := map[string]any{
".a": "",
}

_, err := json.Marshal([]byte(obj), option.WithStructName("reqName"), option.WithTagName("json"), option.WithGetRawValue(getValue))
fmt.Println(getValue[".a"], "b"))
```
19 changes: 19 additions & 0 deletions internal/guesstype/guesstype.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,22 @@ func TypeOf(s string) string {

return "string"
}

func ToAny(s string) any {
if IsFloat(s) {
f, _ := strconv.ParseFloat(s, 0)
return f
}

if IsInt(s) {
i, _ := strconv.Atoi(s)
return i
}

if IsBool(s) {
b, _ := strconv.ParseBool(s)
return b
}

return s
}
8 changes: 8 additions & 0 deletions internal/map2struct/map2struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ func MapGenStruct(m map[string][]string, opt option.Option) (res []byte, err err

for _, k := range ks {
v := m[k]
if opt.GetRawValue != nil {
_, ok := opt.GetRawValue[k]
if ok {
opt.GetRawValue[k] = guesstype.ToAny(v[0])

}
}

fieldName, tagName := name.GetFieldAndTagName(k)
if opt.TagNameFromKey {
tagName = k
Expand Down
6 changes: 6 additions & 0 deletions json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,13 @@ func (f *JSON) marshalValue(key string, obj interface{}, fromArray bool, depth i
if ok {
f.GetValue[keyPath] = fmt.Sprintf("%s", obj)
}
}

if f.GetRawValue != nil {
_, ok := f.GetRawValue[keyPath]
if ok {
f.GetRawValue[keyPath] = obj
}
}

tmpFieldName := strings.ToUpper(fieldName)
Expand Down
8 changes: 8 additions & 0 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Option struct {
StructName string
TypeMap map[string]string
GetValue map[string]string
GetRawValue map[string]any
TagNameFromKey bool
OutputFmtBefore io.Writer //需要format之前的数据
}
Expand Down Expand Up @@ -78,6 +79,13 @@ func WithGetValue(getValue map[string]string) OptionFunc {
}
}

// 目前支持json/yaml/http header/query string
func WithGetRawValue(getValue map[string]any) OptionFunc {
return func(c *Option) {
c.GetRawValue = getValue
}
}

// tag使用变量名, http header特殊一点
// 目前仅仅支持http header marshal
func WithTagNameFromKey() OptionFunc {
Expand Down