ddbmarshal

package module
v0.0.0-...-2368ed5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2022 License: BSD-3-Clause Imports: 8 Imported by: 0

README

ddb-marshal

Marshal/Unmarshal AWS Dynamo DB entries to GoLang structs

Usage

1. Annotate struct with ddb tag

type Entry struct {
    Name string `ddb:"name,required"`
    Value uint64 `ddb:"value,required"`
    Timestamp time.Time `ddb:"ts"`
    Items map[string]string `ddb:"items"`
}

Use public names (capitalized names) for the fields

2. Create Marshaller

marshaller := NewMarshaller()

3. Reading: Unmarshal results of ddb read API

if output, err := dynamodb.GetItem(input); err == nil {
    var entry Entry
    err = marshaller.Unmarshal(&entry, output.Item)
}
3.1 Get unmarshalled fields

These can be:

  1. Checked to be empty, or
  2. Preserved to persist on consequent update of the entry
  3. Passed to unmarshaller again with different data structure for data of "specialization" of the entry
unmarshalled, err := marshaller.GetUnmarshalledFields(&entry, output.Item)

3.3 Marshal structure for insert/update operation

var input dynamodb.PutItemInput
input.Item, _ = marshaller.Marshal(&entry)
for k, v := range unmarshalled {
    input.Item[k] = v
}

Field tags

Minimal support:

  1. comma-separated
  2. first element is name, should follow DDB requirements
  3. other entries may be any
  4. if one of them is "required", there is minimal validation on the value to be present during unmarshal
  5. future extensions are possible, for example HashKet/RangeKey specifications, GSI/LSI specifications

Field types support

Many types are supported, but far from covering any variant

Supported:

  1. Primitive types (string, ints, floats, boolean)
  2. time.Time as unix time (numeric) - specifically for TTL field support
  3. Arrays of the primitives and time.Time
  4. Byte arrays
  5. Arrays of byte arrays
  6. Maps with string as key and primitives or time.Time as a value

BUGS

  1. No default behavior (required/optional)
  2. No transparent encryption support

TODO

  1. Separate set of classes to create table, GSI, LSI based on the tags
  2. Separate set of classes to query, scan, and update table

Documentation

Index

Constants

View Source
const (
	TagDdb          = "ddb"
	TagItemHashJey  = "hash-key"
	TagItemRangeKey = "range-key"
	TagItemRequired = "required"
	TagItemTtlField = "ttl-ts"
)

Variables

This section is empty.

Functions

func IsHashKey

func IsHashKey(spec specs) bool

func IsKeyField

func IsKeyField(spec specs) bool

func IsRangeKey

func IsRangeKey(spec specs) bool

func IsRequired

func IsRequired(spec specs) bool

func ParseDdbTag

func ParseDdbTag(tag string) (specs, error)

func Version

func Version() string

Types

type DdbMarshaller

type DdbMarshaller struct {
	// contains filtered or unexported fields
}

func NewMarshaller

func NewMarshaller() *DdbMarshaller

func (*DdbMarshaller) GetUnmarshaledFields

func (me *DdbMarshaller) GetUnmarshaledFields(target interface{}, response map[string]*dynamodb.AttributeValue) (map[string]*dynamodb.AttributeValue, error)

func (*DdbMarshaller) Marshal

func (me *DdbMarshaller) Marshal(source interface{}) (result map[string]*dynamodb.AttributeValue, err error)

func (*DdbMarshaller) MarshalTagFilter

func (me *DdbMarshaller) MarshalTagFilter(source interface{}, filter func(spec specs) bool) (result map[string]*dynamodb.AttributeValue, err error)

func (*DdbMarshaller) SetDecapitalizeUntaggedFieldNames

func (marshaller *DdbMarshaller) SetDecapitalizeUntaggedFieldNames(value bool)

func (*DdbMarshaller) SetFieldNamePrefix

func (marshaller *DdbMarshaller) SetFieldNamePrefix(prefix string)

func (*DdbMarshaller) SetMarshalAllPublicFields

func (marshaller *DdbMarshaller) SetMarshalAllPublicFields(value bool)

func (*DdbMarshaller) Unmarshal

func (me *DdbMarshaller) Unmarshal(target interface{}, source map[string]*dynamodb.AttributeValue) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL