Add Serializer Value method
This commit is contained in:
		
							parent
							
								
									bf85076f0e
								
							
						
					
					
						commit
						0b039497c4
					
				@ -478,6 +478,29 @@ func (field *Field) setupValuerAndSetter() {
 | 
				
			|||||||
		return fv, zero
 | 
							return fv, zero
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if field.Serializer != nil {
 | 
				
			||||||
 | 
							oldValuerOf := field.ValueOf
 | 
				
			||||||
 | 
							field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) {
 | 
				
			||||||
 | 
								value, zero := oldValuerOf(ctx, v)
 | 
				
			||||||
 | 
								if zero {
 | 
				
			||||||
 | 
									return value, zero
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								serializer, ok := value.(SerializerInterface)
 | 
				
			||||||
 | 
								if !ok {
 | 
				
			||||||
 | 
									serializer = field.Serializer
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								return Serializer{
 | 
				
			||||||
 | 
									Field:       field,
 | 
				
			||||||
 | 
									Interface:   serializer,
 | 
				
			||||||
 | 
									Destination: v,
 | 
				
			||||||
 | 
									Context:     ctx,
 | 
				
			||||||
 | 
									fieldValue:  value,
 | 
				
			||||||
 | 
								}, false
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// ReflectValueOf returns field's reflect value
 | 
						// ReflectValueOf returns field's reflect value
 | 
				
			||||||
	field.ReflectValueOf = func(ctx context.Context, v reflect.Value) reflect.Value {
 | 
						field.ReflectValueOf = func(ctx context.Context, v reflect.Value) reflect.Value {
 | 
				
			||||||
		v = reflect.Indirect(v)
 | 
							v = reflect.Indirect(v)
 | 
				
			||||||
 | 
				
			|||||||
@ -29,6 +29,7 @@ type Serializer struct {
 | 
				
			|||||||
	Destination reflect.Value
 | 
						Destination reflect.Value
 | 
				
			||||||
	Context     context.Context
 | 
						Context     context.Context
 | 
				
			||||||
	value       interface{}
 | 
						value       interface{}
 | 
				
			||||||
 | 
						fieldValue  interface{}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Scan implements sql.Scanner interface
 | 
					// Scan implements sql.Scanner interface
 | 
				
			||||||
@ -39,13 +40,13 @@ func (s *Serializer) Scan(value interface{}) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Value implements driver.Valuer interface
 | 
					// Value implements driver.Valuer interface
 | 
				
			||||||
func (s Serializer) Value() (driver.Value, error) {
 | 
					func (s Serializer) Value() (driver.Value, error) {
 | 
				
			||||||
	return s.Interface.Value(s.Context, s.Field, s.Destination)
 | 
						return s.Interface.Value(s.Context, s.Field, s.Destination, s.fieldValue)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// SerializerInterface serializer interface
 | 
					// SerializerInterface serializer interface
 | 
				
			||||||
type SerializerInterface interface {
 | 
					type SerializerInterface interface {
 | 
				
			||||||
	Scan(ctx context.Context, field *Field, dst reflect.Value, dbValue interface{}) error
 | 
						Scan(ctx context.Context, field *Field, dst reflect.Value, dbValue interface{}) error
 | 
				
			||||||
	Value(ctx context.Context, field *Field, dst reflect.Value) (interface{}, error)
 | 
						Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (interface{}, error)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// JSONSerializer json serializer
 | 
					// JSONSerializer json serializer
 | 
				
			||||||
@ -75,9 +76,8 @@ func (JSONSerializer) Scan(ctx context.Context, field *Field, dst reflect.Value,
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Value implements serializer interface
 | 
					// Value implements serializer interface
 | 
				
			||||||
func (JSONSerializer) Value(ctx context.Context, field *Field, dst reflect.Value) (interface{}, error) {
 | 
					func (JSONSerializer) Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) {
 | 
				
			||||||
	fv, _ := field.ValueOf(ctx, dst)
 | 
						return fieldValue, nil
 | 
				
			||||||
	return fv, nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// CreateClausesInterface create clauses interface
 | 
					// CreateClausesInterface create clauses interface
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user