-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic get/put operations to dynamo service
- Loading branch information
1 parent
2d72c4b
commit 955c31f
Showing
5 changed files
with
185 additions
and
21 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package dynamo | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
) | ||
|
||
type Marshaler interface { | ||
MarshalDynamo() (map[string]types.AttributeValue, error) | ||
} | ||
|
||
type Unmarshaler interface { | ||
UnmarshalDynamo(map[string]types.AttributeValue) error | ||
} | ||
|
||
func marshal(v any) (map[string]types.AttributeValue, error) { | ||
marshaler, ok := v.(Marshaler) | ||
if ok { | ||
return marshaler.MarshalDynamo() | ||
} | ||
|
||
return attributevalue.MarshalMap(v) | ||
} | ||
|
||
func unmarshal(m map[string]types.AttributeValue, v any) error { | ||
unmarshaler, ok := v.(Unmarshaler) | ||
if ok { | ||
return unmarshaler.UnmarshalDynamo(m) | ||
} | ||
|
||
return attributevalue.UnmarshalMap(m, v) | ||
} | ||
|
||
func MarshalJSONGZ(v any) ([]byte, error) { | ||
buf := &bytes.Buffer{} | ||
w := gzip.NewWriter(buf) | ||
|
||
if err := json.NewEncoder(w).Encode(v); err != nil { | ||
return nil, fmt.Errorf("error encoding value as json+gzip: %w", err) | ||
} | ||
|
||
w.Close() | ||
|
||
return buf.Bytes(), nil | ||
} | ||
|
||
func UnmarshalJSONGZ(d []byte, v any) error { | ||
r, err := gzip.NewReader(bytes.NewReader(d)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := json.NewDecoder(r).Decode(v); err != nil { | ||
return fmt.Errorf("error decoding value from json+gzip: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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
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