This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
/
options.go
162 lines (142 loc) · 4.21 KB
/
options.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
package options
type Format int
// file format
const (
JSON Format = 0
Protowire Format = 1
Prototext Format = 2
Excel Format = 3
CSV Format = 4
XML Format = 5
)
// Options is the wrapper of tableau params.
// Options follow the design of Functional Options (https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md).
type Options struct {
LocationName string // Location represents the collection of time offsets in use in a geographical area. Default is "Asia/Shanghai".
LogLevel string // Log level: debug, info, warn, error
Header *HeaderOption // header rows of excel file.
Output *OutputOption // output settings.
Input *InputOption // input settings.
Imports []string // imported common proto file paths
Workbook string // workbook path or name
Worksheet string // worksheet name
}
type HeaderOption struct {
Namerow int32
Typerow int32
Noterow int32
Datarow int32
Nameline int32
Typeline int32
}
type OutputOption struct {
// only for protogen generated protoconf file
FilenameWithSubdirPrefix bool // default true, filename dir separator `/` or `\` is replaced by "__"
FilenameSuffix string
// only for confgen generated JSON/prototext/protowire file
FilenameAsSnakeCase bool // default false, output filename as snake case, default is camel case same as the protobuf message name.
Format Format // output pretty format, with mulitline and indent.
Pretty bool // default true, output format: json, protowire, or prototext, and default is json.
// Output.EmitUnpopulated specifies whether to emit unpopulated fields. It does not
// emit unpopulated oneof fields or unpopulated extension fields.
// The JSON value emitted for unpopulated fields are as follows:
// ╔═══════╤════════════════════════════╗
// ║ JSON │ Protobuf field ║
// ╠═══════╪════════════════════════════╣
// ║ false │ proto3 boolean fields ║
// ║ 0 │ proto3 numeric fields ║
// ║ "" │ proto3 string/bytes fields ║
// ║ null │ proto2 scalar fields ║
// ║ null │ message fields ║
// ║ [] │ list fields ║
// ║ {} │ map fields ║
// ╚═══════╧════════════════════════════╝
EmitUnpopulated bool // default true
}
type InputOption struct {
Format Format
}
// Option is the functional option type.
type Option func(*Options)
func Header(o *HeaderOption) Option {
return func(opts *Options) {
opts.Header = o
}
}
func Output(o *OutputOption) Option {
return func(opts *Options) {
opts.Output = o
}
}
func Input(o *InputOption) Option {
return func(opts *Options) {
opts.Input = o
}
}
func LocationName(o string) Option {
return func(opts *Options) {
opts.LocationName = o
}
}
func LogLevel(level string) Option {
return func(opts *Options) {
opts.LogLevel = level
}
}
func Imports(imports []string) Option {
return func(opts *Options) {
opts.Imports = imports
}
}
func Workbook(wb string) Option {
return func(opts *Options) {
opts.Workbook = wb
}
}
func Worksheet(ws string) Option {
return func(opts *Options) {
opts.Worksheet = ws
}
}
func newDefaultOptions() *Options {
return &Options{
LocationName: "Asia/Shanghai",
LogLevel: "info",
Header: &HeaderOption{
Namerow: 1,
Typerow: 2,
Noterow: 3,
Datarow: 4,
},
Output: &OutputOption{
FilenameWithSubdirPrefix: true,
FilenameAsSnakeCase: false,
Format: JSON,
Pretty: true,
EmitUnpopulated: true,
},
Input: &InputOption{
Format: Excel,
},
}
}
func ParseOptions(setters ...Option) *Options {
// Default Options
opts := newDefaultOptions()
for _, setter := range setters {
setter(opts)
}
return opts
}
func Ext2Format(ext string) Format {
switch ext {
case ".xlsx":
return Excel
case ".xml":
return XML
case ".csv":
return CSV
default:
return Excel
}
}