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

encoder.encodeMap() should not encodeNull() when map length is zero #195

Closed
wizardishungry opened this issue Jun 29, 2018 · 5 comments
Closed
Labels
feature-request A feature should be added or improved. v1-sync

Comments

@wizardishungry
Copy link

wizardishungry commented Jun 29, 2018

Version of AWS SDK for Go?

2.0.0-preview.4

Version of Go (go version)?

go version go1.10.3 darwin/amd64

What issue did you see?

My Dynamo records are being created with NULL entries instead of empty maps.

Steps to reproduce

type Message struct {
	ID            string `json:"id"`
	Likes   map[int]string `json:"likes"`

}
// Create persist a message
func (d *MessageDao) Create(message *model.Message,) (*model.Message, error) {

	av, err := dynamodbattribute.MarshalMap(message)
	if err != nil {
		return nil, err
	}
	input := &dynamodb.PutItemInput{
		Item:      av,
		TableName: aws.String(d.tableName),
	}

	req := d.DynamoDB.PutItemRequest(input)
	_, err = req.Send()

	if err != nil {
		return nil, err
	}
	return message, nil
}

func (e *Encoder) encodeMap(av *dynamodb.AttributeValue, v reflect.Value, fieldTag tag) error {
av.M = map[string]dynamodb.AttributeValue{}
for _, key := range v.MapKeys() {
keyName := fmt.Sprint(key.Interface())
if keyName == "" {
return &InvalidMarshalError{msg: "map key cannot be empty"}
}
elemVal := v.MapIndex(key)
elem := dynamodb.AttributeValue{}
err := e.encode(&elem, elemVal, tag{})
skip, err := keepOrOmitEmpty(fieldTag.OmitEmptyElem, elem, err)
if err != nil {
return err
} else if skip {
continue
}
av.M[keyName] = elem
}
if len(av.M) == 0 {
encodeNull(av)
}
return nil
}

@wizardishungry
Copy link
Author

see aws/aws-sdk-go#682

@xibz
Copy link
Contributor

xibz commented Jun 29, 2018

Hello @wizardishungry, thank you for reaching out to us. We've received this same issue in v1, please see here. The workaround will also work for v2. I am going to go ahead and mark this as a feature request. If you have any more questions or issues with the workaround please let us know.

@wizardishungry
Copy link
Author

wizardishungry commented Jun 29, 2018

For those arriving via search, etc. – a workaround for v2:

av, err := dynamodbattribute.MarshalMap(message)
if err != nil {
  return nil, err
}
emptyMap := make(map[string]dynamodb.AttributeValue)
av["some map"] = dynamodb.AttributeValue{M: emptyMap}

@xibz xibz added the feature-request A feature should be added or improved. label Jun 29, 2018
jasdel pushed a commit to jasdel/aws-sdk-go that referenced this issue Jan 21, 2019
…s empty map

Adds support for marshaling nil maps and slices as empty maps and
lists instead of NULL DynamoDB AttributeValue.

Fix aws#682
Related: aws/aws-sdk-go-v2#195
Original PR: aws#2105
@jasdel jasdel added the v1-sync label Mar 4, 2019
@jasdel
Copy link
Contributor

jasdel commented Mar 4, 2019

This is related to the v1 SDK work, aws/aws-sdk-go#2419

@jasdel
Copy link
Contributor

jasdel commented May 20, 2020

This feature was implemented in #401 and released in v0.13.0

@jasdel jasdel closed this as completed May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved. v1-sync
Projects
None yet
Development

No branches or pull requests

3 participants