golang 简单实现数据转换格式化 https://github.com/snowlyg/gotransformer
写这个项目的原因是,正在写一个关务系统的项目,模型对象的数据字段会非常多,多的可能会几十数据字段。这样在写接口的时候返回数据格式化的时候会非常痛苦(当然也可以不格式化),还有在做 excel 导入的时候,转换数据也是会非常麻烦。
因为目前还是从事 php 工作所以目前比较多在 learnku.com 论坛(https://learnku.com/articles/38643)。如果有问题,可以去那边留言,或者在 github 提交 issues。
同时有错误的地方,欢迎指出。
```go
package main
import (
"fmt"
"time"
"github.com/snowlyg/gotransform"
)
// 基础数据模型 beego/ormtype BaseModel struct {
Id int64
CreatedAt time.Time `orm:"auto_now_add;type(datetime);column(created_at);type(timestamp)"` UpdatedAt time.Time `orm:"auto_now;type(datetime);column(updated_at);type(timestamp)"`}
// 数据模型
type Model struct {
BaseModel
Name string `orm:"column(Name);null" description:""` Rmk string `orm:"column(rmk);size(255);null" description:""` DeletedAt time.Time `form:"-" orm:"column(deleted_at);type(timestamp);null" `}
// 格式化数据
type Response struct {
Id int64
Name string
Rmk string
DeletedAt string
CreatedAt string
UpdatedAt string
}
func main() {
// struct response := Response{}
baseModel := BaseModel{1,time.Now(),time.Now()}
model := Model{baseModel,"name","remark",time.Now()}
g := gotransform.NewTransform(&response, model, time.RFC3339)
err := g.Transformer()
if err != nil { _ = fmt.Sprintf("err:%v",err)
}
_ = fmt.Sprintf("response:%v",response)
// slice models := []*Model{&model}
var responses []*Response
for _, m := range models { r := Response{}
g1 := gotransform.NewTransform(&r, m, time.RFC3339)
err := g1.Transformer()
if err != nil { _ = fmt.Sprintf("err:%v",err)
} responses = append(responses, &r)
_ = fmt.Sprintf("responses:%v",responses)
}
}
```
## Simple Relation Example
简单关联关系格式化,使用 gtf 标识加`relationName.fieldName`
```
// 基础数据模型 beego/ormtype BaseModel struct {
Id int64 CreatedAt time.Time `orm:"auto_now_add;type(datetime);column(created_at);type(timestamp)"` UpdatedAt time.Time `orm:"auto_now;type(datetime);column(updated_at);type(timestamp)"`}
// 数据模型
type Model struct {
BaseModel Name string `orm:"column(Name);null" description:""` Parent *Parent `orm:"null;rel(fk)"` // RelForeignKey relation Rmk string `orm:"column(rmk);size(255);null" description:""` DeletedAt time.Time `form:"-" orm:"column(deleted_at);type(timestamp);null" `}
type Parent struct {
BaseModel Name string `orm:"column(Name);null" description:""`}
// 格式化数据
type Response struct {
Id int64 ParentName string `gtf:"Parent.Name"` Rmk string DeletedAt string CreatedAt string UpdatedAt string}
```
## One To More Relation Example
一对多关联关系格式化,使用 gtf 标识加`Func.FormatTime(arg)`,参数为关联关系名称
```
// 基础数据模型 beego/ormtype BaseModel struct {
Id int64 CreatedAt time.Time `orm:"auto_now_add;type(datetime);column(created_at);type(timestamp)"` UpdatedAt time.Time `orm:"auto_now;type(datetime);column(updated_at);type(timestamp)"`}
// 数据模型
type Model struct {
BaseModel Name string `orm:"column(Name);null" description:""` Parents []*Parent `orm:"null;rel(fk)"` // RelForeignKey relation Rmk string `orm:"column(rmk);size(255);null" description:""` DeletedAt time.Time `form:"-" orm:"column(deleted_at);type(timestamp);null" `}
// 数据模型
type Parent struct {
BaseModel Name string `orm:"column(Name);null" description:""`}
// 格式化数据
type Response struct {
Id int64 ParentName string `gtf:"Func.FormatTime(Parents)"` Rmk string DeletedAt string CreatedAt string UpdatedAt string}
func (r *Response) GetAdminName(vs []*models.Parent) string {
for _, v := range vs { //... }
return ""}
```
## 时间数据格式化
- 根据数据模型类型 time.Time 自动格式化,
- 默认使用 `gotransform.NewTransform(&response, model, time.RFC3339)` 定义的时间格式
- 自定义时间格式 使用标识 `gtf:"Time.2006-01-02 15:04:05"`
```
// 基础数据模型 beego/ormtype BaseModel struct {
Id int64 CreatedAt time.Time `orm:"auto_now_add;type(datetime);column(created_at);type(timestamp)"` UpdatedAt time.Time `orm:"auto_now;type(datetime);column(updated_at);type(timestamp)"`}
// 数据模型
type Model struct {
BaseModel Time time.Time `form:"-" orm:"column(Time);type(timestamp);null" ` Time1 time.Tim `form:"-" orm:"column(Time1);type(timestamp);null" ` DeletedAt time.Time `form:"-" orm:"column(deleted_at);type(timestamp);null" `}
// 格式化数据
type Response struct {
Id int64 Time string `gtf:"Time.2006-01-02 15:04:05"` Time1 string DeletedAt string
CreatedAt string UpdatedAt string}
```
## Func Example
[Func Example](https://github.com/snowlyg/gotransformer/blob/master/FUNC.md)
## Map Example
[Map Example](https://github.com/snowlyg/gotransformer/blob/master/MAP.md)
## Excel 导入数据转换
[Excel 导入数据转换](https://github.com/snowlyg/gotransformer/blob/master/XLSX.md)
有疑问加站长微信联系(非本文作者)