|
|
@@ -11,22 +11,22 @@ import (
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
- assignDefaultStringSliceSeparator = "::"
|
|
|
- assignTagPartSeparator = ";"
|
|
|
- assignTagPartKeyValueSeparator = ":"
|
|
|
+ defaultStringSliceSeparator = "::"
|
|
|
+ tagPartSeparator = ";"
|
|
|
+ tagPartKeyValueSeparator = ":"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
- assignTagKey = "assign"
|
|
|
- assignIgnore = "-"
|
|
|
- assignToField = "toField"
|
|
|
- assignParseTime = "parseTime"
|
|
|
- assignFormatTime = "formatTime"
|
|
|
- assignJoinWith = "joinWith"
|
|
|
- assignSplitWith = "splitWith"
|
|
|
- assignTrim = "trim"
|
|
|
- assignTrimPrefix = "trimPrefix"
|
|
|
- assignTrimSuffix = "trimSuffix"
|
|
|
+ tagKey = "assign"
|
|
|
+ tagPartIgnore = "-"
|
|
|
+ tagPartToField = "toField"
|
|
|
+ tagPartParseTime = "parseTime"
|
|
|
+ tagPartFormatTime = "formatTime"
|
|
|
+ tagPartJoinWith = "joinWith"
|
|
|
+ tagPartSplitWith = "splitWith"
|
|
|
+ tagPartTrim = "trim"
|
|
|
+ tagPartTrimPrefix = "trimPrefix"
|
|
|
+ tagPartTrimSuffix = "trimSuffix"
|
|
|
)
|
|
|
|
|
|
type Tag struct {
|
|
|
@@ -56,7 +56,7 @@ func parseTag(fromElemValue reflect.Value, toElemValue *reflect.Value, onParsedF
|
|
|
|
|
|
fromFieldElemValue := reflectutils.PointerValueElem(fromFieldValue)
|
|
|
|
|
|
- tagStr := fromField.Tag.Get(assignTagKey)
|
|
|
+ tagStr := fromField.Tag.Get(tagKey)
|
|
|
|
|
|
// 结构类型的字段上没有添加Tag, 先尝试直接按照字段赋值
|
|
|
if strutils.IsStringEmpty(tagStr) && fromFieldElemValue.Kind() == reflect.Struct &&
|
|
|
@@ -113,7 +113,7 @@ func parseTag(fromElemValue reflect.Value, toElemValue *reflect.Value, onParsedF
|
|
|
}
|
|
|
|
|
|
func parseFieldTag(field reflect.StructField, tagStr string) (*Tag, error) {
|
|
|
- if tagStr == assignIgnore {
|
|
|
+ if tagStr == tagPartIgnore {
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
@@ -121,8 +121,8 @@ func parseFieldTag(field reflect.StructField, tagStr string) (*Tag, error) {
|
|
|
ToField: field.Name,
|
|
|
ParseTime: time.DateTime,
|
|
|
FormatTime: time.DateTime,
|
|
|
- JoinWith: assignDefaultStringSliceSeparator,
|
|
|
- SplitWith: assignDefaultStringSliceSeparator,
|
|
|
+ JoinWith: defaultStringSliceSeparator,
|
|
|
+ SplitWith: defaultStringSliceSeparator,
|
|
|
Trim: "",
|
|
|
TrimPrefix: "",
|
|
|
TrimSuffix: "",
|
|
|
@@ -132,41 +132,41 @@ func parseFieldTag(field reflect.StructField, tagStr string) (*Tag, error) {
|
|
|
return tag, nil
|
|
|
}
|
|
|
|
|
|
- assignParts := strings.Split(tagStr, assignTagPartSeparator)
|
|
|
- if assignParts != nil || len(assignParts) != 0 {
|
|
|
- for _, assignPart := range assignParts {
|
|
|
- assignPartKeyValue := strings.SplitN(strings.TrimSpace(assignPart), assignTagPartKeyValueSeparator, 2)
|
|
|
- if assignPartKeyValue != nil && len(assignPartKeyValue) == 2 && strutils.IsStringNotEmpty(assignPartKeyValue[1]) {
|
|
|
- assignPartKeyValue[1] = strings.Trim(assignPartKeyValue[1], "'")
|
|
|
+ tagParts := strings.Split(tagStr, tagPartSeparator)
|
|
|
+ if tagParts != nil || len(tagParts) != 0 {
|
|
|
+ for _, tagPart := range tagParts {
|
|
|
+ tagPartKeyValue := strings.SplitN(strings.TrimSpace(tagPart), tagPartKeyValueSeparator, 2)
|
|
|
+ if tagPartKeyValue != nil && len(tagPartKeyValue) == 2 && strutils.IsStringNotEmpty(tagPartKeyValue[1]) {
|
|
|
+ tagPartKeyValue[1] = strings.Trim(tagPartKeyValue[1], "'")
|
|
|
}
|
|
|
|
|
|
- switch assignPartKeyValue[0] {
|
|
|
- case assignToField:
|
|
|
- tag.ToField = assignPartKeyValue[1]
|
|
|
- case assignParseTime:
|
|
|
- tag.ParseTime = assignPartKeyValue[1]
|
|
|
- case assignFormatTime:
|
|
|
- tag.FormatTime = assignPartKeyValue[1]
|
|
|
- case assignJoinWith:
|
|
|
- if strutils.IsStringEmpty(assignPartKeyValue[1]) {
|
|
|
- return nil, fserr.New(assignJoinWith + "没有赋值分隔符")
|
|
|
+ switch tagPartKeyValue[0] {
|
|
|
+ case tagPartToField:
|
|
|
+ tag.ToField = tagPartKeyValue[1]
|
|
|
+ case tagPartParseTime:
|
|
|
+ tag.ParseTime = tagPartKeyValue[1]
|
|
|
+ case tagPartFormatTime:
|
|
|
+ tag.FormatTime = tagPartKeyValue[1]
|
|
|
+ case tagPartJoinWith:
|
|
|
+ if strutils.IsStringEmpty(tagPartKeyValue[1]) {
|
|
|
+ return nil, fserr.New(tagPartJoinWith + "没有赋值分隔符")
|
|
|
}
|
|
|
|
|
|
- tag.JoinWith = assignPartKeyValue[1]
|
|
|
- case assignSplitWith:
|
|
|
- if strutils.IsStringEmpty(assignPartKeyValue[1]) {
|
|
|
- return nil, fserr.New(assignSplitWith + "没有赋值分隔符")
|
|
|
+ tag.JoinWith = tagPartKeyValue[1]
|
|
|
+ case tagPartSplitWith:
|
|
|
+ if strutils.IsStringEmpty(tagPartKeyValue[1]) {
|
|
|
+ return nil, fserr.New(tagPartSplitWith + "没有赋值分隔符")
|
|
|
}
|
|
|
|
|
|
- tag.SplitWith = assignPartKeyValue[1]
|
|
|
- case assignTrim:
|
|
|
- tag.Trim = assignPartKeyValue[1]
|
|
|
- case assignTrimPrefix:
|
|
|
- tag.TrimPrefix = assignPartKeyValue[1]
|
|
|
- case assignTrimSuffix:
|
|
|
- tag.TrimSuffix = assignPartKeyValue[1]
|
|
|
+ tag.SplitWith = tagPartKeyValue[1]
|
|
|
+ case tagPartTrim:
|
|
|
+ tag.Trim = tagPartKeyValue[1]
|
|
|
+ case tagPartTrimPrefix:
|
|
|
+ tag.TrimPrefix = tagPartKeyValue[1]
|
|
|
+ case tagPartTrimSuffix:
|
|
|
+ tag.TrimSuffix = tagPartKeyValue[1]
|
|
|
default:
|
|
|
- err := fserr.New(assignTagKey + "不支持的tag: " + assignPartKeyValue[0])
|
|
|
+ err := fserr.New(tagKey + "不支持的tag: " + tagPartKeyValue[0])
|
|
|
logger.GetInstance().Error(err)
|
|
|
continue
|
|
|
}
|