-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from scrambledeggs/Release/v1.1.0
Release/v1.1.0
- Loading branch information
Showing
5 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package marshalling | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | ||
) | ||
|
||
// CustomMarshalMap is a custom marshal func which marshals Go value to AttributeValues | ||
func CustomMarshalMap(in interface{}) (map[string]*dynamodb.AttributeValue, error) { | ||
av := dynamodbattribute.NewEncoder() | ||
av.NullEmptyString = false // keep empty strings | ||
|
||
// convert interface{} to map[string]*dynamodb.AttributeValue | ||
aav, err := av.Encode(in) | ||
if err != nil || av == nil || aav.M == nil { | ||
return map[string]*dynamodb.AttributeValue{}, err | ||
} | ||
|
||
// loop over map[string]*dynamodb.AttributeValue | ||
for key, elem := range aav.M { | ||
// convert elem to Go value from *dynamodb.AttributeValue | ||
var i interface{} | ||
_ = dynamodbattribute.Unmarshal(elem, &i) | ||
|
||
// change go value to reflect.Value for null checking | ||
r := reflect.ValueOf(i) | ||
if emptyValue(r) { | ||
delete(aav.M, key) | ||
|
||
// convert back to *dynamodb.AttributeValue | ||
t := true | ||
aav.M[key] = &dynamodb.AttributeValue{NULL: &t} | ||
} | ||
} | ||
|
||
return aav.M, nil | ||
} | ||
|
||
func emptyValue(v reflect.Value) bool { | ||
switch v.Kind() { | ||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String: | ||
return v.Len() == 0 | ||
case reflect.Interface, reflect.Ptr: | ||
return v.IsNil() | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package marshalling | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type Offer struct { | ||
Title string `json:"title,omitempty"` | ||
} | ||
|
||
type Brand struct { | ||
Name string `json:"brand_name,omitempty"` | ||
Description *string `json:"description,omitempty"` | ||
Kind string `json:"kind,omitempty"` | ||
Status string `json:"brand_status,omitempty"` | ||
Limit *int `json:"offer_limit,omitempty"` | ||
Offer *Offer `json:"offer"` | ||
} | ||
|
||
func TestCustomMarshalMap(t *testing.T) { | ||
description := "" | ||
zero := 0 | ||
brand := Brand{ | ||
Name: "Renamed Brand 1", | ||
Status: "inactive", | ||
Description: &description, | ||
Limit: &zero, | ||
Offer: &Offer{}, | ||
} | ||
|
||
res, err := CustomMarshalMap(brand) | ||
assert.Equal(t, true, *res["description"].NULL) | ||
assert.Equal(t, "0", *res["offer_limit"].N) | ||
assert.Equal(t, true, *res["offer"].NULL) | ||
assert.Nil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module scrambledeggs/booky-go-common/marshalling | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go v1.25.4 | ||
github.com/stretchr/testify v1.4.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
github.com/aws/aws-sdk-go v1.25.4 h1:exwxtR517g6OKm2rtAD5EANtjbzmnjEAco189zy/Uhc= | ||
github.com/aws/aws-sdk-go v1.25.4/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= | ||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= | ||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |