github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/types/globalsecondaryindex/globalsecondaryindex.go (about) 1 // GlobalSecondaryIndex for defining new keys. See: 2 // http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GlobalSecondaryIndex.html 3 package globalsecondaryindex 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 "github.com/smugmug/godynamo/types/provisionedthroughput" 12 ) 13 14 type GlobalSecondaryIndex struct { 15 IndexName string 16 KeySchema keydefinition.KeySchema 17 Projection struct { 18 NonKeyAttributes []string 19 ProjectionType string 20 } 21 ProvisionedThroughput provisionedthroughput.ProvisionedThroughput 22 } 23 24 func NewGlobalSecondaryIndex() *GlobalSecondaryIndex { 25 g := new(GlobalSecondaryIndex) 26 g.KeySchema = make(keydefinition.KeySchema, 0) 27 g.Projection.NonKeyAttributes = make([]string, 0) 28 return g 29 } 30 31 type globalSecondaryIndex GlobalSecondaryIndex 32 33 type GlobalSecondaryIndexes []GlobalSecondaryIndex 34 35 func (g GlobalSecondaryIndex) MarshalJSON() ([]byte, error) { 36 if !(aws_strings.ALL == g.Projection.ProjectionType || 37 aws_strings.KEYS_ONLY == g.Projection.ProjectionType || 38 aws_strings.INCLUDE == g.Projection.ProjectionType) { 39 e := fmt.Sprintf("endpoint.GlobalSecondaryIndex.MarshalJSON: "+ 40 "ProjectionType %s is not valid", g.Projection.ProjectionType) 41 return nil, errors.New(e) 42 } 43 if len(g.Projection.NonKeyAttributes) > 20 { 44 e := fmt.Sprintf("endpoint.GlobalSecondaryIndex.MarshalJSON: " + 45 "NonKeyAttributes > 20") 46 return nil, errors.New(e) 47 } 48 var gi globalSecondaryIndex 49 gi.IndexName = g.IndexName 50 gi.KeySchema = g.KeySchema 51 gi.Projection = g.Projection 52 // if present, must have length between 1 and 20 53 if len(g.Projection.NonKeyAttributes) == 0 { 54 gi.Projection.NonKeyAttributes = nil 55 } 56 gi.ProvisionedThroughput = g.ProvisionedThroughput 57 gi.Projection.ProjectionType = g.Projection.ProjectionType 58 return json.Marshal(gi) 59 } 60 61 type GlobalSecondaryIndexDesc struct { 62 IndexName string 63 IndexSizeBytes uint64 64 IndexStatus string 65 ItemCount uint64 66 KeySchema keydefinition.KeySchema 67 Projection struct { 68 NonKeyAttributes []string 69 ProjectionType string 70 } 71 ProvisionedThroughput provisionedthroughput.ProvisionedThroughputDesc 72 } 73 74 func NewGlobalSecondaryIndexDesc() *GlobalSecondaryIndexDesc { 75 d := new(GlobalSecondaryIndexDesc) 76 d.KeySchema = make(keydefinition.KeySchema, 0) 77 d.Projection.NonKeyAttributes = make([]string, 0) 78 return d 79 } 80 81 type GlobalSecondaryIndexUpdates struct { 82 IndexName string 83 ProvisionedThroughput provisionedthroughput.ProvisionedThroughput 84 } 85 86 func NewGlobalSecondaryIndexUpdates() *GlobalSecondaryIndexUpdates { 87 g := new(GlobalSecondaryIndexUpdates) 88 return g 89 }