Skip to content

dynamodb/attributevalue: allow registration of custom type marshal/unmarshal functions #2042

Closed as not planned
@ztison

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 and UnmarshalDynamoDBAttributeValue functions with dependency on AttributeValue 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

feature-requestA feature should be added or improved.p3This is a minor priority issuewontfixWe have determined that we will not resolve the issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions