-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
165 lines (137 loc) · 3.75 KB
/
config.go
File metadata and controls
165 lines (137 loc) · 3.75 KB
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
164
165
package gsheets
import (
"context"
"reflect"
"github.com/gertd/go-pluralize"
"google.golang.org/api/sheets/v4"
)
// defaultTag is the default tag-name to be looked at in the structs.
const defaultTag = "gsheets"
// Config holds the configuration for the Google Sheets parser.
type Config struct {
Service *sheets.Service
config
}
type config struct {
spreadsheetID string
sheetName string
tagName string
datetimeFormats []string
allowSkipFields bool
allowSkipColumns bool
built bool
ctx context.Context
fetch fetchFN
}
// MakeConfig creates a new Config with the given Google Sheets service and arbitrary options.
func MakeConfig(svc *sheets.Service, spreadsheetID string, opts ...ConfigOption) Config {
cfg := &config{
spreadsheetID: spreadsheetID,
tagName: defaultTag,
fetch: fetchViaGoogleAPI,
}
for _, modify := range opts {
modify(cfg)
}
return Config{
Service: svc,
config: *cfg,
}
}
// ConfigOption is a function allows to modify a Config.
type ConfigOption func(*config)
// WithContext sets the given context for the Config.
// This context will be used for all API calls, and cancellation will be respected during iteration.
func WithContext(ctx context.Context) ConfigOption {
return func(c *config) {
c.ctx = ctx
}
}
// WithSpreadsheetID sets the spreadsheet ID for the Config.
func WithSpreadsheetID(id string) ConfigOption {
return func(c *config) {
c.spreadsheetID = id
}
}
// WithSheetName sets the sheet-name for the Config.
func WithSheetName(name string) ConfigOption {
return func(c *config) {
c.sheetName = name
}
}
// WithTagName sets the tag-name to be looked at in the structs.
// This might come in handy if you have multiple structs with different tags,
// or another library also uses `gsheets:` as tag identifier.
func WithTagName(name string) ConfigOption {
return func(c *config) {
c.tagName = name
}
}
// WithDatetimeFormats allows to define additional date-time formats to be recognized during the parsing.
func WithDatetimeFormats(formats ...string) ConfigOption {
return func(c *config) {
c.datetimeFormats = formats
}
}
// WithAllowSkipFields allows to skip fields that are not found in the sheet.
// If this is set to false, an error will be raised.
func WithAllowSkipFields(allow bool) ConfigOption {
return func(c *config) {
c.allowSkipFields = allow
}
}
// WithAllowSkipColumns allows to skip columns that cannot be mapped to a struct field.
// If this is set to false, an error will be raised.
func WithAllowSkipColumns(allow bool) ConfigOption {
return func(c *config) {
c.allowSkipColumns = allow
}
}
// withFetch is for testing purposes only, and allows to mock the call to the Google Sheets API.
func withFetch(fetch fetchFN) ConfigOption {
return func(c *config) {
c.fetch = fetch
}
}
func (c Config) init(ref reflect.Type, opts []ConfigOption) (Config, error) {
if len(opts) > 0 {
for _, modify := range opts {
modify(&c.config)
}
c.built = false
}
if c.built {
return c, nil
}
if c.Service == nil {
return c, ErrNoService
}
if c.spreadsheetID == "" {
return c, ErrNoSpreadSheetID
}
if c.fetch == nil {
c.fetch = fetchViaGoogleAPI
}
if c.tagName == "" {
c.tagName = defaultTag
}
if c.sheetName == "" {
c.sheetName = pluralizeClient.Plural(ref.Name())
}
c.datetimeFormats = append(c.datetimeFormats, dateTimeFormats[:]...)
c.built = true
return c, nil
}
// Context returns the configured context, or creates a new background context
func (c *config) Context() context.Context {
if c.ctx == nil {
c.ctx = context.Background()
}
return c.ctx
}
var pluralizeClient = pluralize.NewClient()
var dateTimeFormats = [...]string{
"2006-01-02",
"2006-01-02 15:04:05",
"2006-01-02 15:04:05 -0700",
}