Skip to content

Commit

Permalink
fix: remarshal List on MarshalDynamo
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberde committed Feb 23, 2021
1 parent 38a5ec9 commit 9c710af
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.3.2 // indirect
github.com/pelletier/go-toml v1.8.0 // indirect
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.19.0
github.com/spf13/afero v1.3.2 // indirect
github.com/spf13/cast v1.3.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ github.com/pelletier/go-toml v1.8.0 h1:Keo9qb7iRJs2voHvunFtuuYFsbWeOBh8/P9v/kVMF
github.com/pelletier/go-toml v1.8.0/go.mod h1:D6yutnOGMveHEPV7VQOuvI/gXY61bv+9bAOTRnLElKs=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
2 changes: 1 addition & 1 deletion pkg/db/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func Dump(cfg *config.DynamoDBDumpConfig) error {
continue
}

marshaled, err := marshalDynamo(jsonItem)
marshaled, err := MarshalDynamo(jsonItem)
if err != nil {
log.Err(err).Msg("failed to marshal dynamodb object")
continue
Expand Down
33 changes: 23 additions & 10 deletions pkg/db/util.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
package db

import (
"fmt"
"encoding/json"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)

// MarshalDynamoValue make flatten dynamodb.AttributeValue
func marshalDynamoValue(item interface{}) (interface{}, error) {
func MarshalDynamoValue(item interface{}) (interface{}, error) {
var result interface{}
for k, v := range item.(map[string]interface{}) {
if v == nil {
continue
}

switch k {
case "N", "BOOL", "S", "BS", "NULL", "NS", "SS":
result = v
break
case "L":
l := v.([]interface{})
var l []map[string]interface{}
bytes, err := json.Marshal(v)
if err != nil {
log.Err(err).Stack().Interface("item", item).Msg("[L]json.Marshal() failed")
continue
}

if err := json.Unmarshal(bytes, &l); err != nil {
log.Err(err).Stack().Interface("item", item).Msg("[L]json.Unmarshal() failed")
continue
}

r := make([]interface{}, 0, len(l))
for i := range l {
v, err := marshalDynamoValue(l[i])
v, err := MarshalDynamoValue(l[i])
if err != nil {
return nil, err
}
Expand All @@ -29,30 +42,30 @@ func marshalDynamoValue(item interface{}) (interface{}, error) {
result = r
break
case "M":
r, err := marshalDynamo(v)
r, err := MarshalDynamo(v)
if err != nil {
return nil, err
}
result = r
break
default:
return nil, fmt.Errorf("unsupported format")
return nil, errors.New("unsupported format")
}
}

return result, nil
}

// MarshalDynamo make flatten dynamodb.Item
func marshalDynamo(item interface{}) (interface{}, error) {
func MarshalDynamo(item interface{}) (interface{}, error) {
converted, ok := item.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid data to marshal. item=%v", item)
return nil, errors.Errorf("invalid data to marshal. item=%v", item)
}

result := make(map[string]interface{}, len(converted))
for k1, v1 := range converted {
v, err := marshalDynamoValue(v1)
v, err := MarshalDynamoValue(v1)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9c710af

Please sign in to comment.