123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package assign
- import (
- "git.sxidc.com/go-tools/utils/reflectutils"
- "git.sxidc.com/go-tools/utils/strutils"
- "reflect"
- "strings"
- "time"
- )
- func DefaultUsage(from any, to any) error {
- return UseTag(from, to,
- func(fromFieldName string, fromFieldElemValue reflect.Value, retFieldElementValue reflect.Value, tag *Tag) error {
- if fromFieldElemValue.IsZero() {
- return nil
- }
- return defaultCallback(fromFieldElemValue, retFieldElementValue, tag)
- })
- }
- func defaultCallback(fromFieldElemValue reflect.Value, toFieldElemValue reflect.Value, tag *Tag) error {
- fromKind := reflectutils.GroupValueKind(fromFieldElemValue)
- toKind := reflectutils.GroupValueKind(toFieldElemValue)
- var fromAny any
- switch fromKind {
- case reflect.Struct:
- // time.Time类型的结构,接收字段是string类型,使用FormatTime的格式转换
- if reflectutils.IsValueTime(fromFieldElemValue) && toKind == reflect.String {
- fromString := fromFieldElemValue.Interface().(time.Time).Format(tag.TimeLayout)
- fromAny = trimFromString(fromString, tag)
- break
- }
- // 不是time.Time类型的结构,接收字段是结构,执行结构到结构字段的赋值
- if !reflectutils.IsValueTime(fromFieldElemValue) && toKind == reflect.Struct {
- return parseTag(fromFieldElemValue, &toFieldElemValue, func(fromFieldName string, fromFieldElemValue reflect.Value, toFieldElementValue reflect.Value, tag *Tag) error {
- return defaultCallback(fromFieldElemValue, toFieldElementValue, tag)
- })
- }
- // 直接将整个结构进行字段赋值
- fromAny = fromFieldElemValue.Interface()
- case reflect.Slice:
- if reflectutils.IsSliceValueOf(fromFieldElemValue, reflect.String) && toKind == reflect.String {
- fromString := strings.Join(fromFieldElemValue.Interface().([]string), tag.JoinWith)
- fromAny = trimFromString(fromString, tag)
- break
- }
- fromAny = fromFieldElemValue.Interface()
- case reflect.String:
- fromString := fromFieldElemValue.String()
- if reflectutils.IsValueTime(toFieldElemValue) {
- toTimeField, err := time.ParseInLocation(tag.TimeLayout, fromString, time.Local)
- if err != nil {
- return err
- }
- fromAny = toTimeField
- break
- }
- if reflectutils.IsSliceValueOf(toFieldElemValue, reflect.String) {
- fromAny = strings.Split(fromString, tag.SplitWith)
- break
- }
- fromAny = trimFromString(fromString, tag)
- default:
- fromAny = fromFieldElemValue.Interface()
- }
- switch toKind {
- case reflect.Int64:
- return reflectutils.AssignInt64Value(fromAny, toFieldElemValue)
- case reflect.Uint64:
- return reflectutils.AssignUint64Value(fromAny, toFieldElemValue)
- case reflect.Float64:
- return reflectutils.AssignFloat64Value(fromAny, toFieldElemValue)
- case reflect.Bool:
- return reflectutils.AssignBoolValue(fromAny, toFieldElemValue)
- case reflect.String:
- return reflectutils.AssignStringValue(fromAny, toFieldElemValue)
- default:
- toFieldElemValue.Set(reflect.ValueOf(fromAny))
- return nil
- }
- }
- func trimFromString(fromString string, tag *Tag) string {
- if strutils.IsStringNotEmpty(tag.Trim) {
- return strings.Trim(fromString, tag.Trim)
- } else {
- if strutils.IsStringNotEmpty(tag.TrimPrefix) {
- return strings.TrimPrefix(fromString, tag.TrimPrefix)
- }
- if strutils.IsStringNotEmpty(tag.TrimSuffix) {
- return strings.TrimSuffix(fromString, tag.TrimSuffix)
- }
- }
- return fromString
- }
|