项目作者: devfeel

项目描述 :
A simple and easy go tools for auto mapper map to struct, struct to map, struct to struct, slice to slice, map to slice, map to json.
高级语言: Go
项目地址: git://github.com/devfeel/mapper.git
创建时间: 2017-11-14T12:42:06Z
项目社区:https://github.com/devfeel/mapper

开源协议:

下载


devfeel/mapper

A simple and easy go tools for auto mapper struct to map, struct to struct, slice to slice, map to slice, map to json.

1. Install

  1. go get -u github.com/devfeel/mapper

2. Getting Started

Traditional Usage

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/devfeel/mapper"
  5. )
  6. type (
  7. User struct {
  8. Name string
  9. Age int
  10. Id string `mapper:"_id"`
  11. AA string `json:"Score"`
  12. Time time.Time
  13. }
  14. Student struct {
  15. Name string
  16. Age int
  17. Id string `mapper:"_id"`
  18. Score string
  19. }
  20. Teacher struct {
  21. Name string
  22. Age int
  23. Id string `mapper:"_id"`
  24. Level string
  25. }
  26. )
  27. func init() {
  28. mapper.Register(&User{})
  29. mapper.Register(&Student{})
  30. }
  31. func main() {
  32. user := &User{}
  33. userMap := &User{}
  34. teacher := &Teacher{}
  35. student := &Student{Name: "test", Age: 10, Id: "testId", Score: "100"}
  36. valMap := make(map[string]interface{})
  37. valMap["Name"] = "map"
  38. valMap["Age"] = 10
  39. valMap["_id"] = "x1asd"
  40. valMap["Score"] = 100
  41. valMap["Time"] = time.Now()
  42. mapper.Mapper(student, user)
  43. mapper.AutoMapper(student, teacher)
  44. mapper.MapperMap(valMap, userMap)
  45. fmt.Println("student:", student)
  46. fmt.Println("user:", user)
  47. fmt.Println("teacher", teacher)
  48. fmt.Println("userMap:", userMap)
  49. }

执行main,输出:

  1. student: &{test 10 testId 100}
  2. user: &{test 10 testId 100 0001-01-01 00:00:00 +0000 UTC}
  3. teacher &{test 10 testId }
  4. userMap: &{map 10 x1asd 100 2017-11-20 13:45:56.3972504 +0800 CST m=+0.006004001}

Object Usage

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/devfeel/mapper"
  5. )
  6. type (
  7. User struct {
  8. Name string `json:"name" mapper:"name"`
  9. Class int `mapper:"class"`
  10. Age int `json:"age" mapper:"-"`
  11. }
  12. Student struct {
  13. Name string `json:"name" mapper:"name"`
  14. Class int `mapper:"class"`
  15. Age []int `json:"age" mapper:"-"`
  16. }
  17. )
  18. func main() {
  19. user := &User{Name: "shyandsy", Class: 1, Age: 10}
  20. student := &Student{}
  21. // create mapper object
  22. m := mapper.NewMapper()
  23. // in the version < v0.7.8, we will use field name as key when mapping structs
  24. // we keep it as default behavior in this version
  25. m.SetEnableIgnoreFieldTag(true)
  26. student.Age = []int{1}
  27. // disable the json tag
  28. m.SetEnabledJsonTag(false)
  29. // student::age should be 1
  30. m.Mapper(user, student)
  31. fmt.Println("user:")
  32. fmt.Println(user)
  33. fmt.Println("student:")
  34. fmt.Println(student)
  35. }

执行main,输出:

  1. user:
  2. &{shyandsy 1 10}
  3. student:
  4. &{shyandsy 1 [1]}

Features

  • 支持不同结构体相同名称相同类型字段自动赋值,使用Mapper
  • 支持不同结构体Slice的自动赋值,使用MapperSlice
  • 支持字段为结构体时的自动赋值
  • 支持struct到map的自动映射赋值,使用Mapper
  • 支持map到struct的自动映射赋值,使用MapperMap
  • 支持map到struct slice的自动赋值,使用MapToSlice
  • 支持map与json的互相转换
  • 支持Time与Unix自动转换
  • 支持tag标签,tag关键字为 mapper
  • 兼容json-tag标签
  • 当tag为”-“时,将忽略tag定义,使用struct field name
  • 无需手动Register struct,内部自动识别
  • 支持开启关闭
    • SetEnabledTypeChecking(bool) // 类型检查
    • IsEnabledTypeChecking
    • SetEnabledMapperTag // mapper tag
    • IsEnabledMapperTag
    • SetEnabledJsonTag // json tag
    • IsEnabledJsonTag
    • SetEnabledAutoTypeConvert // auto type convert
    • IsEnabledAutoTypeConvert
    • SetEnabledMapperStructField // mapper struct field
    • IsEnabledMapperStructField