bytes.go 2.1 KB
Newer Older
Hamdi Allam's avatar
Hamdi Allam committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
package serializers

import (
	"context"
	"fmt"
	"reflect"

	"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 {
21
	if dbValue == nil {
Hamdi Allam's avatar
Hamdi Allam committed
22
		return nil
Hamdi Allam's avatar
Hamdi Allam committed
23 24 25 26 27 28 29 30 31
	}

	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
32
		return fmt.Errorf("failed to decode database value: %w", err)
Hamdi Allam's avatar
Hamdi Allam committed
33 34 35
	}

	fieldValue := reflect.New(field.FieldType)
Hamdi Allam's avatar
Hamdi Allam committed
36 37 38 39
	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
40
	if field.FieldType.Kind() == reflect.Pointer {
Hamdi Allam's avatar
Hamdi Allam committed
41 42 43 44 45
		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
46 47
		// 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
48 49
		nestedField.Set(reflect.New(field.FieldType.Elem()))
		fieldInterface = nestedField.Interface()
Hamdi Allam's avatar
Hamdi Allam committed
50 51 52 53 54 55 56 57 58 59 60 61 62
	}

	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
63 64 65 66
	if fieldValue == nil || (field.FieldType.Kind() == reflect.Pointer && reflect.ValueOf(fieldValue).IsNil()) {
		return nil, nil
	}

Hamdi Allam's avatar
Hamdi Allam committed
67 68 69 70 71
	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
72
	hexStr := hexutil.Encode(fieldBytes.Bytes())
Hamdi Allam's avatar
Hamdi Allam committed
73 74
	return hexStr, nil
}