• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fogfish / dynamo / 11990190580

23 Nov 2024 08:27PM UTC coverage: 83.45% (-0.3%) from 83.747%
11990190580

push

github

web-flow
Use fogfish/opts library to declare options (#105)

60 of 68 new or added lines in 14 files covered. (88.24%)

2 existing lines in 2 files now uncovered.

1311 of 1571 relevant lines covered (83.45%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.3
/service/ddb/options.go
1
//
2
// Copyright (C) 2022 Dmitry Kolesnikov
3
//
4
// This file may be modified and distributed under the terms
5
// of the MIT license.  See the LICENSE file for details.
6
// https://github.com/fogfish/dynamo
7
//
8

9
package ddb
10

11
import (
12
        "context"
13

14
        "github.com/aws/aws-sdk-go-v2/aws"
15
        "github.com/aws/aws-sdk-go-v2/config"
16
        "github.com/aws/aws-sdk-go-v2/service/dynamodb"
17
        "github.com/fogfish/curie"
18
        "github.com/fogfish/opts"
19
)
20

21
// DynamoDB declares interface of original AWS DynamoDB API used by the library
22
type DynamoDB interface {
23
        GetItem(context.Context, *dynamodb.GetItemInput, ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
24
        PutItem(context.Context, *dynamodb.PutItemInput, ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
25
        DeleteItem(context.Context, *dynamodb.DeleteItemInput, ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
26
        UpdateItem(context.Context, *dynamodb.UpdateItemInput, ...func(*dynamodb.Options)) (*dynamodb.UpdateItemOutput, error)
27
        Query(context.Context, *dynamodb.QueryInput, ...func(*dynamodb.Options)) (*dynamodb.QueryOutput, error)
28
        BatchGetItem(context.Context, *dynamodb.BatchGetItemInput, ...func(*dynamodb.Options)) (*dynamodb.BatchGetItemOutput, error)
29
}
30

31
// Option type to configure the S3
32
type Option = opts.Option[Options]
33

34
// Config Options
35
type Options struct {
36
        table         string
37
        index         string
38
        hashKey       string
39
        sortKey       string
40
        useStrictType bool
41
        service       DynamoDB
42
}
43

44
func (c *Options) checkRequired() error {
1✔
45
        return opts.Required(c,
1✔
46
                WithTable(""),
1✔
47
        )
1✔
48
}
1✔
49

50
var (
51
        // Set DynamoDB table for session, the option is required
52
        WithTable = opts.ForName[Options, string]("table")
53

54
        // Set Global Secondary Index for the session
55
        WithGlobalSecondaryIndex = opts.ForName[Options, string]("index")
56

57
        // Configure the custom name for HashKey, default one is "prefix"
58
        WithHashKey = opts.ForName[Options, string]("hashKey")
59

60
        // Configure the custom name for SortKey, default one is "suffix"
61
        WithSortKey = opts.ForName[Options, string]("sortKey")
62

63
        // Configure CURIE prefixes, does nothing for DynamoDB
64
        WithPrefixes = opts.FMap(func(*Options, curie.Prefixes) error { return nil })
1✔
65

66
        // Enables strict serialization of the type, the I/O would fails
67
        // if type attributes does not match the storage schema.
68
        // It demand that storage schema "knows" all type attributes.
69
        WithStrictType = opts.ForName[Options, bool]("useStrictType")
70

71
        // Set DynamoDB client for the client
72
        WithService = opts.ForType[Options, DynamoDB]()
73

74
        // Set DynamoDB client for the client
75
        WithDynamoDB = opts.ForType[Options, DynamoDB]()
76

77
        // Configure client's DynamoDB to use provided the aws.Config
78
        WithConfig = opts.FMap(optsFromConfig)
79

80
        // Use default aws.Config for all DynamoDB clients
81
        WithDefaultDDB = opts.From(optsDefaultDDB)
82
)
83

84
// creates default config options
85
func optsDefault() Options {
1✔
86
        return Options{
1✔
87
                hashKey: "prefix",
1✔
88
                sortKey: "suffix",
1✔
89
        }
1✔
90
}
1✔
91

92
func optsDefaultDDB(c *Options) error {
1✔
93
        cfg, err := config.LoadDefaultConfig(context.Background())
1✔
94
        if err != nil {
1✔
NEW
95
                return err
×
UNCOV
96
        }
×
97
        return optsFromConfig(c, cfg)
1✔
98
}
99

100
func optsFromConfig(c *Options, cfg aws.Config) error {
1✔
101
        if c.service == nil {
2✔
102
                c.service = dynamodb.NewFromConfig(cfg)
1✔
103
        }
1✔
104
        return nil
1✔
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc