-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtag.go
111 lines (101 loc) · 2.41 KB
/
tag.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
package godiff
import (
"fmt"
"strconv"
"strings"
)
//Tag represents a tag
type Tag struct {
Name string
PresenceMarker bool
PairSeparator string
PairDelimiter string
pairDelimiter []string
ItemSeparator string
Whitespace string
IndexBy string
Sort bool
TimeLayout string
Precision *int
Ignore bool
NullifyEmpty *bool
}
func (t *Tag) decodable() bool {
return t.PairDelimiter != "" || t.ItemSeparator != ""
}
func (t *Tag) init(config *Config) {
if t.NullifyEmpty == nil {
t.NullifyEmpty = config.NullifyEmpty
}
if t.PairDelimiter != "" {
t.pairDelimiter = strings.Split(t.PairDelimiter, "|")
}
if t.PairDelimiter != "" && t.PairSeparator == "" {
t.PairSeparator = "="
}
}
func (t *Tag) removeWhitespace(value string) string {
if t.Whitespace == "" {
return value
}
value = strings.TrimSpace(value)
for _, ws := range strings.Split(t.Whitespace, "|") {
value = strings.ReplaceAll(value, ws, "")
}
return value
}
func (t Tag) clone() *Tag {
clone := t
return &clone
}
//ParseTag parses tag
func ParseTag(tagString string) (*Tag, error) {
tag := &Tag{}
if tagString == "-" {
tag.Ignore = true
return tag, nil
}
elements := strings.Split(tagString, ",")
if len(elements) == 0 {
return tag, nil
}
for _, element := range elements {
if count := strings.Count(element, "$coma"); count > 0 {
element = strings.Replace(element, "$coma", ",", count)
}
nv := strings.Split(element, "=")
switch len(nv) {
case 2:
switch strings.ToLower(strings.TrimSpace(nv[0])) {
case "ignore":
tag.Ignore = true
case "name":
tag.Name = strings.TrimSpace(nv[1])
case "indexby":
tag.IndexBy = strings.TrimSpace(nv[1])
case "timelayout":
tag.TimeLayout = strings.TrimSpace(nv[1])
case "precision":
precision, err := strconv.Atoi(strings.TrimSpace(nv[1]))
if err != nil {
return nil, fmt.Errorf("invalid precission: %w, %v", err, nv[1])
}
tag.Precision = &precision
case "whitespace":
tag.Whitespace = strings.TrimSpace(nv[1])
case "pairseparator":
tag.PairSeparator = strings.TrimSpace(nv[1])
case "pairdelimiter":
tag.PairDelimiter = strings.TrimSpace(nv[1])
case "itemseparator":
tag.ItemSeparator = strings.TrimSpace(nv[1])
case "sort":
tag.Sort, _ = strconv.ParseBool(strings.TrimSpace(nv[1]))
}
continue
case 1:
tag.Name = strings.TrimSpace(element)
}
}
return tag, nil
}