Skip to content

Commit

Permalink
Merge pull request #8 from scrambledeggs/Release/v1.1.0
Browse files Browse the repository at this point in the history
Release/v1.1.0
  • Loading branch information
Ashllives authored Oct 3, 2019
2 parents 3268ca3 + 1a032f1 commit 1488a26
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,33 @@ Usage:
# [Module] photo/
Generates Booky's Image URL. Module is named `photo` as to not override go's `image` package

Usage: FormatImageURL(ID int, assetType string, filename string, extra ...string)
Usage:
+ FormatImageURL(ID int, assetType string, filename string, extra ...string)
- ID - ID of entity.
- assetType - Type of entity (e.g. 'offers' or 'brands')
- filename - Image filename
- extra - Accepts up to two optional parameters. Sets imageSize(default:`original`) and imageType(default:`photo`).
- Sample Output: "https://assets1.phonebooky.com/brands/photos/000/000/020/original/sample.jpg"

# [Module] marshalling/
Customized MarshalMap for dynamodb attributes. This will keep empty values _(empty strings, zeros, empty structs/objects)_ to persist them in dynamodb tables.

Usage:
+ CustomMarshalMap(in interface{})
- in - interface{} of value struct such as `{Renamed Brand 1 0xc0000874b0 inactive 0xc0000908c0}`

- Sample Output of type `map[string]*dynamodb.AttributeValue`:

```go
map[brand_name:{
S: "Renamed Brand 1"
} brand_status:{
S: "inactive"
} description:{
NULL: true
} offer:{
NULL: true
} offer_limit:{
N: "0"
}]
```
49 changes: 49 additions & 0 deletions marshalling/custommarshal.go
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
}
38 changes: 38 additions & 0 deletions marshalling/custommarshal_test.go
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)
}
8 changes: 8 additions & 0 deletions marshalling/go.mod
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
)
14 changes: 14 additions & 0 deletions marshalling/go.sum
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=

0 comments on commit 1488a26

Please sign in to comment.