@@ -3,22 +3,45 @@ package dynamo
33import (
44 "context"
55 "fmt"
6- "regexp"
76
8- "github.com/aws/aws-sdk-go/service/dynamodb"
7+ "github.com/aws/aws-sdk-go-v2/service/dynamodb"
8+ "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
99 "github.com/k1LoW/errors"
1010 "github.com/k1LoW/tbls/dict"
1111 "github.com/k1LoW/tbls/schema"
1212)
1313
14- var re = regexp .MustCompile (`(?s)\n\s*` )
14+ // formatKeySchema formats KeySchemaElement slice to expected string format.
15+ func formatKeySchema (keySchema []types.KeySchemaElement ) string {
16+ if len (keySchema ) == 0 {
17+ return "[]"
18+ }
19+
20+ result := "["
21+ for i , k := range keySchema {
22+ if i > 0 {
23+ result += " "
24+ }
25+ result += fmt .Sprintf ("{ AttributeName: \" %s\" , KeyType: \" %s\" }" , * k .AttributeName , string (k .KeyType ))
26+ }
27+ result += "]"
28+ return result
29+ }
30+
31+ // formatProjection formats Projection to expected string format.
32+ func formatProjection (projection * types.Projection ) string {
33+ if projection == nil {
34+ return "{ ProjectionType: \" \" }"
35+ }
36+ return fmt .Sprintf ("{ ProjectionType: \" %s\" }" , string (projection .ProjectionType ))
37+ }
1538
1639type Dynamodb struct {
1740 ctx context.Context
18- client * dynamodb.DynamoDB
41+ client * dynamodb.Client
1942}
2043
21- func New (ctx context.Context , client * dynamodb.DynamoDB ) (* Dynamodb , error ) {
44+ func New (ctx context.Context , client * dynamodb.Client ) (* Dynamodb , error ) {
2245 return & Dynamodb {
2346 ctx : ctx ,
2447 client : client ,
@@ -38,16 +61,16 @@ func (d *Dynamodb) Analyze(s *schema.Schema) error {
3861 tables := []* schema.Table {}
3962 tableType := "BASIC TABLE"
4063 for {
41- list , err := d .client .ListTablesWithContext (d .ctx , input )
64+ list , err := d .client .ListTables (d .ctx , input )
4265 if err != nil {
4366 return err
4467 }
4568
4669 for _ , t := range list .TableNames {
4770 input := & dynamodb.DescribeTableInput {
48- TableName : t ,
71+ TableName : & t ,
4972 }
50- desc , err := d .client .DescribeTableWithContext (d .ctx , input )
73+ desc , err := d .client .DescribeTable (d .ctx , input )
5174 if err != nil {
5275 return err
5376 }
@@ -73,28 +96,28 @@ func (d *Dynamodb) Analyze(s *schema.Schema) error {
7396 return nil
7497}
7598
76- func listColumns (td * dynamodb .TableDescription ) []* schema.Column {
99+ func listColumns (td * types .TableDescription ) []* schema.Column {
77100 columns := []* schema.Column {}
78101 for _ , ad := range td .AttributeDefinitions {
79102 column := & schema.Column {
80103 Name : * ad .AttributeName ,
81- Type : * ad .AttributeType ,
104+ Type : string ( ad .AttributeType ) ,
82105 Nullable : false ,
83106 }
84107 columns = append (columns , column )
85108 }
86109 return columns
87110}
88111
89- func listConstraints (td * dynamodb .TableDescription ) []* schema.Constraint {
112+ func listConstraints (td * types .TableDescription ) []* schema.Constraint {
90113 constraints := []* schema.Constraint {}
91114 switch {
92115 case len (td .KeySchema ) == 2 :
93116 columns := []string {}
94117 for _ , k := range td .KeySchema {
95118 columns = append (columns , * k .AttributeName )
96119 }
97- def := re . ReplaceAllString ( fmt . Sprintf ( "%v" , td .KeySchema ), " " )
120+ def := formatKeySchema ( td .KeySchema )
98121 constraint := & schema.Constraint {
99122 Name : "Primary Key" ,
100123 Type : "Partition key and sort key" ,
@@ -107,7 +130,7 @@ func listConstraints(td *dynamodb.TableDescription) []*schema.Constraint {
107130 for _ , k := range td .KeySchema {
108131 columns = append (columns , * k .AttributeName )
109132 }
110- def := re . ReplaceAllString ( fmt . Sprintf ( "%v" , td .KeySchema ), " " )
133+ def := formatKeySchema ( td .KeySchema )
111134 constraint := & schema.Constraint {
112135 Name : "Primary Key" ,
113136 Type : "Partition key" ,
@@ -119,18 +142,18 @@ func listConstraints(td *dynamodb.TableDescription) []*schema.Constraint {
119142 return constraints
120143}
121144
122- func listIndexes (td * dynamodb .TableDescription ) []* schema.Index {
145+ func listIndexes (td * types .TableDescription ) []* schema.Index {
123146 indexes := []* schema.Index {}
124147 for _ , lsi := range td .LocalSecondaryIndexes {
125- def := re . ReplaceAllString ( fmt .Sprintf ("LocalSecondaryIndex { %s, %s }" , lsi .KeySchema , lsi .Projection . String ()), " " )
148+ def := fmt .Sprintf ("LocalSecondaryIndex { %s, %s }" , formatKeySchema ( lsi .KeySchema ), formatProjection ( lsi .Projection ) )
126149 Index := & schema.Index {
127150 Name : * lsi .IndexName ,
128151 Def : def ,
129152 }
130153 indexes = append (indexes , Index )
131154 }
132155 for _ , gsi := range td .GlobalSecondaryIndexes {
133- def := re . ReplaceAllString ( fmt .Sprintf ("GlobalSecondaryIndex { %s, %s }" , gsi .KeySchema , gsi .Projection . String ()), " " )
156+ def := fmt .Sprintf ("GlobalSecondaryIndex { %s, %s }" , formatKeySchema ( gsi .KeySchema ), formatProjection ( gsi .Projection ) )
134157 Index := & schema.Index {
135158 Name : * gsi .IndexName ,
136159 Def : def ,
0 commit comments