-
Notifications
You must be signed in to change notification settings - Fork 0
/
hunkee.go
126 lines (109 loc) · 3.6 KB
/
hunkee.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
package hunkee
import (
"errors"
"time"
)
var (
ErrSyntax = errors.New("syntax error")
ErrOnlyStructs = errors.New("only struct types supported")
ErrNotSpecified = errors.New("tag not specified")
ErrNotUint = errors.New("corresponded kind is not Uint-like")
ErrNotInt = errors.New("corresponded kind is not Int-like")
ErrNotFloat = errors.New("corresponded kind is not Float32 or Float64")
ErrEmptyLine = errors.New("empty line passed")
ErrComaNotSupported = errors.New("coma-separated tag options is not supported")
ErrUnexpectedColon = errors.New("unexpected ':' while parsing format string")
ErrNotSupportedType = errors.New("corresponded kind is not supported")
ErrNilTimeOptions = errors.New("nil time options, time cannot be parsed")
)
type Parser struct {
mapper *mapper
debug bool
}
func NewParser(format string, to interface{}) (*Parser, error) {
mapper, err := initMapper(format, to)
if err != nil {
return nil, err
}
p := &Parser{
mapper: mapper,
}
p.SetCommentPrefix("#")
return p, nil
}
type TimeOption struct {
Layout string
Location *time.Location
}
// ParseLine gets line of input and structure to parse in
// Returns ErrEmptyLine if passed empty string or string with only \n
func (p *Parser) ParseLine(line string, to interface{}) error {
return p.parseLine(line, to)
}
// SetDebug makes hunkee more verbose
func (p *Parser) SetDebug(val bool) {
p.debug = val
debug = val
}
// SetTimeLayout setups provided time layout for time.Time
// fields in log entry. By default it's corresponded to
// RFC3339 - "2006-01-02T15:04:05Z07:00"
func (p *Parser) SetTimeLayout(tag, timeLayout string) {
p.mapper.fields[tag].timeOptions.Layout = timeLayout
}
// SetMultiplyTimeLayout receives map of TAG -> LAYOUT and sets up
// proposed layouts for different fields by their tag.
func (p *Parser) SetMultiplyTimeLayout(tagToLayouts map[string]string) {
for tag, layout := range tagToLayouts {
p.SetTimeLayout(tag, layout)
}
}
// SetTimeLocation used to parse time in provided location.
func (p *Parser) SetTimeLocation(tag string, loc *time.Location) {
if loc == nil {
panic("passed nil location")
}
p.mapper.fields[tag].timeOptions.Location = loc
}
// SetTimeOption sets provided timeOption to provided tag.
// Make sure you do it once at start, no andy dynamic behavior
func (p *Parser) SetTimeOption(tag string, to *TimeOption) {
if to == nil {
return
}
p.mapper.fields[tag].timeOptions = to
}
// TimeOption returns corresponded TimeOptions for tag
func (p *Parser) TimeOption(tag string) *TimeOption {
if to := p.mapper.fields[tag].timeOptions; to != nil {
return to
}
return nil
}
// SetCommentPrefix receives prefix which from will be start commented lines.
// As soon parser will get string with such prefix, that line will be ignored.
// Default commentary prefix is '#'.
func (p *Parser) SetCommentPrefix(pref string) {
p.mapper.comPrefix = pref
p.mapper.prefixActive = true
}
// SetWorkersAmount increases amount of workers from current value (10 by default)
// up to provided value
func (p *Parser) SetWorkersAmount(amount int) {
p.mapper.gainWorkers(amount)
}
// SetTokenSeparator receives byte which will be before token and right after it
// (useful when some fields contains more than one word).
// E.g. provided line:
//
// '"user" "123" "hunkee is slow"' with the next format line:
// ':name :id :description'
// The token separator here is '"'.
func (p *Parser) SetTokenSeparator(sep byte) {
p.mapper.tokenSep = sep
}
func DefaultTimeOptions() *TimeOption {
return &TimeOption{
Layout: time.RFC3339, // default time layout "2006-01-02T15:04:05Z07:00"
}
}