Skip to content
Snippets Groups Projects
Select Git revision
  • cf0f274a15ec93ae2e60d8cbc66948bc7f664cfa
  • master default protected
  • refactor/PRXS-3053-Files
  • feature/PRXS-3143-3235-ReferenceOptions
  • feature/PRXS-3421-ImplementNewRefAPI
  • feature/PRXS-3143-LimitReferenceFields
  • feature/PRXS-3234-FeaturePruneIdents
  • feature/3149-LocaleCodeAsID-Feature
  • feature/PRXS-3383-CollectionsRankSortAPI
  • PRXS-3421-RecursiveReferences
  • feature/PRXS-3383-CollectionsSort
  • feature/3109-SerializeFeature
  • release/0.33
  • feature/3109-RecoverySchema
  • feature/3109-feature
  • fix/PRXS-3369-ValidateFields
  • refactor/PRXS-3306-MovePkgGroup1
  • refactor/6-pkg-refactor-expr
  • fix/PRXS-3360-TemplateBuilderPatch
  • feature/3293-MongoV2
  • feature/3272-GoVersionUp
  • v0.33.1
  • v0.32.0
  • v0.31.1
  • v0.31.0
  • v0.30.0
  • v0.29.0
  • v0.28.0
  • v0.27.0-alpha.1+16
  • v0.27.0-alpha.1+15
  • v0.27.0-alpha.1+14
  • v0.27.0-alpha.1+13
  • v0.27.0-alpha.1+12
  • v0.27.0-alpha.1+11
  • v0.27.0-alpha.1+10
  • v0.27.0-alpha.1+9
  • v0.27.0-alpha.1+8
  • v0.27.0-alpha.1+7
  • v0.27.0-alpha.1+6
  • v0.27.0-alpha.1+5
  • v0.27.0-alpha.1+4
41 results

mongo.go

Blame
  • encode.go 2.16 KiB
    package field
    
    import (
    	"context"
    
    	"git.perx.ru/perxis/perxis-go/pkg/errors"
    )
    
    type Decoder interface {
    	Decode(ctx context.Context, field *Field, v interface{}) (interface{}, error)
    }
    
    type Encoder interface {
    	Encode(ctx context.Context, field *Field, v interface{}) (interface{}, error)
    }
    
    // NonStrictConverter определяет метод для преобразования данных в нестрогом режиме.
    type NonStrictConverter interface {
    	NonStrictConverter(ctx context.Context, field *Field, v interface{}) interface{}
    }
    
    type EncodeOptions struct {
    	NonStrictMode bool
    }
    
    type EncodeOption func(opts *EncodeOptions)
    
    // NonStrictMode указывает, что декодирование необходимо провести в нестрогом режиме.
    func NonStrictMode() EncodeOption {
    	return func(opts *EncodeOptions) {
    		opts.NonStrictMode = true
    	}
    }
    
    func NewEncodeOptions(opt ...EncodeOption) *EncodeOptions {
    	opts := &EncodeOptions{}
    	for _, o := range opt {
    		o(opts)
    	}
    	return opts
    }
    
    func Decode(ctx context.Context, w Walker, v interface{}, opts ...EncodeOption) (interface{}, error) {
    	opt := NewEncodeOptions(opts...)
    
    	val, _, err := w.Walk(ctx, v, func(ctx context.Context, f *Field, v interface{}) (res WalkFuncResult, err error) {
    		if opt.NonStrictMode {
    			if converter, ok := f.GetType().(NonStrictConverter); ok {
    				v = converter.NonStrictConverter(ctx, f, v)
    			}
    		}
    
    		if decoder, ok := f.GetType().(Decoder); ok {
    			if v, err = decoder.Decode(ctx, f, v); err != nil {
    				return
    			}
    			res.Value = v
    			res.Changed = true
    			return
    		}
    		res.Value = v
    		return
    	})
    
    	if err != nil {
    		return nil, errors.Wrap(err, "decode error")
    	}
    
    	return val, nil
    }
    
    func Encode(ctx context.Context, w Walker, v interface{}) (interface{}, error) {
    	val, _, err := w.Walk(ctx, v, func(ctx context.Context, f *Field, v interface{}) (res WalkFuncResult, err error) {
    		if encode, ok := f.GetType().(Encoder); ok {
    			if v, err = encode.Encode(ctx, f, v); err != nil {
    				return
    			}
    			res.Value = v
    			res.Changed = true
    			return
    		}
    		res.Value = v
    		return
    	})
    	if err != nil {
    		return nil, errors.Wrap(err, "encode error")
    	}
    	return val, nil
    
    }