github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/types/localsecondaryindex/localsecondaryindex.go (about)

     1  // LocalSecondaryIndex for defining new keys. See:
     2  // http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_LocalSecondaryIndex.html
     3  package localsecondaryindex
     4  
     5  import (
     6  	"encoding/json"
     7  	"errors"
     8  	"fmt"
     9  	"github.com/smugmug/godynamo/types/aws_strings"
    10  	"github.com/smugmug/godynamo/types/keydefinition"
    11  )
    12  
    13  type LocalSecondaryIndex struct {
    14  	IndexName  string                  `json:",omitempty"`
    15  	KeySchema  keydefinition.KeySchema `json:",omitempty"`
    16  	Projection struct {
    17  		NonKeyAttributes []string `json:",omitempty"`
    18  		ProjectionType   string   `json:",omitempty"`
    19  	}
    20  }
    21  
    22  func NewLocalSecondaryIndex() *LocalSecondaryIndex {
    23  	l := new(LocalSecondaryIndex)
    24  	l.KeySchema = make(keydefinition.KeySchema, 0)
    25  	l.Projection.NonKeyAttributes = make([]string, 0)
    26  	return l
    27  }
    28  
    29  type localSecondaryIndex LocalSecondaryIndex
    30  
    31  type LocalSecondaryIndexes []LocalSecondaryIndex
    32  
    33  func (l LocalSecondaryIndex) MarshalJSON() ([]byte, error) {
    34  	if !(aws_strings.ALL == l.Projection.ProjectionType ||
    35  		aws_strings.KEYS_ONLY == l.Projection.ProjectionType ||
    36  		aws_strings.INCLUDE == l.Projection.ProjectionType) {
    37  		e := fmt.Sprintf("endpoint.LocalSecondaryIndex.MarshalJSON: "+
    38  			"ProjectionType %s is not valid", l.Projection.ProjectionType)
    39  		return nil, errors.New(e)
    40  	}
    41  	if len(l.Projection.NonKeyAttributes) > 20 {
    42  		e := fmt.Sprintf("endpoint.LocalSecondaryIndex.MarshalJSON: " +
    43  			"NonKeyAttributes > 20")
    44  		return nil, errors.New(e)
    45  	}
    46  	var li localSecondaryIndex
    47  	li.IndexName = l.IndexName
    48  	li.KeySchema = l.KeySchema
    49  	li.Projection = l.Projection
    50  	// if present, must have length between 1 and 20
    51  	if l.Projection.NonKeyAttributes != nil && len(l.Projection.NonKeyAttributes) != 0 {
    52  		li.Projection.NonKeyAttributes = l.Projection.NonKeyAttributes
    53  	} else {
    54  		li.Projection.NonKeyAttributes = nil
    55  	}
    56  	li.Projection.ProjectionType = l.Projection.ProjectionType
    57  	return json.Marshal(li)
    58  }
    59  
    60  type LocalSecondaryIndexDesc struct {
    61  	IndexName      string
    62  	IndexSizeBytes uint64
    63  	ItemCount      uint64
    64  	KeySchema      keydefinition.KeySchema
    65  	Projection     struct {
    66  		NonKeyAttributes []string
    67  		ProjectionType   string
    68  	}
    69  }
    70  
    71  func NewLocalSecondaryIndexDesc() *LocalSecondaryIndexDesc {
    72  	d := new(LocalSecondaryIndexDesc)
    73  	d.KeySchema = make(keydefinition.KeySchema, 0)
    74  	d.Projection.NonKeyAttributes = make([]string, 0)
    75  	return d
    76  }