@@ -23,8 +23,7 @@ import (
2323 "strconv"
2424 "strings"
2525
26- "github.com/go-spring/log"
27- "github.com/go-spring/spring-core/util"
26+ "github.com/go-spring/spring-base/util"
2827)
2928
3029var (
@@ -93,12 +92,12 @@ func (tag ParsedTag) String() string {
9392func ParseTag (tag string ) (ret ParsedTag , err error ) {
9493 j := strings .LastIndex (tag , "}" )
9594 if j <= 0 {
96- err = log .FormatError (ErrInvalidSyntax , "parse tag '%s' error" , tag )
95+ err = util .FormatError (ErrInvalidSyntax , "parse tag '%s' error" , tag )
9796 return
9897 }
9998 k := strings .Index (tag , "${" )
10099 if k < 0 {
101- err = log .FormatError (ErrInvalidSyntax , "parse tag '%s' error" , tag )
100+ err = util .FormatError (ErrInvalidSyntax , "parse tag '%s' error" , tag )
102101 return
103102 }
104103 if i := strings .LastIndex (tag , ">>" ); i > j {
@@ -145,7 +144,7 @@ func (param *BindParam) BindTag(tag string, validate reflect.StructTag) error {
145144 param .Tag = parsedTag
146145 return nil
147146 }
148- return log .FormatError (ErrInvalidSyntax , "parse tag '%s' error" , tag )
147+ return util .FormatError (ErrInvalidSyntax , "parse tag '%s' error" , tag )
149148 }
150149 if parsedTag .Key == "ROOT" {
151150 parsedTag .Key = ""
@@ -180,16 +179,18 @@ type Filter interface {
180179func BindValue (p Properties , v reflect.Value , t reflect.Type , param BindParam , filter Filter ) (RetErr error ) {
181180
182181 if ! util .IsPropBindingTarget (t ) {
183- err := errors . New ( "target should be value type" )
184- return log .FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
182+ err := util . FormatError ( nil , "target should be value type" )
183+ return util .FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
185184 }
186185
187186 // run validation if "expr" tag is defined and no prior error
188187 defer func () {
189188 if RetErr == nil {
190189 tag , ok := param .Validate .Lookup ("expr" )
191190 if ok && len (tag ) > 0 {
192- RetErr = validateField (tag , v .Interface ())
191+ if RetErr = validateField (tag , v .Interface ()); RetErr != nil {
192+ RetErr = util .FormatError (RetErr , "validate path=%s type=%s error" , param .Path , v .Type ().String ())
193+ }
193194 }
194195 }
195196 }()
@@ -200,8 +201,8 @@ func BindValue(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
200201 case reflect .Slice :
201202 return bindSlice (p , v , t , param , filter )
202203 case reflect .Array :
203- err := errors . New ( "use slice instead of array" )
204- return log .FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
204+ err := util . FormatError ( nil , "use slice instead of array" )
205+ return util .FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
205206 default : // for linter
206207 }
207208
@@ -216,7 +217,7 @@ func BindValue(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
216217 // resolve property value (with default and references)
217218 val , err := resolve (p , param )
218219 if err != nil {
219- return log . WrapError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
220+ return util . FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
220221 }
221222
222223 // try converter function first
@@ -225,7 +226,7 @@ func BindValue(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
225226 out := fnValue .Call ([]reflect.Value {reflect .ValueOf (val )})
226227 if ! out [1 ].IsNil () {
227228 err = out [1 ].Interface ().(error )
228- return log . WrapError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
229+ return util . FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
229230 }
230231 v .Set (out [0 ])
231232 return nil
@@ -239,28 +240,28 @@ func BindValue(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
239240 v .SetUint (u )
240241 return nil
241242 }
242- return log . WrapError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
243+ return util . FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
243244 case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 :
244245 var i int64
245246 if i , err = strconv .ParseInt (val , 0 , 0 ); err == nil {
246247 v .SetInt (i )
247248 return nil
248249 }
249- return log . WrapError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
250+ return util . FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
250251 case reflect .Float32 , reflect .Float64 :
251252 var f float64
252253 if f , err = strconv .ParseFloat (val , 64 ); err == nil {
253254 v .SetFloat (f )
254255 return nil
255256 }
256- return log . WrapError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
257+ return util . FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
257258 case reflect .Bool :
258259 var b bool
259260 if b , err = strconv .ParseBool (val ); err == nil {
260261 v .SetBool (b )
261262 return nil
262263 }
263- return log . WrapError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
264+ return util . FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
264265 default :
265266 // treat everything else as string
266267 v .SetString (val )
@@ -283,7 +284,7 @@ func bindSlice(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
283284 elemType := t .Elem ()
284285 p , err := getSlice (p , elemType , param )
285286 if err != nil {
286- return log . WrapError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
287+ return util . FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
287288 }
288289
289290 slice := reflect .MakeSlice (t , 0 , 0 )
@@ -305,7 +306,7 @@ func bindSlice(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
305306 break
306307 }
307308 if err != nil {
308- return log . WrapError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
309+ return util . FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
309310 }
310311 slice = reflect .Append (slice , subValue )
311312 }
@@ -336,13 +337,13 @@ func getSlice(p Properties, et reflect.Type, param BindParam) (Properties, error
336337 strVal = p .Get (param .Key )
337338 } else {
338339 if ! param .Tag .HasDef {
339- return nil , fmt . Errorf ( "property %q %w" , param .Key , ErrNotExist )
340+ return nil , util . FormatError ( nil , "property %q %w" , param .Key , ErrNotExist )
340341 }
341342 if param .Tag .Def == "" {
342343 return nil , nil
343344 }
344345 if ! util .IsPrimitiveValueType (et ) && converters [et ] == nil {
345- return nil , fmt . Errorf ( "can't find converter for %s" , et .String ())
346+ return nil , util . FormatError ( nil , "can't find converter for %s" , et .String ())
346347 }
347348 strVal = param .Tag .Def
348349 }
@@ -365,10 +366,10 @@ func getSlice(p Properties, et reflect.Type, param BindParam) (Properties, error
365366 } else if fn , ok := splitters [s ]; ok && fn != nil {
366367 // use custom splitter function
367368 if arrVal , err = fn (strVal ); err != nil {
368- return nil , log .FormatError (err , "split %q error" , strVal )
369+ return nil , util .FormatError (err , "split %q error" , strVal )
369370 }
370371 } else {
371- return nil , fmt . Errorf ( "unknown splitter '%s'" , s )
372+ return nil , util . FormatError ( nil , "unknown splitter '%s'" , s )
372373 }
373374
374375 r := New ()
@@ -396,8 +397,8 @@ func getSlice(p Properties, et reflect.Type, param BindParam) (Properties, error
396397func bindMap (p Properties , v reflect.Value , t reflect.Type , param BindParam , filter Filter ) error {
397398
398399 if param .Tag .HasDef && param .Tag .Def != "" {
399- err := errors . New ( "map can't have a non-empty default value" )
400- return log .FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
400+ err := util . FormatError ( nil , "map can't have a non-empty default value" )
401+ return util .FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
401402 }
402403
403404 elemType := t .Elem ()
@@ -416,13 +417,13 @@ func bindMap(p Properties, v reflect.Value, t reflect.Type, param BindParam, fil
416417 if param .Tag .HasDef {
417418 return nil
418419 }
419- return fmt . Errorf ( "property %q %w" , param .Key , ErrNotExist )
420+ return util . FormatError ( nil , "property %q %w" , param .Key , ErrNotExist )
420421 }
421422
422423 // fetch subkeys under the current key prefix
423424 keys , err := p .SubKeys (param .Key )
424425 if err != nil {
425- return log . WrapError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
426+ return util . FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
426427 }
427428
428429 for _ , key := range keys {
@@ -464,8 +465,8 @@ func bindMap(p Properties, v reflect.Value, t reflect.Type, param BindParam, fil
464465func bindStruct (p Properties , v reflect.Value , t reflect.Type , param BindParam , filter Filter ) error {
465466
466467 if param .Tag .HasDef && param .Tag .Def != "" {
467- err := errors . New ( "struct can't have a non-empty default value" )
468- return log .FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
468+ err := util . FormatError ( nil , "struct can't have a non-empty default value" )
469+ return util .FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
469470 }
470471
471472 for i := range t .NumField () {
@@ -484,12 +485,12 @@ func bindStruct(p Properties, v reflect.Value, t reflect.Type, param BindParam,
484485
485486 if tag , ok := ft .Tag .Lookup ("value" ); ok {
486487 if err := subParam .BindTag (tag , ft .Tag ); err != nil {
487- return log . WrapError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
488+ return util . FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
488489 }
489490 if filter != nil {
490491 ret , err := filter .Do (fv .Addr ().Interface (), subParam )
491492 if err != nil {
492- return log . WrapError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
493+ return util . FormatError (err , "bind path=%s type=%s error" , param .Path , v .Type ().String ())
493494 }
494495 if ret {
495496 continue
@@ -531,12 +532,12 @@ func resolve(p Properties, param BindParam) (string, error) {
531532 return resolveString (p , val )
532533 }
533534 if p .Has (param .Key ) {
534- return "" , fmt . Errorf ( "property %q isn't simple value" , param .Key )
535+ return "" , util . FormatError ( nil , "property %q isn't simple value" , param .Key )
535536 }
536537 if param .Tag .HasDef {
537538 return resolveString (p , param .Tag .Def )
538539 }
539- return "" , fmt . Errorf ( "property %q %w" , param .Key , ErrNotExist )
540+ return "" , util . FormatError ( nil , "property %q %w" , param .Key , ErrNotExist )
540541}
541542
542543// resolveString expands property references of the form ${key}
@@ -590,7 +591,7 @@ func resolveString(p Properties, s string) (string, error) {
590591
591592 if end < 0 {
592593 err := ErrInvalidSyntax
593- return "" , log .FormatError (err , "resolve string %q error" , s )
594+ return "" , util .FormatError (err , "resolve string %q error" , s )
594595 }
595596
596597 var param BindParam
@@ -599,13 +600,13 @@ func resolveString(p Properties, s string) (string, error) {
599600 // resolve the referenced property
600601 resolved , err := resolve (p , param )
601602 if err != nil {
602- return "" , log . WrapError (err , "resolve string %q error" , s )
603+ return "" , util . FormatError (err , "resolve string %q error" , s )
603604 }
604605
605606 // resolve the remaining part of the string
606607 suffix , err := resolveString (p , s [end + 1 :])
607608 if err != nil {
608- return "" , log . WrapError (err , "resolve string %q error" , s )
609+ return "" , util . FormatError (err , "resolve string %q error" , s )
609610 }
610611
611612 // combine: prefix + resolved + suffix
0 commit comments