@@ -7,17 +7,44 @@ import (
77 "github.com/aws/aws-sdk-go-v2/service/dynamodb"
88)
99
10+ // TableOptions provides configuration options for a DynamoDB Table.
11+ //
12+ // T is the type of the items stored in the table.
1013type TableOptions [T any ] struct {
11- Schema * Schema [T ]
12- DynamoDBOptions []func (* dynamodb.Options )
14+ // Schema defines the schema for the table, including attribute mapping and validation.
15+ Schema * Schema [T ]
16+
17+ // DynamoDBOptions is a list of functions to customize the underlying DynamoDB client options.
18+ DynamoDBOptions []func (* dynamodb.Options )
19+
20+ // ExtensionRegistry holds the registry of extensions to be used with the table.
1321 ExtensionRegistry * ExtensionRegistry [T ]
22+
23+ // MaxConsecutiveErrors sets the maximum number of consecutive errors allowed during batch, query, or scan operations.
24+ // If this threshold is exceeded, the operation will stop and return.
25+ // If set to 0, the default value of DefaultMaxConsecutiveErrors will be used.
26+ MaxConsecutiveErrors uint
1427}
1528
29+ // DefaultMaxConsecutiveErrors is the fallback value used for MaxConsecutiveErrors when it is set to 0.
30+ // A value of 1 means the operation will stop after the first error.
31+ const DefaultMaxConsecutiveErrors uint = 1
32+
33+ // Table represents a strongly-typed DynamoDB table for items of type T.
34+ //
35+ // It provides methods for interacting with DynamoDB using the provided client and options.
1636type Table [T any ] struct {
17- client Client
37+ // client is the DynamoDB client used to perform operations on the table.
38+ client Client
39+
40+ // options holds the configuration options for the table.
1841 options TableOptions [T ]
1942}
2043
44+ // NewTable creates a new Table for items of type T using the provided client and configuration functions.
45+ //
46+ // The configuration functions can be used to customize the TableOptions before the table is created.
47+ // Returns an error if T is not a struct type or if required options cannot be resolved.
2148func NewTable [T any ](client Client , fns ... func (options * TableOptions [T ])) (* Table [T ], error ) {
2249 if reflect .TypeFor [T ]().Kind () != reflect .Struct {
2350 return nil , fmt .Errorf ("NewClient() can only be created from structs, %T given" , * new (T ))
@@ -32,6 +59,7 @@ func NewTable[T any](client Client, fns ...func(options *TableOptions[T])) (*Tab
3259 defaultResolvers := []resolverFn [T ]{
3360 resolveDefaultSchema [T ],
3461 resolveDefaultExtensionRegistry [T ],
62+ resolveDefaultMaxConsecutiveErrors [T ],
3563 }
3664
3765 for _ , fn := range defaultResolvers {
@@ -46,20 +74,44 @@ func NewTable[T any](client Client, fns ...func(options *TableOptions[T])) (*Tab
4674 }, nil
4775}
4876
77+ // WithSchema returns a configuration function that sets the Schema for TableOptions.
78+ //
79+ // Use this to specify a custom schema when creating a Table.
4980func WithSchema [T any ](schema * Schema [T ]) func (options * TableOptions [T ]) {
5081 return func (options * TableOptions [T ]) {
5182 options .Schema = schema
5283 }
5384}
5485
86+ // WithExtensionRegistry returns a configuration function that sets the ExtensionRegistry for TableOptions.
87+ //
88+ // Use this to specify a custom extension registry when creating a Table.
5589func WithExtensionRegistry [T any ](registry * ExtensionRegistry [T ]) func (options * TableOptions [T ]) {
5690 return func (options * TableOptions [T ]) {
5791 options .ExtensionRegistry = registry
5892 }
5993}
6094
95+ // WithMaxConsecutiveErrors returns a configuration function that sets the MaxConsecutiveErrors option for TableOptions.
96+ //
97+ // Use this to specify the maximum number of consecutive errors allowed during batch, query, or scan operations.
98+ // A value of 0 means no limit is enforced.
99+ // WithMaxConsecutiveErrors returns a configuration function that sets the MaxConsecutiveErrors option for TableOptions.
100+ //
101+ // Use this to specify the maximum number of consecutive errors allowed during batch, query, or scan operations.
102+ // If set to 0, the default value of DefaultMaxConsecutiveErrors will be used.
103+ func WithMaxConsecutiveErrors [T any ](maxConsecutiveErrors uint ) func (options * TableOptions [T ]) {
104+ return func (options * TableOptions [T ]) {
105+ options .MaxConsecutiveErrors = maxConsecutiveErrors
106+ }
107+ }
108+
109+ // resolverFn defines a function type for resolving or setting default options on TableOptions.
61110type resolverFn [T any ] func (opts * TableOptions [T ]) error
62111
112+ // resolveDefaultSchema sets a default schema on TableOptions if none is provided.
113+ //
114+ // Returns an error if the schema cannot be created.
63115func resolveDefaultSchema [T any ](opts * TableOptions [T ]) error {
64116 if opts .Schema == nil {
65117 var err error
@@ -72,10 +124,21 @@ func resolveDefaultSchema[T any](opts *TableOptions[T]) error {
72124 return nil
73125}
74126
127+ // resolveDefaultExtensionRegistry sets a default extension registry on TableOptions if none is provided.
75128func resolveDefaultExtensionRegistry [T any ](opts * TableOptions [T ]) error {
76129 if opts .ExtensionRegistry == nil {
77130 opts .ExtensionRegistry = DefaultExtensionRegistry [T ]()
78131 }
79132
80133 return nil
81134}
135+
136+ // resolveDefaultMaxConsecutiveErrors sets MaxConsecutiveErrors to DefaultMaxConsecutiveErrors
137+ // if it is not explicitly set (i.e., if the value is 0).
138+ // This ensures a sensible default for error handling in batch, query, or scan operations.
139+ func resolveDefaultMaxConsecutiveErrors [T any ](opts * TableOptions [T ]) error {
140+ if opts .MaxConsecutiveErrors == 0 {
141+ opts .MaxConsecutiveErrors = DefaultMaxConsecutiveErrors
142+ }
143+ return nil
144+ }
0 commit comments