Skip to content

Commit

Permalink
allow struct tag, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
elee1766 committed Nov 28, 2023
1 parent 4917a00 commit 6463181
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/alicebob/miniredis/v2 v2.30.5
github.com/fatih/structtag v1.2.0
github.com/redis/go-redis/v9 v9.0.2
github.com/stretchr/testify v1.8.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.0.2 h1:BA426Zqe/7r56kCcvxYLWe1mkaz71LKF77GwgFzSxfE=
Expand Down
18 changes: 17 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/fatih/structtag"
"github.com/redis/go-redis/v9"
)

Expand Down Expand Up @@ -166,7 +167,22 @@ func mapToStruct(st any, data map[string]any) error {
fieldRv := rv.Field(i)
fieldRt := rt.Field(i)

v, ok := data[toSnakeCase(fieldRt.Name)]
fieldTag := fieldRt.Tag
tags, err := structtag.Parse(string(fieldTag))
if err != nil {
// failing to parse a struct tag means = tag is invalid = we should panic
panic(err)
}

var fieldName string
gtrsTag, err := tags.Get("gtrs")
if err == nil {
fieldName = gtrsTag.Name
} else {
fieldName = toSnakeCase(fieldRt.Name)
}

v, ok := data[fieldName]
if !ok {
continue
}
Expand Down
20 changes: 20 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ type Person struct {
Height float32
}

type TaggedPerson struct {
Name string
Age int
Height float32 `gtrs:"cm"`
}

type City struct {
Name string
Size int
Expand Down Expand Up @@ -61,6 +67,20 @@ func TestUtils_convertMapToStruct_Simple(t *testing.T) {
assert.Equal(t, Person{Name: "Vlad", Age: 19, Height: 172.0}, p1)
}

func TestUtils_convertMapToStruct_Tags(t *testing.T) {
m1 := map[string]any{
"name": "Vlad",
"age": "19",
"cm": "172",
}

var p1 TaggedPerson
err := mapToStruct(&p1, m1)

assert.Nil(t, err)
assert.Equal(t, TaggedPerson{Name: "Vlad", Age: 19, Height: 172.0}, p1)
}

func TestUtils_convertMapToStruct_AllTypes(t *testing.T) {
type AllTypes struct {
S string
Expand Down

0 comments on commit 6463181

Please sign in to comment.