Skip to content
Snippets Groups Projects
Commit f7e7aa34 authored by Danis Kirasirov's avatar Danis Kirasirov
Browse files

refactoring

parent ea8660ba
Branches
No related tags found
No related merge requests found
...@@ -16,7 +16,7 @@ type Encoder interface { ...@@ -16,7 +16,7 @@ type Encoder interface {
// NonStrictConverter определяет метод для преобразования данных в нестрогом режиме. // NonStrictConverter определяет метод для преобразования данных в нестрогом режиме.
type NonStrictConverter interface { type NonStrictConverter interface {
NonStrictConverter(ctx context.Context, field *Field, v interface{}) interface{} NonStrictConvert(ctx context.Context, field *Field, v interface{}) interface{}
} }
type EncodeOptions struct { type EncodeOptions struct {
...@@ -48,22 +48,24 @@ func Decode(ctx context.Context, w Walker, v interface{}, opts ...EncodeOption) ...@@ -48,22 +48,24 @@ func Decode(ctx context.Context, w Walker, v interface{}, opts ...EncodeOption)
} }
val, _, err := w.Walk(ctx, v, func(ctx context.Context, f *Field, v interface{}) (res WalkFuncResult, err error) { val, _, err := w.Walk(ctx, v, func(ctx context.Context, f *Field, v interface{}) (res WalkFuncResult, err error) {
decoder, ok := f.GetType().(Decoder)
if !ok {
res.Value = v
return res, nil
}
if opt.NonStrictMode { if opt.NonStrictMode {
if converter, ok := f.GetType().(NonStrictConverter); ok { if converter, hasConv := f.GetType().(NonStrictConverter); hasConv {
v = converter.NonStrictConverter(ctx, f, v) v = converter.NonStrictConvert(ctx, f, v)
} }
} }
if decoder, ok := f.GetType().(Decoder); ok {
if v, err = decoder.Decode(ctx, f, v); err != nil { if v, err = decoder.Decode(ctx, f, v); err != nil {
return return
} }
res.Value = v res.Value = v
res.Changed = true res.Changed = true
return return
}
res.Value = v
return
}, walkOpts...) }, walkOpts...)
if err != nil { if err != nil {
......
...@@ -139,7 +139,7 @@ func (n NumberType) Encode(ctx context.Context, field *Field, v interface{}) (in ...@@ -139,7 +139,7 @@ func (n NumberType) Encode(ctx context.Context, field *Field, v interface{}) (in
return n.decode(ctx, field, v) return n.decode(ctx, field, v)
} }
func (NumberType) NonStrictConverter(_ context.Context, _ *Field, v interface{}) interface{} { func (NumberType) NonStrictConvert(_ context.Context, _ *Field, v interface{}) interface{} {
if v == nil { if v == nil {
return nil return nil
} }
......
...@@ -58,7 +58,7 @@ func (StringType) Encode(_ context.Context, _ *Field, v interface{}) (interface{ ...@@ -58,7 +58,7 @@ func (StringType) Encode(_ context.Context, _ *Field, v interface{}) (interface{
return nil, fmt.Errorf("StringField encode error: unsupported value type : \"%s\"", reflect.ValueOf(v).Kind()) return nil, fmt.Errorf("StringField encode error: unsupported value type : \"%s\"", reflect.ValueOf(v).Kind())
} }
func (StringType) NonStrictConverter(_ context.Context, _ *Field, v interface{}) interface{} { func (StringType) NonStrictConvert(_ context.Context, _ *Field, v interface{}) interface{} {
if v == nil { if v == nil {
return nil return nil
} }
......
...@@ -37,9 +37,9 @@ func WalkSchema() WalkOption { ...@@ -37,9 +37,9 @@ func WalkSchema() WalkOption {
} }
} }
// WalkNonStrict указывает что обход может выполняться в нестрогом режиме. // WalkNonStrict указывает что обход выполняется в нестрогом режиме.
// При обходе по данным в случае несоответствия их типа будет выполнена // В случае несоответствия типа данных ожидаемому, по возможности данные
// попытка преобразования типа с использованием NonStrictConverter. // будут преобразованы в соответствии с ожидаемым типом.
func WalkNonStrict() WalkOption { func WalkNonStrict() WalkOption {
return func(opts *WalkOptions) { return func(opts *WalkOptions) {
opts.WalkNonStrict = true opts.WalkNonStrict = true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment