| 1 |
package schema |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"database/sql" |
| 7 |
"database/sql/driver" |
| 8 |
"encoding/gob" |
| 9 |
"encoding/json" |
| 10 |
"fmt" |
| 11 |
"math" |
| 12 |
"reflect" |
| 13 |
"strings" |
| 14 |
"sync" |
| 15 |
"time" |
| 16 |
) |
| 17 |
|
| 18 |
var serializerMap = sync.Map{} |
| 19 |
|
| 20 |
// RegisterSerializer register serializer |
| 21 |
func RegisterSerializer(name string, serializer SerializerInterface) { |
| 22 |
serializerMap.Store(strings.ToLower(name), serializer) |
| 23 |
} |
| 24 |
|
| 25 |
// GetSerializer get serializer |
| 26 |
func GetSerializer(name string) (serializer SerializerInterface, ok bool) { |
| 27 |
v, ok := serializerMap.Load(strings.ToLower(name)) |
| 28 |
if ok { |
| 29 |
serializer, ok = v.(SerializerInterface) |
| 30 |
} |
| 31 |
return serializer, ok |
| 32 |
} |
| 33 |
|
| 34 |
func init() { |
| 35 |
RegisterSerializer("json", JSONSerializer{}) |
| 36 |
RegisterSerializer("unixtime", UnixSecondSerializer{}) |
| 37 |
RegisterSerializer("gob", GobSerializer{}) |
| 38 |
} |
| 39 |
|
| 40 |
// Serializer field value serializer |
| 41 |
type serializer struct { |
| 42 |
Field *Field |
| 43 |
Serializer SerializerInterface |
| 44 |
SerializeValuer SerializerValuerInterface |
| 45 |
Destination reflect.Value |
| 46 |
Context context.Context |
| 47 |
value interface{} |
| 48 |
fieldValue interface{} |
| 49 |
} |
| 50 |
|
| 51 |
// Scan implements sql.Scanner interface |
| 52 |
func (s *serializer) Scan(value interface{}) error { |
| 53 |
s.value = value |
| 54 |
return nil |
| 55 |
} |
| 56 |
|
| 57 |
// Value implements driver.Valuer interface |
| 58 |
func (s serializer) Value() (driver.Value, error) { |
| 59 |
return s.SerializeValuer.Value(s.Context, s.Field, s.Destination, s.fieldValue) |
| 60 |
} |
| 61 |
|
| 62 |
// SerializerInterface serializer interface |
| 63 |
type SerializerInterface interface { |
| 64 |
Scan(ctx context.Context, field *Field, dst reflect.Value, dbValue interface{}) error |
| 65 |
SerializerValuerInterface |
| 66 |
} |
| 67 |
|
| 68 |
// SerializerValuerInterface serializer valuer interface |
| 69 |
type SerializerValuerInterface interface { |
| 70 |
Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) |
| 71 |
} |
| 72 |
|
| 73 |
// JSONSerializer json serializer |
| 74 |
type JSONSerializer struct{} |
| 75 |
|
| 76 |
// Scan implements serializer interface |
| 77 |
func (JSONSerializer) Scan(ctx context.Context, field *Field, dst reflect.Value, dbValue interface{}) (err error) { |
| 78 |
fieldValue := reflect.New(field.FieldType) |
| 79 |
|
| 80 |
if dbValue != nil { |
| 81 |
var bytes []byte |
| 82 |
switch v := dbValue.(type) { |
| 83 |
case []byte: |
| 84 |
bytes = v |
| 85 |
case string: |
| 86 |
bytes = []byte(v) |
| 87 |
default: |
| 88 |
bytes, err = json.Marshal(v) |
| 89 |
if err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if len(bytes) > 0 { |
| 95 |
err = json.Unmarshal(bytes, fieldValue.Interface()) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
field.ReflectValueOf(ctx, dst).Set(fieldValue.Elem()) |
| 100 |
return |
| 101 |
} |
| 102 |
|
| 103 |
// Value implements serializer interface |
| 104 |
func (JSONSerializer) Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) { |
| 105 |
result, err := json.Marshal(fieldValue) |
| 106 |
if string(result) == "null" { |
| 107 |
if field.TagSettings["NOT NULL"] != "" { |
| 108 |
return "", nil |
| 109 |
} |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
return string(result), err |
| 113 |
} |
| 114 |
|
| 115 |
// UnixSecondSerializer json serializer |
| 116 |
type UnixSecondSerializer struct{} |
| 117 |
|
| 118 |
// Scan implements serializer interface |
| 119 |
func (UnixSecondSerializer) Scan(ctx context.Context, field *Field, dst reflect.Value, dbValue interface{}) (err error) { |
| 120 |
t := sql.NullTime{} |
| 121 |
if err = t.Scan(dbValue); err == nil && t.Valid { |
| 122 |
err = field.Set(ctx, dst, t.Time.Unix()) |
| 123 |
} |
| 124 |
|
| 125 |
return |
| 126 |
} |
| 127 |
|
| 128 |
// Value implements serializer interface |
| 129 |
func (UnixSecondSerializer) Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (result interface{}, err error) { |
| 130 |
rv := reflect.ValueOf(fieldValue) |
| 131 |
switch fieldValue.(type) { |
| 132 |
case int, int8, int16, int32, int64: |
| 133 |
result = time.Unix(rv.Int(), 0).UTC() |
| 134 |
case uint, uint8, uint16, uint32, uint64: |
| 135 |
if uv := rv.Uint(); uv > math.MaxInt64 { |
| 136 |
err = fmt.Errorf("integer overflow conversion uint64(%d) -> int64", uv) |
| 137 |
} else { |
| 138 |
result = time.Unix(int64(uv), 0).UTC() //nolint:gosec |
| 139 |
} |
| 140 |
case *int, *int8, *int16, *int32, *int64: |
| 141 |
if rv.IsZero() { |
| 142 |
return nil, nil |
| 143 |
} |
| 144 |
result = time.Unix(rv.Elem().Int(), 0).UTC() |
| 145 |
case *uint, *uint8, *uint16, *uint32, *uint64: |
| 146 |
if rv.IsZero() { |
| 147 |
return nil, nil |
| 148 |
} |
| 149 |
if uv := rv.Elem().Uint(); uv > math.MaxInt64 { |
| 150 |
err = fmt.Errorf("integer overflow conversion uint64(%d) -> int64", uv) |
| 151 |
} else { |
| 152 |
result = time.Unix(int64(uv), 0).UTC() //nolint:gosec |
| 153 |
} |
| 154 |
default: |
| 155 |
err = fmt.Errorf("invalid field type %#v for UnixSecondSerializer, only int, uint supported", fieldValue) |
| 156 |
} |
| 157 |
return |
| 158 |
} |
| 159 |
|
| 160 |
// GobSerializer gob serializer |
| 161 |
type GobSerializer struct{} |
| 162 |
|
| 163 |
// Scan implements serializer interface |
| 164 |
func (GobSerializer) Scan(ctx context.Context, field *Field, dst reflect.Value, dbValue interface{}) (err error) { |
| 165 |
fieldValue := reflect.New(field.FieldType) |
| 166 |
|
| 167 |
if dbValue != nil { |
| 168 |
var bytesValue []byte |
| 169 |
switch v := dbValue.(type) { |
| 170 |
case []byte: |
| 171 |
bytesValue = v |
| 172 |
default: |
| 173 |
return fmt.Errorf("failed to unmarshal gob value: %#v", dbValue) |
| 174 |
} |
| 175 |
if len(bytesValue) > 0 { |
| 176 |
decoder := gob.NewDecoder(bytes.NewBuffer(bytesValue)) |
| 177 |
err = decoder.Decode(fieldValue.Interface()) |
| 178 |
} |
| 179 |
} |
| 180 |
field.ReflectValueOf(ctx, dst).Set(fieldValue.Elem()) |
| 181 |
return |
| 182 |
} |
| 183 |
|
| 184 |
// Value implements serializer interface |
| 185 |
func (GobSerializer) Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) { |
| 186 |
buf := new(bytes.Buffer) |
| 187 |
err := gob.NewEncoder(buf).Encode(fieldValue) |
| 188 |
return buf.Bytes(), err |
| 189 |
} |