Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1486: dynamo/attributevalue allow json tag encoding/decoding #1520

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions feature/dynamodb/attributevalue/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func UnmarshalListOfMaps(l []map[string]types.AttributeValue, out interface{}) e
// DecoderOptions is a collection of options to configure how the decoder
// unmarshalls the value.
type DecoderOptions struct {
// States that the encoding/json struct tags should be supported.
// if a `dynamodbav` struct tag is also provided the encoding/json
// tag will be ignored.
//
// Enabled by default.
SupportJSONTags bool

// Support other custom struct tag keys, such as `yaml`, `json`, or `toml`.
// Note that values provided with a custom TagKey must also be supported
// by the (un)marshalers in this package.
Expand All @@ -136,7 +143,10 @@ type Decoder struct {
// NewDecoder creates a new Decoder with default configuration. Use
// the `opts` functional options to override the default configuration.
func NewDecoder(optFns ...func(*DecoderOptions)) *Decoder {
var options DecoderOptions
var options = DecoderOptions{
SupportJSONTags: true,
}

for _, fn := range optFns {
fn(&options)
}
Expand Down Expand Up @@ -509,7 +519,8 @@ func (d *Decoder) decodeMap(avMap map[string]types.AttributeValue, v reflect.Val
}
} else if v.Kind() == reflect.Struct {
fields := unionStructFields(v.Type(), structFieldOptions{
TagKey: d.options.TagKey,
TagKey: d.options.TagKey,
SupportJSONTags: true,
})
for k, av := range avMap {
if f, ok := fields.FieldByName(k); ok {
Expand Down
10 changes: 9 additions & 1 deletion feature/dynamodb/attributevalue/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ func MarshalList(in interface{}) ([]types.AttributeValue, error) {
// EncoderOptions is a collection of options shared between marshaling
// and unmarshaling
type EncoderOptions struct {
// States that the encoding/json struct tags should be supported.
// if a `dynamodbav` struct tag is also provided the encoding/json
// tag will be ignored.
//
// Enabled by default.
SupportJSONTags bool

// Support other custom struct tag keys, such as `yaml`, `json`, or `toml`.
// Note that values provided with a custom TagKey must also be supported
// by the (un)marshalers in this package.
Expand Down Expand Up @@ -241,7 +248,8 @@ type Encoder struct {
// the `opts` functional options to override the default configuration.
func NewEncoder(optFns ...func(*EncoderOptions)) *Encoder {
options := EncoderOptions{
NullEmptySets: true,
NullEmptySets: true,
SupportJSONTags: true,
}
for _, fn := range optFns {
fn(&options)
Expand Down
9 changes: 9 additions & 0 deletions feature/dynamodb/attributevalue/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ func buildField(pIdx []int, i int, sf reflect.StructField, fieldTag tag) field {
}

type structFieldOptions struct {
// States that the encoding/json struct tags should be supported.
// if a `dynamodbav` struct tag is also provided the encoding/json
// tag will be ignored.
//
// Enabled by default.
SupportJSONTags bool

// Support other custom struct tag keys, such as `yaml`, `json`, or `toml`.
// Note that values provided with a custom TagKey must also be supported
// by the (un)marshalers in this package.
Expand Down Expand Up @@ -107,6 +114,8 @@ func enumFields(t reflect.Type, opts structFieldOptions) []field {
// Because MarshalOptions.TagKey must be explicitly set.
if opts.TagKey != "" && fieldTag == (tag{}) {
fieldTag.parseStructTag(opts.TagKey, sf.Tag)
} else if opts.SupportJSONTags && fieldTag == (tag{}) {
fieldTag.parseStructTag("json", sf.Tag)
}

if fieldTag.Ignore {
Expand Down