直接上git地址:
https://github.com/pkgng/structor
https://github.com/pkgng/copit
第二个是简单相同名字字段copy的库,也可以定义字段计算方法,但是只支持接受一个字段。
第一个链接是通过struct Tag自定义字段计算方法的,语法是javaScrip,非常好用。
记得给作者Star 哦!
Demo 源代码:
```
package main
import (
"encoding/json"
"fmt"
"github.com/pkgng/structor"
)
type Human struct {
Name string
Role string
Age *int32
Tel string
Notes []string
Flags string
}
type AddressT struct {
Address string
Adcode string
Gps string
}
type WifeT struct {
structor.BaseStructor `structor:"CopyByName,Wife"`
Name string
NickName string `structor:"Wife.Name.toLocaleLowerCase()"`
Age int
Age3 int32 `structor:"self.Age + 3"`
}
type Farmer struct {
structor.BaseStructor `structor:"CopyByName,Human,address"`
Name string
Age int64
Nickname string `structor:"self.Name.toLocaleLowerCase()"`
DoubleAge int32 `structor:"Human.Age * 2"`
SuperRole string `structor:"'Farmer-' + Human.Role"`
Notes []string `structor:"Human.Notes.reverse()"`
Flags []string `structor:"Human.Flags.split(',')"`
Contact struct {
Tel string `structor:"Human.Tel"`
Address string `structor:"address.Address"`
Adcode string `structor:"address.Adcode"`
}
Wife WifeT
}
func main() {
var age int32 = 23
var age2 int32 = 22
man := Human{Name: "LiLei", Age: &age, Tel: "18611009988", Role: "Farmer", Notes: []string{"hello", "world"}, Flags: "a,b,c"}
address := AddressT{Adcode: "110108", Address: "北京海淀区五道口优盛大厦D座", Gps: "116.328115,40.054629"}
wife := Human{Name: "HanMeiMei", Age: &age2, Role: "Wife", Notes: []string{"hello", "world"}, Flags: "e,f,g"}
farmer := Farmer{}
structor.New().Set("Human", &man).Set("address", address).Set("Wife", &wife).Construct(&farmer)
// fmt.Printf("%#v\n", farmer)
b, err := json.Marshal(farmer)
if err != nil {
fmt.Println("JSON ERR:", err)
}
fmt.Println(string(b))
}
```
Demo 结果:
```
{
"Name":"LiLei",
"Age":23,
"Nickname":"lilei",
"DoubleAge":46,
"SuperRole":"Farmer-Farmer",
"Notes":["world","hello"],
"Flags":["a","b","c"],
"Contact":{
"Tel":"18611009988",
"Address":"北京海淀区五道口优盛大厦D座",
"Adcode":"110108"
},
"Wife":{
"Name":"HanMeiMei",
"NickName":"hanmeimei",
"Age":22,
"Age3":25
}
}
```
总结:
很多web 数据api开发时需要根据各种数据表model组合出返回前端的结构,有的需要对字段做裁剪,有的需要修改格式,有的需要经过简单计算等,有了这个工具不用在一个一个写了
有疑问加站长微信联系(非本文作者)