根据字段里的 StructField 设置进行相应的处理,使用方法就好像 json 和 beego.orm 那样...

TagSugar

根据字段里的 StructField 设置进行相应的处理,使用方法就好像 json 和 beego.orm 那样。

有时候,数据库存储头像或者其它文件的时候保存的是一个路径或者一个 key,查询出来的时候,往往并没有拼接域名地址;如果每次都要自己手动拼接一次的话,真的感觉好累,为了不想多做不必要的逻辑判断和代码,然后就去看了下 json 和 beego.orm 的实现方法都是通过反射处理的,于是就有了 TagSugar。

还有些情况就是数据库字段保存的是一个 json 字符串,想要转换成一个 json 对象或数组。(在这里我使用的是 MySQL/MariaDB)

github.com/nukc/tagsug…

Usage

Download and install
go get github.com/nukc/tagsugar
复制代码
package main

import "github.com/nukc/tagsugar"

func main(){
    // if use url(http) options
    tagsugar.Http = "https://cdn.github.com/"

    tagsugar.Lick(&model)
}

复制代码

Tag options sample

  • url(http)
type Model struct {
    Id    int
    Image string `ts:"url(http)"`
}

model := Model{Id: 1, Image: "test.png"}
tagsugar.Lick(&model)

log.Print(model.Image)
// https://cdn.github.com/test.png
复制代码
  • initial

set a initial value

  • assign_to(otherFieldName)
  • assign_type(raw)
    • raw: default
    • bool: set the otherFieldName a bool
    • unmarshal: set the otherFieldName a json.Unmarshal(str, &obj) value
assign_type(bool)
type Model struct {
	Id    int
	Sex   int8   `ts:"assign_to(IsMan);assign_type(bool)"`
	IsMan bool
}

model := Model{Id: 2, Sex: 1}
// IsMan: false
tagsugar.Lick(&model)
// IsMan : true
复制代码
assign_type(unmarshal)
type Model struct {
	Id     int
	Json   string `ts:"assign_to(Object);assign_type(unmarshal)" json:"-"`
	Object interface{}
}

json := "{\"id\": 1, \"post\": 2}"
model := Model{Id: 3, Json: json}
// Object: <nil>
tagsugar.Lick(&model)
// Object: map[id:1 post:2]
复制代码
type Model struct {
	Id     int
	Json   string `ts:"assign_to(Post);assign_type(unmarshal)" json:"-"`
	Post   Post
}

type Post struct {
	Id   int
	Post int
}

json := "{\"id\": 2, \"post\": 6}"
model := Model{Id: 4, Json: json}
// Post: {0 0}
tagsugar.Lick(&model)
// Post: {2 6}
复制代码
type Model struct {
	Id     int
	Json   string `ts:"assign_to(Array);assign_type(unmarshal)" json:"-"`
	Array  []interface{}
}

json := "[{\"id\": 1, \"post\": 3},{\"id\": 2, \"post\": 66}]"
model := Model{Id: 4, Json: json}
// Array: []
tagsugar.Lick(&model)
// Array: [map[id:1 post:3] map[id:2 post:66]]
复制代码

License

Apache License, Version 2.0

转载于:https://juejin.im/post/5bbc6657f265da0a8b573e87

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值