github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/util/dynamo/table.go (about) 1 package dynamo 2 3 import ( 4 "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" 5 ) 6 7 type ITable interface { 8 Name() string 9 AttributeDefinitions() []types.AttributeDefinition 10 KeySchema() []types.KeySchemaElement 11 } 12 13 func NewTable(name, hashKey, rangeKey string, attrs map[string]types.ScalarAttributeType) ITable { 14 ads := make([]types.AttributeDefinition, 0, len(attrs)) 15 for name, attr := range attrs { 16 ads = append(ads, types.AttributeDefinition{ 17 AttributeName: &name, 18 AttributeType: attr, 19 }) 20 } 21 return &table{ 22 name: name, 23 attributeMap: ads, 24 hashAttr: hashKey, 25 rangeAttr: rangeKey, 26 } 27 } 28 29 type table struct { 30 name string 31 attributeMap []types.AttributeDefinition 32 hashAttr string 33 rangeAttr string 34 } 35 36 func (t table) Name() string { 37 return t.name 38 } 39 40 func (t table) AttributeDefinitions() []types.AttributeDefinition { 41 slc := make([]types.AttributeDefinition, 0, len(t.attributeMap)) 42 for _, attr := range t.attributeMap { 43 slc = append(slc, attr) 44 } 45 return slc 46 } 47 48 func (t table) KeySchema() []types.KeySchemaElement { 49 if t.rangeAttr == "" { 50 return []types.KeySchemaElement{ 51 { 52 AttributeName: &t.hashAttr, 53 KeyType: types.KeyTypeHash, 54 }, 55 } 56 } 57 return []types.KeySchemaElement{ 58 { 59 AttributeName: &t.hashAttr, 60 KeyType: types.KeyTypeHash, 61 }, 62 { 63 AttributeName: &t.rangeAttr, 64 KeyType: types.KeyTypeRange, 65 }, 66 } 67 }