-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
164 lines (153 loc) · 4.06 KB
/
option.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
164
package router
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
_ "time/tzdata"
"github.com/kayac/s3-object-router/wildcard"
"github.com/mickep76/mapslice-json"
"github.com/pkg/errors"
)
var DefaultTimeKey = "time"
// Option represents option values of router
type Option struct {
Bucket string `json:"bucket,omitempty"`
KeyPrefix string `json:"key_prefix,omitempty"`
TimeParse bool `json:"time_parse,omitempty"`
TimeKey string `json:"time_key,omitempty"`
TimeFormat string `json:"time_format,omitempty"`
LocalTime bool `json:"local_time,omitempty"`
TimeZone string `json:"timezone,omitempty"`
Gzip bool `json:"gzip,omitempty"`
Replacer string `json:"replacer,omitempty"`
Parser string `json:"parser,omitempty"`
PutS3 bool `json:"put_s3,omitempty"`
ObjectFormat string `json:"object_format,omitempty"`
KeepOriginalName bool `json:"keep_original_name,omitempty"`
replacer replacer
recordParser recordParser
newEncoder func() encoder
newBuffer func() buffer
timeParser timeParser
}
type replacer interface {
Replace(string) string
}
type recordParser interface {
Parse([]byte) ([]*record, error)
}
type timeParser struct {
layout string
loc *time.Location
}
func (p timeParser) Parse(s string) (time.Time, error) {
t, err := time.Parse(p.layout, s)
if err != nil {
return t, err
}
return t.In(p.loc), nil
}
// Init initializes option struct.
func (opt *Option) Init() error {
if opt.Bucket == "" {
return errors.New("bucket must not be empty")
}
if opt.KeyPrefix == "" {
return errors.New("key-prefix must not be empty")
}
if opt.Replacer != "" {
mp := mapslice.MapSlice{}
if err := json.Unmarshal([]byte(opt.Replacer), &mp); err != nil {
return errors.Wrap(err, "invalid replacer")
}
args := make([]string, 0, len(mp)*2)
for _, kv := range mp {
if key, ok := kv.Key.(string); !ok {
return errors.New("replacer pattern must be string")
} else {
args = append(args, key)
}
if value, ok := kv.Value.(string); !ok {
return errors.New("replacer replacement must be string")
} else {
args = append(args, value)
}
}
opt.replacer = wildcard.NewReplacer(args...)
} else {
opt.replacer = strings.NewReplacer() // nop replacer
}
switch opt.Parser {
case "", "json":
opt.recordParser = recordParserFunc(func(b []byte) ([]*record, error) {
r := newRecord(b)
if err := json.Unmarshal(b, &(r.parsed)); err != nil {
return nil, err
}
return []*record{r}, nil
})
case "json.Records":
opt.recordParser = recordParserFunc(func(b []byte) ([]*record, error) {
var m struct {
Records []map[string]interface{} `json:"Records"`
}
if err := json.Unmarshal(b, &m); err != nil {
return nil, err
}
records := make([]*record, 0, len(m.Records))
for _, parsed := range m.Records {
p := parsed
records = append(records, &record{parsed: p, raw: nil})
}
return records, nil
})
case "cloudfront":
opt.recordParser = &cloudfrontParser{}
default:
return errors.New("parser must be string any of json|cloudfront")
}
if opt.TimeParse {
p := timeParser{layout: opt.TimeFormat}
switch {
case opt.LocalTime:
p.loc = time.Local
case opt.TimeZone != "":
var err error
p.loc, err = time.LoadLocation(opt.TimeZone)
if err != nil {
return fmt.Errorf("timezone is invalid, %w", err)
}
default:
p.loc = time.UTC
}
opt.timeParser = p
}
if opt.TimeKey == "" {
opt.TimeKey = DefaultTimeKey
}
if opt.Gzip {
opt.newBuffer = newGzipBuffer
} else {
opt.newBuffer = func() buffer {
return new(bytes.Buffer)
}
}
switch opt.ObjectFormat {
case "", "none":
if opt.Parser == "json.Records" {
return errors.New("parser must not be json.Records when object-format is none")
}
opt.newEncoder = func() encoder {
return newNoneEncoder(opt.newBuffer())
}
case "json":
opt.newEncoder = func() encoder {
return newJSONEncoder(opt.newBuffer())
}
default:
return errors.New("format must be string any of json|none")
}
return nil
}