Skip to content

Commit

Permalink
Modify struct field setting logic. (#138)
Browse files Browse the repository at this point in the history
resolve the issues #137
  • Loading branch information
creky authored Dec 8, 2023
1 parent 58c5080 commit 4eb9721
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
6 changes: 3 additions & 3 deletions dump/_examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module dump_example
go 1.18

require (
github.com/gookit/color v1.5.2
github.com/gookit/color v1.5.4
github.com/gookit/goutil v0.6.6-00010101000000-000000000000
github.com/kortschak/utter v1.0.1
github.com/kr/pretty v0.2.1
Expand All @@ -12,8 +12,8 @@ require (
require (
github.com/kr/text v0.1.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
)

replace github.com/gookit/goutil => ../../
3 changes: 3 additions & 0 deletions dump/_examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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/gookit/color v1.5.2 h1:uLnfXcaFjlrDnQDT+NCBcfhrXqYTx/rcCa6xn01Y8yI=
github.com/gookit/color v1.5.2/go.mod h1:w8h4bGiHeeBpvQVePTutdbERIUf3oJE5lZ8HM0UgXyg=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/kortschak/utter v1.0.1 h1:AJVccwLrdrikvkH0aI5JKlzZIORLpfMeGBQ5tHfIXis=
github.com/kortschak/utter v1.0.1/go.mod h1:vSmSjbyrlKjjsL71193LmzBOKgwePk9DH6uFaWHIInc=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
Expand All @@ -24,8 +25,10 @@ golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZ
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
29 changes: 21 additions & 8 deletions structs/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package structs

import (
"errors"
"fmt"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/timex"
"reflect"
"time"

"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/reflects"
Expand Down Expand Up @@ -100,7 +102,7 @@ func setValues(rv reflect.Value, data map[string]any, opt *SetOptions) error {
if ok {
info, err := ParseTagValueDefault(name, tagVal)
if err != nil {
return err
continue
}
name = info.Get("name")
}
Expand All @@ -112,7 +114,7 @@ func setValues(rv reflect.Value, data map[string]any, opt *SetOptions) error {
if !ok && opt.ParseDefault && fv.IsZero() {
defVal := ft.Tag.Get(opt.DefaultValTag)
if err := initDefaultValue(fv, defVal, opt.ParseDefaultEnv); err != nil {

Check failure on line 116 in structs/writer.go

View workflow job for this annotation

GitHub Actions / staticcheck

[staticcheck] structs/writer.go#L116 <SA9003>(https://staticcheck.io/docs/checks#SA9003)

empty branch
Raw output
{"source":{"name":"staticcheck","url":"https://staticcheck.io"},"message":"empty branch","code":{"value":"SA9003","url":"https://staticcheck.io/docs/checks#SA9003"},"location":{"path":"/home/runner/work/goutil/goutil/structs/writer.go","range":{"start":{"line":116,"column":4}}},"severity":"ERROR"}
return err
//return err
}
continue
}
Expand All @@ -128,19 +130,30 @@ func setValues(rv reflect.Value, data map[string]any, opt *SetOptions) error {
// field is struct
if fv.Kind() == reflect.Struct {
asMp, err := maputil.TryAnyMap(val)
if err != nil {
return fmt.Errorf("must provide map data for field %q, err=%v", ft.Name, err)
if err == nil {
continue
}

if _, ok := fv.Interface().(time.Time); ok {
tm, er := timex.TryToTime(strutil.StringOr(val, ""), time.Time{})
if er != nil {
continue
}
if er = reflects.SetValue(fv, tm); er == nil {
continue
}
}

if err := setValues(fv, asMp, opt); err != nil {
return err
if err = setValues(fv, asMp, opt); err == nil {
continue
}

continue
}

// set field value
if err := reflects.SetValue(fv, val); err != nil {
return err
continue
}
}

Expand Down

0 comments on commit 4eb9721

Please sign in to comment.