123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package sql_result
- import (
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
- "git.sxidc.com/go-tools/utils/reflectutils"
- "git.sxidc.com/go-tools/utils/strutils"
- "git.sxidc.com/go-tools/utils/template"
- "github.com/iancoleman/strcase"
- "github.com/pkg/errors"
- "reflect"
- "strings"
- "time"
- )
- const (
- defaultSplitWith = "::"
- tagPartSeparator = ";"
- tagPartKeyValueSeparator = ":"
- )
- const (
- tagKey = "sqlresult"
- tagPartIgnore = "-"
- tagPartColumn = "column"
- tagPartTimeLayout = "timeLayout"
- tagPartAes = "aes"
- tagPartSplitWith = "splitWith"
- tagPartTrim = "trim"
- tagPartTrimPrefix = "trimPrefix"
- tagPartTrimSuffix = "trimSuffix"
- )
- type Tag struct {
- Name string
- TimeLayout string
- AESKey string
- SplitWith string
- Trim string
- TrimPrefix string
- TrimSuffix string
- }
- func parseTag(entityElemValue reflect.Value, onParsedFieldTagFunc OnParsedFieldTagFunc) error {
- for i := 0; i < entityElemValue.NumField(); i++ {
- entityField := entityElemValue.Type().Field(i)
- entityFieldValue := entityElemValue.Field(i)
- // 无效值,不进行映射
- if !entityFieldValue.IsValid() {
- continue
- }
- if entityFieldValue.Kind() == reflect.Pointer && entityFieldValue.IsNil() {
- entityFieldValue.Set(reflect.New(entityField.Type.Elem()))
- }
- entityFieldElemValue := reflectutils.PointerValueElem(entityFieldValue)
- tagStr := entityField.Tag.Get(tagKey)
- tag, err := parseFieldTag(entityField, tagStr)
- if err != nil {
- return err
- }
- if tag == nil {
- continue
- }
- // 结构类型的字段,解析结构内部
- if entityFieldElemValue.Kind() == reflect.Struct &&
- !reflectutils.IsValueTime(entityFieldElemValue) {
- err := parseTag(entityFieldElemValue, onParsedFieldTagFunc)
- if err != nil {
- return err
- }
- continue
- }
- err = onParsedFieldTagFunc(entityField.Name, entityFieldElemValue, tag)
- if err != nil {
- return err
- }
- }
- return nil
- }
- func parseFieldTag(field reflect.StructField, tagStr string) (*Tag, error) {
- if tagStr == tagPartIgnore {
- return nil, nil
- }
- tag := &Tag{
- Name: strcase.ToSnake(template.Id(field.Name)),
- TimeLayout: time.DateTime,
- AESKey: "",
- SplitWith: defaultSplitWith,
- }
- if strutils.IsStringEmpty(tagStr) {
- return tag, nil
- }
- 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], "'")
- }
- if strutils.IsStringEmpty(tagPartKeyValue[0]) {
- continue
- }
- switch tagPartKeyValue[0] {
- case tagPartColumn:
- tag.Name = tagPartKeyValue[1]
- case tagPartTimeLayout:
- tag.TimeLayout = tagPartKeyValue[1]
- case tagPartAes:
- if len(tagPartKeyValue[1]) != 32 {
- return nil, errors.New("AES密钥长度应该为32个字节")
- }
- tag.AESKey = tagPartKeyValue[1]
- case tagPartSplitWith:
- if strutils.IsStringEmpty(tagPartKeyValue[1]) {
- return nil, errors.New(defaultSplitWith + "没有赋值分隔符")
- }
- tag.SplitWith = tagPartKeyValue[1]
- case tagPartTrim:
- tag.Trim = tagPartKeyValue[1]
- case tagPartTrimPrefix:
- tag.TrimPrefix = tagPartKeyValue[1]
- case tagPartTrimSuffix:
- tag.TrimSuffix = tagPartKeyValue[1]
- default:
- err := errors.New(tagKey + "不支持的tag: " + tagPartKeyValue[0])
- logger.GetInstance().Error(err)
- continue
- }
- }
- }
- return tag, nil
- }
|