项目作者: alexsem80

项目描述 :
go-mapper module is designed for automatic types conversion between structs, pointers, maps and slices
高级语言: Go
项目地址: git://github.com/alexsem80/go-mapper.git
创建时间: 2020-11-06T11:20:09Z
项目社区:https://github.com/alexsem80/go-mapper

开源协议:

下载


go-mapper

Installation

go get github.com/alexsem80/go-mapper

Usage

  1. package main
  2. import (
  3. "github.com/alexsem80/go-mapper/mapper"
  4. )
  5. func main() {
  6. src := Destination{
  7. Id: 1,
  8. Name: "Name",
  9. Weight: 32.32,
  10. Marks: []int32{2, 3, 4},
  11. Address: []*NestedDestination{
  12. {
  13. State: "RYA",
  14. Index: map[int]*DestinationMapType{
  15. 1: {Name: "Name1"},
  16. },
  17. },
  18. {
  19. State: "MOW",
  20. Index: map[int]*DestinationMapType{
  21. 2: {Name: "Name2"},
  22. },
  23. },
  24. },
  25. }
  26. dest := &Source{}
  27. mapper := mapper.NewMapper()
  28. mapper.CreateMap((*Source)(nil), (*Destination)(nil))
  29. mapper.CreateMap((*NestedSource)(nil), (*NestedDestination)(nil))
  30. mapper.CreateMap((*SourceMapType)(nil), (*DestinationMapType)(nil))
  31. mapper.Init()
  32. mapper.Map(dest, src)
  33. }
  34. type Source struct {
  35. ID int
  36. FirstName string `mapper:"Name"`
  37. Weight float32
  38. Marks []int32
  39. Address []*NestedSource
  40. }
  41. type NestedSource struct {
  42. State string
  43. Index map[int]*SourceMapType
  44. }
  45. type Destination struct {
  46. Id int `mapper:"ID"`
  47. Name string
  48. Weight float32
  49. Marks []int32
  50. Address []*NestedDestination
  51. }
  52. type NestedDestination struct {
  53. State string
  54. Index map[int]*DestinationMapType
  55. }
  56. type SourceMapType struct {
  57. Name string
  58. }
  59. type DestinationMapType struct {
  60. Name string
  61. }