forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
160 lines (146 loc) · 4.68 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
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
package carbon
import (
"fmt"
"reflect"
"strings"
)
var (
// invalid pointer error
// 无效的指针错误
invalidPtrError = func() error {
return fmt.Errorf("invalid struct pointer, please make sure the struct is a pointer")
}
// invalid tag error
// 无效的标签错误
invalidTagError = func(field string) error {
return fmt.Errorf("invalid carbon tag in %s field, please make sure the tag is valid", field)
}
// supported types
// 支持的类型
tagTypes = map[string]string{
"dateTime": "layout:" + DateTimeLayout,
"dateTimeMilli": "layout:" + DateTimeMilliLayout,
"dateTimeMicro": "layout:" + DateTimeMicroLayout,
"dateTimeNano": "layout:" + DateTimeNanoLayout,
"shortDateTime": "layout:" + ShortDateTimeLayout,
"shortDateTimeMilli": "layout:" + ShortDateTimeMilliLayout,
"shortDateTimeMicro": "layout:" + ShortDateTimeMicroLayout,
"shortDateTimeNano": "layout:" + ShortDateTimeNanoLayout,
"dayDateTime": "layout:" + DayDateTimeLayout,
"date": "layout:" + DateLayout,
"dateMilli": "layout:" + DateMilliLayout,
"dateMicro": "layout:" + DateMicroLayout,
"dateNano": "layout:" + DateNanoLayout,
"shortDate": "layout:" + ShortDateLayout,
"shortDateMilli": "layout:" + ShortDateMilliLayout,
"shortDateMicro": "layout:" + ShortDateMicroLayout,
"shortDateNano": "layout:" + ShortDateNanoLayout,
"time": "layout:" + TimeLayout,
"timeMilli": "layout:" + TimeMilliLayout,
"timeMicro": "layout:" + TimeMicroLayout,
"timeNano": "layout:" + TimeNanoLayout,
"shortTime": "layout:" + ShortTimeLayout,
"shortTimeMilli": "layout:" + ShortTimeMilliLayout,
"shortTimeMicro": "layout:" + ShortTimeMicroLayout,
"shortTimeNano": "layout:" + ShortTimeNanoLayout,
"atom": "layout:" + AtomLayout,
"ansic": "layout:" + ANSICLayout,
"cookie": "layout:" + CookieLayout,
"kitchen": "layout:" + KitchenLayout,
"rss": "layout:" + RssLayout,
"rubyDate": "layout:" + RubyDateLayout,
"unixDate": "layout:" + UnixDateLayout,
"rfc1036": "layout:" + RFC1036Layout,
"rfc1123": "layout:" + RFC1123Layout,
"rfc1123Z": "layout:" + RFC1123ZLayout,
"rfc2822": "layout:" + RFC2822Layout,
"rfc3339": "layout:" + RFC3339Layout,
"rfc3339Milli": "layout:" + RFC3339MilliLayout,
"rfc3339Micro": "layout:" + RFC3339MicroLayout,
"rfc3339Nano": "layout:" + RFC3339NanoLayout,
"rfc7231": "layout:" + RFC7231Layout,
"rfc822": "layout:" + RFC822Layout,
"rfc822Z": "layout:" + RFC822ZLayout,
"rfc850": "layout:" + RFC850Layout,
"iso8601": "layout:" + ISO8601Layout,
"iso8601Milli": "layout:" + ISO8601MilliLayout,
"iso8601Micro": "layout:" + ISO8601MicroLayout,
"iso8601Nano": "layout:" + ISO8601NanoLayout,
"timestamp": "format:U",
"timestampMilli": "format:V",
"timestampMicro": "format:X",
"timestampNano": "format:Z",
}
)
// tag defines a tag struct.
// 定义 tag 结构体
type tag struct {
carbon string
tz string
}
// SetTag sets tag.
// 设置标签
func (c Carbon) SetTag(tag *tag) Carbon {
if c.Error != nil {
return c
}
c.tag = tag
return c
}
// parseTag parses tag.
// 解析标签
func (c Carbon) parseTag() (key, value, tz string) {
if c.tag == nil {
return "layout", defaultLayout, defaultTimezone
}
tz = strings.TrimSpace(c.tag.tz)
if tz == "" {
tz = defaultTimezone
}
carbon := strings.TrimSpace(c.tag.carbon)
if carbon == "" {
return "layout", defaultLayout, tz
}
if !strings.HasPrefix(carbon, "layout:") && !strings.HasPrefix(carbon, "format:") {
return "", "", tz
}
key = strings.TrimSpace(carbon[:6])
value = strings.TrimSpace(carbon[7:])
return
}
// LoadTag loads tag.
// 加载标签
func LoadTag(v interface{}) error {
typeObj, valueObj := reflect.TypeOf(v), reflect.ValueOf(v)
if typeObj.Kind() != reflect.Ptr {
return invalidPtrError()
}
typeElem, valueElem := typeObj.Elem(), valueObj.Elem()
params := make([]reflect.Value, 1)
for i := 0; i < valueElem.NumField(); i++ {
fieldType, fieldValue := typeElem.Field(i), valueElem.Field(i)
if reflect.TypeOf(Carbon{}) != fieldValue.Type() {
continue
}
carbon := fieldType.Tag.Get("carbon")
if carbon == "" {
carbon = "layout:" + defaultLayout
}
if strings.HasPrefix(carbon, "type:") {
carbon = tagTypes[carbon[5:]]
}
if !strings.HasPrefix(carbon, "layout:") && !strings.HasPrefix(carbon, "format:") {
return invalidTagError(fieldType.Name)
}
tz := fieldType.Tag.Get("tz")
if tz == "" {
tz = defaultTimezone
}
params[0] = reflect.ValueOf(&tag{
carbon: carbon,
tz: tz,
})
fieldValue.Set(fieldValue.MethodByName("SetTag").Call(params)[0])
}
return nil
}