Documentation
¶
Overview ¶
Package dynamodb reads parameters from an Amazon DynamoDB table, with the cache and the transforms of package parameters.
The table follows Python's schema, whose attribute names can be changed with WithKeyAttribute, WithSortAttribute and WithValueAttribute:
- a single value is the item whose partition key "id" is its name, and its "value" attribute: a table with only a partition key;
- the values under a path are the items whose partition key is the path, keyed by their sort key "sk": a table with a sort key.
Create a Provider once and share it:
var table = dynamodb.FromConfig(cfg, "ParameterTable") // or dynamodb.New(client, ...) value, err := table.Get(ctx, "my-param") values, err := table.GetMultiple(ctx, "my-hash-key")
Get calls GetItem, GetMultiple calls Query, every page; WithConsistentRead makes them strongly consistent. There is no shortcut, since a table must be named.
Differences from Python ¶
- Python's boto3 resource returns the value attribute as a Python value of any type. Here a string is returned as it is, a binary as its bytes, a number as its text, and any other type (map, list, set, boolean, null) as its JSON encoding, which parameters.TransformJSON decodes into a Go type.
- A local DynamoDB is reached through the BaseEndpoint of the AWS configuration, Python's endpoint_url.
Index ¶
Examples ¶
Constants ¶
const ( DefaultKeyAttribute = "id" DefaultSortAttribute = "sk" DefaultValueAttribute = "value" )
The attribute names of Python's table schema.
Variables ¶
var ErrUnsupportedAttribute = errors.New("unsupported attribute type")
ErrUnsupportedAttribute reports an attribute type this provider does not know, one added to DynamoDB after this code was written.
Functions ¶
func WithConsistentRead ¶
func WithConsistentRead(consistent bool) parameters.Option
WithConsistentRead makes the read strongly consistent (ConsistentRead).
Types ¶
type Client ¶
type Client interface {
GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
Query(ctx context.Context, params *dynamodb.QueryInput, optFns ...func(*dynamodb.Options)) (*dynamodb.QueryOutput, error)
}
Client is the part of the DynamoDB client the provider calls; *dynamodb.Client satisfies it.
type Option ¶
type Option func(*store)
Option configures the table schema of a Provider.
func WithKeyAttribute ¶
WithKeyAttribute names the partition key attribute, "id" by default.
func WithSortAttribute ¶
WithSortAttribute names the sort key attribute, "sk" by default.
func WithValueAttribute ¶
WithValueAttribute names the attribute that holds the value, "value" by default.
type Provider ¶
type Provider struct {
*parameters.Provider
// contains filtered or unexported fields
}
Provider reads parameters from a DynamoDB table. It is safe for concurrent use: create it once and share it across invocations.
A single value is the item whose partition key is the name. The values under a path are the items whose partition key is the path, keyed by their sort key. The value is the value attribute: a string is returned as is, a binary as its bytes, a number as its text, and any other type (map, list, set, boolean, null) as its JSON encoding, which parameters.TransformJSON decodes.
func FromConfig ¶
FromConfig returns a provider with a DynamoDB client built from cfg. A local DynamoDB is reached through cfg's BaseEndpoint, Python's endpoint_url.
func New ¶
New returns a provider that reads table through client.
Example ¶
// In a function: dynamodb.FromConfig(cfg, "ParameterTable"), created
// once in main.
provider := dynamodb.New(newTable(), "ParameterTable")
value, err := provider.Get(context.Background(), "my-param")
if err != nil {
panic(err)
}
all, err := parameters.GetMultiple[string](context.Background(), provider, "my-hash-key")
if err != nil {
panic(err)
}
fmt.Println(value, all["param-a"], all["param-b"])
Output: my-value my-value-a my-value-b