-
Notifications
You must be signed in to change notification settings - Fork 7
/
field_value.go
129 lines (114 loc) · 2.97 KB
/
field_value.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
package main
import (
"fmt"
"time"
"github.com/araddon/dateparse"
"github.com/qiangyt/jog/config"
"github.com/qiangyt/jog/util"
)
// FieldValueT ...
type FieldValueT struct {
value util.AnyValue
enumValue config.Enum
timeValue time.Time
Output string
Config config.Field
}
// FieldValue ...
type FieldValue = *FieldValueT
// GetColor ...
func (i FieldValue) GetColor() util.Color {
if i.enumValue != nil {
return i.enumValue.Color
}
return i.Config.Color
}
// NewFieldValue ...
func NewFieldValue(cfg config.Configuration, options Options, fieldConfig config.Field, value util.AnyValue) FieldValue {
var enumValue config.Enum
var err error
var output string
text := value.Text
if fieldConfig.IsEnum() {
enumValue = fieldConfig.Enums.GetEnum(text)
output = enumValue.Name
} else {
if fieldConfig.CompressPrefix.Enabled {
output = fieldConfig.CompressPrefix.Compress(text)
} else {
output = text
}
}
var timeValue time.Time
if options.HasTimestampFilter() {
if fieldConfig.Type == config.FieldTypeTime {
loc := fieldConfig.TimeLocation
tmFormat := fieldConfig.TimeFormat
if loc != nil {
if len(tmFormat) != 0 {
timeValue, err = time.ParseInLocation(tmFormat, text, loc)
if err != nil {
panic(fmt.Errorf("failed to parse time value: %s, with format: %s, loc: %v", text, tmFormat, loc))
}
} else {
timeValue, err = dateparse.ParseIn(text, loc)
if err != nil {
panic(fmt.Errorf("failed to parse time value: %s, loc: %v", text, loc))
}
}
} else {
if len(tmFormat) != 0 {
timeValue, err = time.Parse(tmFormat, text)
if err != nil {
panic(fmt.Errorf("failed to parse time value: %s, with format: %s", text, tmFormat))
}
} else {
timeValue, err = dateparse.ParseAny(text)
if err != nil {
panic(fmt.Errorf("failed to parse time value: %s", text))
}
}
}
}
}
return &FieldValueT{
value: value,
enumValue: enumValue,
timeValue: timeValue,
Output: output,
Config: fieldConfig,
}
}
// ParseTimestamp ...
func ParseTimestamp(fieldConfig config.Field, text string) time.Time {
var timeValue time.Time
var err error
loc := fieldConfig.TimeLocation
tmFormat := fieldConfig.TimeFormat
if loc != nil {
if len(tmFormat) != 0 {
timeValue, err = time.ParseInLocation(tmFormat, text, loc)
if err != nil {
panic(fmt.Errorf("failed to parse time value: %s, with format: %s, loc: %v", text, tmFormat, loc))
}
} else {
timeValue, err = dateparse.ParseIn(text, loc)
if err != nil {
panic(fmt.Errorf("failed to parse time value: %s, loc: %v", text, loc))
}
}
} else {
if len(tmFormat) != 0 {
timeValue, err = time.Parse(tmFormat, text)
if err != nil {
panic(fmt.Errorf("failed to parse time value: %s, with format: %s", text, tmFormat))
}
} else {
timeValue, err = dateparse.ParseAny(text)
if err != nil {
panic(fmt.Errorf("failed to parse time value: %s", text))
}
}
}
return timeValue
}