Commit 2d9df6c2 authored by suyanlong's avatar suyanlong

Add mapstructure and mapper package

parent 5fd5a2a5
...@@ -164,7 +164,7 @@ build-dep: ...@@ -164,7 +164,7 @@ build-dep:
aligner: aligner:
@aligner -r -c "//" -e ".go" -i ./internal/repo/a_repo-packr.go comment @aligner -r -c "//" -e ".go" -i ./internal/repo/a_repo-packr.go comment
goimports: goimports:fmt
@goimports-reviser -dir-path ./ -project-name github.com/link33/sidecar -ignore-dir mock -rm-unused -set-alias -format @goimports-reviser -dir-path ./ -project-name github.com/link33/sidecar -ignore-dir mock -rm-unused -set-alias -format
aligner-check: aligner-check:
......
...@@ -58,6 +58,7 @@ require ( ...@@ -58,6 +58,7 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20190912175916-7055855a373f // indirect github.com/davidlazar/go-crypto v0.0.0-20190912175916-7055855a373f // indirect
github.com/devfeel/mapper v0.7.5 // indirect
github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165 // indirect github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect github.com/go-playground/locales v0.13.0 // indirect
...@@ -131,7 +132,7 @@ require ( ...@@ -131,7 +132,7 @@ require (
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v0.1.1 // indirect github.com/minio/sha256-simd v0.1.1 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/mr-tron/base58 v1.1.3 // indirect github.com/mr-tron/base58 v1.1.3 // indirect
......
...@@ -111,6 +111,8 @@ github.com/davidlazar/go-crypto v0.0.0-20190912175916-7055855a373f/go.mod h1:rQY ...@@ -111,6 +111,8 @@ github.com/davidlazar/go-crypto v0.0.0-20190912175916-7055855a373f/go.mod h1:rQY
github.com/deckarep/golang-set v1.7.2-0.20201129021324-03b572015f8e h1:cZH/C3SR4/BGzKwFwtq5PfYqbKYeJpSFw/4coUAV0/A= github.com/deckarep/golang-set v1.7.2-0.20201129021324-03b572015f8e h1:cZH/C3SR4/BGzKwFwtq5PfYqbKYeJpSFw/4coUAV0/A=
github.com/deckarep/golang-set v1.7.2-0.20201129021324-03b572015f8e/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ= github.com/deckarep/golang-set v1.7.2-0.20201129021324-03b572015f8e/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ=
github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218= github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218=
github.com/devfeel/mapper v0.7.5 h1:YZISGXQbYkdh5Gn3uJlL++UqCzMJDxZoYKAtS58ZdGo=
github.com/devfeel/mapper v0.7.5/go.mod h1:foz4u16jrssGoDfnWYQGFcthjlU6uBV5UV8uYJfKneA=
github.com/dgraph-io/badger v1.5.5-0.20190226225317-8115aed38f8f/go.mod h1:VZxzAIRPHRVNRKRo6AXrX9BJegn6il06VMTZVJYCIjQ= github.com/dgraph-io/badger v1.5.5-0.20190226225317-8115aed38f8f/go.mod h1:VZxzAIRPHRVNRKRo6AXrX9BJegn6il06VMTZVJYCIjQ=
github.com/dgraph-io/badger v1.6.0-rc1/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= github.com/dgraph-io/badger v1.6.0-rc1/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
...@@ -689,6 +691,8 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu ...@@ -689,6 +691,8 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.2 h1:6h7AQ0yhTcIsmFmnAwQls75jp2Gzs4iB8W7pjMO+rqo=
github.com/mitchellh/mapstructure v1.4.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
......
package tool
import "github.com/mitchellh/mapstructure"
// https://github.com/devfeel/mapper
var Decode = mapstructure.Decode
package tool
import (
"fmt"
"testing"
"time"
"github.com/devfeel/mapper"
"github.com/stretchr/testify/assert"
)
func ExampleDecode() {
type Person struct {
Name string
Age int
Emails []string
Extra map[string]string
}
// This input can come from anywhere, but typically comes from
// something like decoding JSON where we're not quite sure of the
// struct initially.
input := map[string]interface{}{
"name": "Mitchell",
"age": 91,
"emails": []string{"one", "two", "three"},
"extra": map[string]string{
"twitter": "mitchellh",
},
}
var result Person
err := Decode(input, &result)
if err != nil {
panic(err)
}
fmt.Printf("%#v", result)
// Output:
// tool.Person{Name:"Mitchell", Age:91, Emails:[]string{"one", "two", "three"}, Extra:map[string]string{"twitter":"mitchellh"}}
}
func TestMapper(t *testing.T) {
type (
User struct {
Name string
Age int
Id string `mapper:"_id"`
AA string `json:"Score"`
Time time.Time
}
Student struct {
Name string
Age int
Id string `mapper:"_id"`
Score string
}
Teacher struct {
Name string
Age int
Id string `mapper:"_id"`
Level string
}
)
mapper.Register(&User{})
mapper.Register(&Student{})
user := &User{}
userMap := &User{}
teacher := &Teacher{}
student := &Student{Name: "test", Age: 10, Id: "testId", Score: "100"}
valMap := make(map[string]interface{})
valMap["Name"] = "map"
valMap["Age"] = 10
valMap["_id"] = "x1asd"
valMap["Score"] = 100
valMap["Time"] = time.Now()
assert.NotNil(t, mapper.Mapper(student, user))
assert.NotNil(t, mapper.AutoMapper(student, teacher))
assert.NotNil(t, mapper.MapperMap(valMap, userMap))
fmt.Println("student:", student)
fmt.Println("user:", user)
fmt.Println("teacher", teacher)
fmt.Println("userMap:", userMap)
}
...@@ -44,3 +44,6 @@ func Exist(path string) bool { ...@@ -44,3 +44,6 @@ func Exist(path string) bool {
// https://github.com/Ompluscator/dynamic-struct // https://github.com/Ompluscator/dynamic-struct
// https://github.com/jhump/protoreflect // https://github.com/jhump/protoreflect
// https://github.com/bufbuild/buf // https://github.com/bufbuild/buf
// go get -u github.com/zekroTJA/timedmap
// https://github.com/m-zajac/json2go
// https://github.com/mitchellh/mapstructure
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment