bytes.go 2.13 KB
Newer Older
Hamdi Allam's avatar
Hamdi Allam committed
1 2 3 4 5 6
package serializers

import (
	"context"
	"fmt"
	"reflect"
Hamdi Allam's avatar
Hamdi Allam committed
7
	"strings"
Hamdi Allam's avatar
Hamdi Allam committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21

	"github.com/ethereum/go-ethereum/common/hexutil"
	"gorm.io/gorm/schema"
)

type BytesSerializer struct{}
type BytesInterface interface{ Bytes() []byte }
type SetBytesInterface interface{ SetBytes([]byte) }

func init() {
	schema.RegisterSerializer("bytes", BytesSerializer{})
}

func (BytesSerializer) Scan(ctx context.Context, field *schema.Field, dst reflect.Value, dbValue interface{}) error {
22
	if dbValue == nil {
Hamdi Allam's avatar
Hamdi Allam committed
23
		return nil
Hamdi Allam's avatar
Hamdi Allam committed
24 25 26 27 28 29 30 31 32
	}

	hexStr, ok := dbValue.(string)
	if !ok {
		return fmt.Errorf("expected hex string as the database value: %T", dbValue)
	}

	b, err := hexutil.Decode(hexStr)
	if err != nil {
Hamdi Allam's avatar
Hamdi Allam committed
33
		return fmt.Errorf("failed to decode database value: %w", err)
Hamdi Allam's avatar
Hamdi Allam committed
34 35 36
	}

	fieldValue := reflect.New(field.FieldType)
Hamdi Allam's avatar
Hamdi Allam committed
37 38 39 40
	fieldInterface := fieldValue.Interface()

	// Detect if we're deserializing into a pointer. If so, we'll need to
	// also allocate memory to where the allocated pointer should point to
Hamdi Allam's avatar
Hamdi Allam committed
41
	if field.FieldType.Kind() == reflect.Pointer {
Hamdi Allam's avatar
Hamdi Allam committed
42 43 44 45 46
		nestedField := fieldValue.Elem()
		if nestedField.Elem().Kind() == reflect.Pointer {
			return fmt.Errorf("double pointers are the max depth supported: %T", fieldValue)
		}

Hamdi Allam's avatar
Hamdi Allam committed
47 48
		// We'll want to call `SetBytes` on the pointer to
		// the allocated memory and not the double pointer
Hamdi Allam's avatar
Hamdi Allam committed
49 50
		nestedField.Set(reflect.New(field.FieldType.Elem()))
		fieldInterface = nestedField.Interface()
Hamdi Allam's avatar
Hamdi Allam committed
51 52 53 54 55 56 57 58 59 60 61 62 63
	}

	fieldSetBytes, ok := fieldInterface.(SetBytesInterface)
	if !ok {
		return fmt.Errorf("field does not satisfy the `SetBytes([]byte)` interface: %T", fieldInterface)
	}

	fieldSetBytes.SetBytes(b)
	field.ReflectValueOf(ctx, dst).Set(fieldValue.Elem())
	return nil
}

func (BytesSerializer) Value(ctx context.Context, field *schema.Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) {
Hamdi Allam's avatar
Hamdi Allam committed
64 65 66 67
	if fieldValue == nil || (field.FieldType.Kind() == reflect.Pointer && reflect.ValueOf(fieldValue).IsNil()) {
		return nil, nil
	}

Hamdi Allam's avatar
Hamdi Allam committed
68 69 70 71 72
	fieldBytes, ok := fieldValue.(BytesInterface)
	if !ok {
		return nil, fmt.Errorf("field does not satisfy the `Bytes() []byte` interface")
	}

Hamdi Allam's avatar
Hamdi Allam committed
73
	hexStr := hexutil.Encode(fieldBytes.Bytes())
Hamdi Allam's avatar
Hamdi Allam committed
74
	return strings.ToLower(hexStr), nil
Hamdi Allam's avatar
Hamdi Allam committed
75
}