Description
Describe the feature
As a DynamoDB user, I want to be able to register custom marshaler/unmarshaler functions.
Use Case
-
I want to have common API types not coupled with the repository layer. So I don't want to implement
MarshalDynamoDBAttributeValue
andUnmarshalDynamoDBAttributeValue
functions with dependency onAttributeValue
in my high-level code. -
I want to implement custom marshaler/unmarshaler for types that I don't own. It can be required because of data masking or to omit empty values.
Proposed Solution
func (e *Encoder) RegisterMarshaller(t reflect.Type, f func(interface{}) (types.AttributeValue, error)) error
func (d *Decoder) RegisterUnmarshaller(t reflect.Type, f func(types.AttributeValue) (interface{}, error)) error
type Foo string
func StringMarshaller(value interface{}) (types.AttributeValue, error) {
if foo, ok := value.(Foo); ok {
return &types.AttributeValueMemberS{Value: string(foo)}, nil
}
return nil, fmt.Errorf("type error")
}
var foo = "foo"
enc.RegisterMarshaller(reflect.TypeOf(foo), StringMarshaller)
Other Information
Based on customer marshaler/unmarshaler for json
Rejected Alternatives
API without exposing reflect:
func (e *Encoder) RegisterFunc(f interface{})
func (d *Decoder) RegisterFunc(f interface{})
API based on generics:
func (e *Encoder) Register[T any](f func(T) (types.AttributeValue, error)) { ... }
func (d *Decoder) Register[T any](f func(types.AttributeValue) (T, error)) { ... }
Acknowledgements
- I may be able to implement this feature request
- This feature might incur a breaking change
AWS Go SDK V2 Module Versions Used
github.com/aws/aws-sdk-go-v2/feature/dynamodb/[email protected]
github.com/aws/aws-sdk-go-v2/feature/dynamodb/[email protected]
github.com/aws/aws-sdk-go-v2/service/[email protected]
github.com/aws/aws-sdk-go-v2/service/[email protected]
Go version used
go version go1.19.5 darwin/arm64
Activity