祝大家新年快乐,新的一年经济恢复,rmb 多多。
https://github.com/antlabs/tostruct
json/yaml/http header/query string 转成 struct 定义,免去手写 struct 的烦恼.
import (
"github.com/antlabs/tostruct/json"
"github.com/antlabs/tostruct/option"
)
func main() {
var str = `{
"action": "Deactivate user",
"entities": [
{
"uuid": "4759aa70-XXXX-XXXX-925f-6fa0510823ba",
"type": "user",
"created": 1542595573399,
"modified": 1542597578147,
"username": "user1",
"activated": false,
"nickname": "user"
}],
"timestamp": 1542602157258,
"duration": 12
}`
// 父子结构合在一起
all, _ := json.Marshal([]byte(str), option.WithStructName("reqName"))
fmt.Println(string(all))
/*
type reqName struct {
Action string `json:"action"`
Duration int `json:"duration"`
Entities []struct {
Activated bool `json:"activated"`
Created int `json:"created"`
Modified int `json:"modified"`
Nickname string `json:"nickname"`
Type string `json:"type"`
Username string `json:"username"`
UUID string `json:"uuid"`
} `json:"entities"`
Timestamp int `json:"timestamp"`
}
*/
// 子结构拆分
all, _ := json.Marshal([]byte(str), option.WithStructName("reqName"), option.WithNotInline())
fmt.Println(string(all))
/*
type reqName struct {
Action string `json:"action"`
Duration int `json:"duration"`
Entities []Entities `json:"entities"`
Timestamp int `json:"timestamp"`
}
type Entities struct {
Activated bool `json:"activated"`
Created int `json:"created"`
Modified int `json:"modified"`
Nickname string `json:"nickname"`
Type string `json:"type"`
Username string `json:"username"`
UUID string `json:"uuid"`
}
*/
}
import (
"github.com/antlabs/tostruct/header"
"github.com/antlabs/tostruct/option"
"net/http"
)
func main() {
h := http.header{
"bool": []string{"true"},
"int" : []string{"1"},
"string" : []string{"hello"},
"float64" : []string{"3.14"},
}
res, err := header.Marshal(h, option.WithStructName("test"), option.WithTagName("header"))
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(res))
/*
type test struct {
Bool bool `header:"bool"`
Float64 float64 `header:"float64"`
Int int `header:"int"`
String string `header:"string"`
}
*/
}
import (
"github.com/antlabs/tostruct/url"
"github.com/antlabs/tostruct/option"
)
func main() {
url := "http://127.0.0.1:8080?int=1&float64=1.1&bool=true&string=hello"
res, err := header.Marshal(h, option.WithStructName("test"), option.WithTagName("form"))
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(res))
/*
type test struct {
Bool bool `form:"bool"`
Float64 float64 `form:"float64"`
Int int `form:"int"`
String string `form:"string"`
}
*/
}
import (
"github.com/antlabs/tostruct/url"
"github.com/antlabs/tostruct/option"
"github.com/antlabs/tostruct/yaml"
)
func main() {
str := `
a: 1
b: 3.14
c: hello
d:
- aa
- bb
e:
- a: 1
b: 3.14
c: hello
f:
first: 1
second: 3.14
`
all, err := Marshal([]byte(str), option.WithStructName("reqName"))
if err != nil {
return
}
fmt.Println(all)
/*
type reqName struct {
A int `yaml:"a"`
B float64 `yaml:"b"`
C string `yaml:"c"`
D []string `yaml:"d"`
E []struct {
A int `yaml:"a"`
B float64 `yaml:"b"`
C string `yaml:"c"`
} `yaml:"e"`
F struct {
First int `yaml:"first"`
Second float64 `yaml:"second"`
} `yaml:"f"`
}
*/
all, err := Marshal([]byte(str), option.WithStructName("reqName"), option.WithNotInline())
if err != nil {
return
}
fmt.Println(all)
/*
type reqName struct {
A int `yaml:"a"`
B float64 `yaml:"b"`
C string `yaml:"c"`
D []string `yaml:"d"`
E []E `yaml:"e"`
F F `yaml:"f"`
}
type E struct {
A int `yaml:"a"`
B float64 `yaml:"b"`
C string `yaml:"c"`
}
type F struct {
First int `yaml:"first"`
Second float64 `yaml:"second"`
}
*/
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.