Skip to content

Commit

Permalink
👔 up: structs - update the SetValues logic will continue on set error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 9, 2023
1 parent f9a9690 commit dd72232
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
34 changes: 33 additions & 1 deletion comdef/errors.go
@@ -1,6 +1,38 @@
package comdef

import "errors"
import (
"errors"
"strings"
)

// ErrConvType error
var ErrConvType = errors.New("convert value type error")

// Errors multi error list
type Errors []error

// Error string
func (es Errors) Error() string {
var sb strings.Builder
for _, err := range es {
sb.WriteString(err.Error())
sb.WriteByte('\n')
}
return sb.String()
}

// ErrOrNil error
func (es Errors) ErrOrNil() error {
if len(es) == 0 {
return nil
}
return es
}

// First error
func (es Errors) First() error {
if len(es) > 0 {
return es[0]
}
return nil
}
38 changes: 25 additions & 13 deletions structs/writer.go
Expand Up @@ -2,13 +2,14 @@ package structs

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

"github.com/gookit/goutil/comdef"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/reflects"
"github.com/gookit/goutil/strutil"
)

// NewWriter create a struct writer
Expand Down Expand Up @@ -48,6 +49,9 @@ type SetOptions struct {
//
// default: false
ParseDefaultEnv bool

// StopOnError if true, will stop set value on error happened. default: false
// StopOnError bool
}

// WithParseDefault value by tag "default"
Expand Down Expand Up @@ -87,6 +91,7 @@ func setValues(rv reflect.Value, data map[string]any, opt *SetOptions) error {
return nil
}

var es comdef.Errors
rt := rv.Type()

for i := 0; i < rt.NumField(); i++ {
Expand All @@ -102,6 +107,7 @@ func setValues(rv reflect.Value, data map[string]any, opt *SetOptions) error {
if ok {
info, err := ParseTagValueDefault(name, tagVal)
if err != nil {
es = append(es, err)
continue
}
name = info.Get("name")
Expand All @@ -114,7 +120,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 {
//return err
es = append(es, err)
}
continue
}
Expand All @@ -129,33 +135,39 @@ 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 {
continue
}

// up: special handle time.Time struct
if _, ok := fv.Interface().(time.Time); ok {
tm, er := timex.TryToTime(strutil.StringOr(val, ""), time.Time{})
tm, er := strutil.ToTime(strutil.StringOr(val, ""))
if er != nil {
es = append(es, er)
continue
}
if er = reflects.SetValue(fv, tm); er == nil {
continue
if er = reflects.SetValue(fv, tm); er != nil {
es = append(es, er)
}
continue
}

if err = setValues(fv, asMp, opt); err == nil {
asMp, err := maputil.TryAnyMap(val)
if err != nil {
err = fmt.Errorf("must provide map for set struct field %q, err=%v", ft.Name, err)
es = append(es, err)
continue
}

// recursive processing sub-struct
if err = setValues(fv, asMp, opt); err != nil {
es = append(es, err)
}
continue
}

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

return nil
return es.ErrOrNil()
}

0 comments on commit dd72232

Please sign in to comment.