carbon/tag.go

161 lines
4.7 KiB
Go
Raw Normal View History

2023-12-22 16:09:01 +08:00
package carbon
import (
"fmt"
"reflect"
"strings"
)
var (
2024-01-04 10:07:47 +08:00
// 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",
}
2024-02-01 11:24:43 +08:00
// 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)
}
2023-12-22 16:09:01 +08:00
)
// tag defines a tag struct.
// 定义 tag 结构体
type tag struct {
carbon string
tz string
2023-12-22 16:09:01 +08:00
}
// SetTag sets tag.
// 设置标签
2024-01-07 20:07:03 +08:00
func (c Carbon) SetTag(tag *tag) Carbon {
2023-12-22 16:09:01 +08:00
if c.Error != nil {
return c
}
c.tag = tag
return c
}
// parseTag parses tag.
// 解析标签
func (c Carbon) parseTag() (key, value, tz string) {
2024-01-07 20:07:03 +08:00
if c.tag == nil {
return "layout", defaultLayout, defaultTimezone
}
tz = strings.TrimSpace(c.tag.tz)
if tz == "" {
2024-01-07 20:07:03 +08:00
tz = defaultTimezone
2023-12-22 16:09:01 +08:00
}
carbon := strings.TrimSpace(c.tag.carbon)
2024-01-07 20:07:03 +08:00
if carbon == "" {
return "layout", defaultLayout, tz
}
2024-01-16 11:08:44 +08:00
if !strings.HasPrefix(carbon, "layout:") && !strings.HasPrefix(carbon, "format:") {
return "", "", tz
}
key = strings.TrimSpace(carbon[:6])
value = strings.TrimSpace(carbon[7:])
return
2023-12-22 16:09:01 +08:00
}
// LoadTag loads tag.
// 加载标签
func LoadTag(v interface{}) error {
typeObj, valueObj := reflect.TypeOf(v), reflect.ValueOf(v)
if typeObj.Kind() != reflect.Ptr {
2023-12-22 16:09:01 +08:00
return invalidPtrError()
}
typeElem, valueElem := typeObj.Elem(), valueObj.Elem()
2023-12-22 16:09:01 +08:00
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")
2024-01-04 10:07:47 +08:00
if carbon == "" {
2024-01-07 20:07:03 +08:00
carbon = "layout:" + defaultLayout
2024-01-04 10:07:47 +08:00
}
2024-01-12 10:57:10 +08:00
if strings.HasPrefix(carbon, "type:") {
2024-01-04 10:07:47 +08:00
carbon = tagTypes[carbon[5:]]
2023-12-22 16:09:01 +08:00
}
2024-01-12 10:57:10 +08:00
if !strings.HasPrefix(carbon, "layout:") && !strings.HasPrefix(carbon, "format:") {
2024-01-04 10:07:47 +08:00
return invalidTagError(fieldType.Name)
2023-12-22 16:09:01 +08:00
}
tz := fieldType.Tag.Get("tz")
if tz == "" {
2024-01-07 20:07:03 +08:00
tz = defaultTimezone
}
2024-01-07 20:07:03 +08:00
params[0] = reflect.ValueOf(&tag{
carbon: carbon,
tz: tz,
})
fieldValue.Set(fieldValue.MethodByName("SetTag").Call(params)[0])
2023-12-22 16:09:01 +08:00
}
return nil
}