Skip to content

Releases: antlabs/tostruct

v0.0.9版本

13 Mar 15:42
a77c5a6
Compare
Choose a tag to compare

新加 option.WithGetRawValue 指定字段变为指针类型。

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"`
}
*/

v0.0.8版本

25 Feb 15:36
Compare
Choose a tag to compare

json/yaml 字符串可生成protobuf message定义

import (
    "github.com/antlabs/tostruct/protobuf"
    "github.com/antlabs/tostruct/option"
)

func main() {
	all, err := Marshal([]byte(`{"int":3, "float64": 3.14, "string":"hello"}`), option.WithStructName("reqProtobuf"))
	fmt.Println(err)
	os.Stdout.Write(all)
}
/*
message reqProtobuf {
    float64 Float64 = 1;
    int64 Int = 2;
    string String = 3;
}
*/

v0.0.7版本

24 Feb 13:01
Compare
Choose a tag to compare

bugfix

v0.0.6版本

07 Feb 15:03
eec06a1
Compare
Choose a tag to compare

支持map[string]any和[]any类型生成protobuf message定义

v0.0.5版本

30 Jan 14:25
ccc4a98
Compare
Choose a tag to compare

把json串序列化成struct字符串的时候,顺带提取json字符串里面指定的key值, value是any(int/string/float64)类型。
可以暴露指定key的类型信息。

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"]))

v0.0.4

29 Jan 15:42
6ec21d6
Compare
Choose a tag to compare

2.3 option.WithOutputFmtBefore 支持(json/yaml/http header/query string)

获取格式代码之前的buf输出,方便debug

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值

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"))

v0.0.3版本

28 Jan 14:48
50407a9
Compare
Choose a tag to compare
2.2 option.WithTagNameFromKey tag直接使用key的名字(支持http header)
http header比较特殊传递的标准header头会有-可以使用option.WithTagNameFromKey()指定直接使用key的名字当作tag名(header:"xxx")这里的xxx

h := http.Header{
	"Content-Type":  []string{"application/json"},
	"Accept": []string{"application/json"},
}

res, err := http.Marshal(h, option.WithStructName("test"), option.WithTagNameFromKey())

// output res
type test struct {
	Accept      string `header:"Accept"`
	ContentType string `header:"Content-Type"`
}

v0.0.2版本

27 Jan 15:23
b7d7eb0
Compare
Choose a tag to compare

option.WithSpecifyType 指定生成类型

obj := `
{
  "action": "get",
  "count": 0,
  "data": {
    "a123": "delivered"
  },
  "duration": 5,
  "entities": [],
  "timestamp": 1542601830084,
  "uri": "http://XXXX/XXXX/XXXX/users/user1/offline_msg_status/123"
}
  `
// 默认对象是转成结构体的,比如这里的data成员,有些接口返回的key是变动的,比如key是用户名value是消息是否投递成功(假装在im系统中)
// 此类业务就需要转成map[string]string类型,这里就可以用上option.WithSpecifyType
// 传参的类型是map[string]string, key为json路径,value值是要指定的类型
all, err := Marshal([]byte(obj), option.WithStructName("reqName"), option.WithTagName("json"), option.WithSpecifyType(map[string]string{
	".data": "map[string]string",
}))

// output all
type reqName struct {
	Action    string            `json:"action"`
	Count     int               `json:"count"`
	Data      map[string]string `json:"data"`
	Duration  int               `json:"duration"`
	entities  interface{}       `json:"json:entities"`
	Timestamp int               `json:"timestamp"`
	URI       string            `json:"uri"`
}

v0.0.1版本

22 Jan 04:25
3e53048
Compare
Choose a tag to compare

json/yaml/http header/query string 都可以生成结构体定义。
这是一个json生成结构体定义的例子。

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"`
	}
	*/
}