This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
forked from crolly/dyngeo
/
config.go
163 lines (141 loc) · 4.26 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package dyngeo
import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/golang/geo/s2"
)
type GeoOptions func(c *Config)
// WithRetryOnFilterOfRadiusQuery sets a max number of retries allowed for radius query;
// Important to mention, this retry will be applied when the algo is trying to find if the results from dynamo db
// are part of the created circle. When this is enabled, the algorithm will do
//
// radius = radius * multiplier; for each reach the maxRetries - do radius * multiplier
//
// be aware that multipler has set 2 as default
func WithRetryOnFilterOfRadiusQuery(maxRetries, multiplier int) GeoOptions {
return func(c *Config) {
c.filterByRadiusMaxRetries = maxRetries
c.filterByRadiusRetryMultiplier = 2
if multiplier > 0 {
c.filterByRadiusRetryMultiplier = multiplier
}
}
}
// WithLongitudeFirst sets the order of a geo coordinate to be readed, the default value is false.
func WithLongitudeFirst(f bool) GeoOptions {
return func(c *Config) {
c.longitudeFirst = f
}
}
func WithUnmarshalAsJSON(f bool) GeoOptions {
return func(c *Config) {
c.unmarshalAsJSON = f
}
}
func WithConsistentRead(consistentRead bool) GeoOptions {
return func(c *Config) {
c.consistentRead = consistentRead
}
}
// WithTableName sets the table name expected to be queried
func WithTableName(name string) GeoOptions {
return func(c *Config) {
c.tableName = name
}
}
// WithHashKey sets a custom hashKey attribute name. When it's missing, the value is `hashKey`
func WithHashkey(attributeName string) GeoOptions {
return func(c *Config) {
c.hashKeyAttributeName = attributeName
}
}
// WithHashKey sets a custom rangeKey attribute name. When it's missing, the value is `rangeKey`
func WithRangekey(attributeName string) GeoOptions {
return func(c *Config) {
c.rangeKeyAttributeName = attributeName
}
}
// WithGeoHashkey sets a custom geo hashKey attribute name. When it's missing, the value is `geohash`
func WithGeoHashkey(attributeName string) GeoOptions {
return func(c *Config) {
c.geoHashAttributeName = attributeName
}
}
// WithGeoJSONKey sets a custom geo JSON key attribute name. When it's missing, the value is `geoJson`
func WithGeoJSONKey(attributeName string) GeoOptions {
return func(c *Config) {
c.geoJSONAttributeName = attributeName
}
}
// WithGeoIndex sets a custom geo index name. When it's missing, the value is `geohash-index`
func WithGeoIndex(name string) GeoOptions {
return func(c *Config) {
c.geoHashIndexName = name
}
}
// WithDynamoClient sets the dynamo client to be used
func WithDynamoClient(client *dynamodb.Client) GeoOptions {
return func(c *Config) {
c.dynamoDBClient = client
}
}
// WithS2RegionCoverer sets the s2.RegionCoverer to be used
func WithS2RegionCoverer(s2rc s2.RegionCoverer) GeoOptions {
return func(c *Config) {
c.s2RegionCoverer = s2rc
}
}
// WithHashkeyLen sets a custom hashKey len. When it's missing, the value is `5`
func WithHashkeyLen(len int8) GeoOptions {
return func(c *Config) {
c.hashKeyLength = len
}
}
// makeDefaultConfig sets config with default values
func makeDefaultConfig() *Config {
return &Config{
hashKeyAttributeName: "hashKey",
rangeKeyAttributeName: "rangeKey",
geoHashAttributeName: "geohash",
geoJSONAttributeName: "geoJson",
geoHashIndexName: "geohash-index",
hashKeyLength: 5,
s2RegionCoverer: s2.RegionCoverer{
MinLevel: 10,
MaxLevel: 10,
MaxCells: 10,
LevelMod: 0,
},
}
}
type Config struct {
filterByRadiusMaxRetries int
filterByRadiusRetryMultiplier int
longitudeFirst bool
unmarshalAsJSON bool
consistentRead bool
hashKeyLength int8
tableName string
hashKeyAttributeName string
rangeKeyAttributeName string
geoHashAttributeName string
geoJSONAttributeName string
geoHashIndexName string
dynamoDBClient *dynamodb.Client
s2RegionCoverer s2.RegionCoverer
}
func MakeConfig(opts ...GeoOptions) *Config {
cfg := makeDefaultConfig()
for _, opt := range opts {
opt(cfg)
}
return cfg
}
func (cfg Config) Valid() error {
if cfg.dynamoDBClient == nil {
return ErrMissingDynamoClient{}
}
if cfg.tableName == "" {
return ErrMissingDynamoTableName{}
}
return nil
}