aws-sdk-go-v2 icon indicating copy to clipboard operation
aws-sdk-go-v2 copied to clipboard

Custom tags should have higher precendence then the json tag.

Open knuppe opened this issue 3 years ago • 0 comments

Describe the bug

The attributevalue.UnmarshalMapWithOptions ignores custom tag when a json:"-" is specified in the struct.

Expected Behavior

A custom tag should have higher precedence than the json tag during the unmarshall, the output struct should have the actual attribute value read from the DynamoDB.

Current Behavior

The attribute is ignored.

Reproduction Steps

Example:

package main

type MyStruct struct {
	Pk           string `json:"id" dynamodb:"pk"`
	OmittedValue string `json:"-" dynamodb:"value"` // this value is omitted in the unmarshall
}

func main() {

	cfg, err := config.LoadDefaultConfig(context.Background())
	if err != nil {
		panic(err)
	}

	ddb := dynamodb.NewFromConfig(cfg)

	_, err = ddb.PutItem(context.Background(), &dynamodb.PutItemInput{
		TableName: aws.String("test"),
		Item: map[string]types.AttributeValue{
			"pk":    &types.AttributeValueMemberS{Value: "test"},
			"value": &types.AttributeValueMemberS{Value: "my value"},
		},
	})
	if err != nil {
		panic(err)
	}

	output, err := ddb.GetItem(context.Background(), &dynamodb.GetItemInput{
		TableName: aws.String("test"),
		Key: map[string]types.AttributeValue{
			"pk": &types.AttributeValueMemberS{Value: "test"},
		},
	})
	if err != nil {
		panic(err)
	}

	var out *MyStruct
	err = attributevalue.UnmarshalMapWithOptions(output.Item, &out, func(options *attributevalue.DecoderOptions) {
		options.TagKey = "dynamodb" // this tag should have higher precedence than the json tag.
	})
	if err != nil {
		panic(err)
	}

	if out.OmittedValue != "my value" {
		panic("this should not happen?")
	}
}

Possible Solution

No response

Additional Information/Context

No response

AWS Go SDK V2 Module Versions Used

github.com/aws/aws-sdk-go-v2/config v1.15.14 github.com/aws/aws-sdk-go-v2/service/dynamodb v1.15.9 github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.9.6

github.com/aws/aws-sdk-go-v2 v1.16.7 // indirect github.com/aws/aws-sdk-go-v2/credentials v1.12.9 // indirect github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.8 // indirect github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.14 // indirect github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.8 // indirect github.com/aws/aws-sdk-go-v2/internal/ini v1.3.15 // indirect github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.13.9 // indirect github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.3 // indirect github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.7.8 // indirect github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.8 // indirect github.com/aws/aws-sdk-go-v2/service/sso v1.11.12 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.16.9 // indirect

Compiler and Version used

1.18.3

Operating System and version

Linux 5.4.0-122-generic

knuppe avatar Jul 16 '22 13:07 knuppe