Skip to content

Commit

Permalink
新增:支持枚举转字符串
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Jun 30, 2024
1 parent 9d16656 commit 80082d7
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion parse/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ func Convert[T any](source any, defVal T) T {
return source.(T)
}

// 枚举转...
if sourceMeta.IsEmum{
switch defValMeta.TypeIdentity {
// 转数字
case "number":
result := anyToNumber(source, sourceMeta.Kind, defVal, defValMeta.Kind)
return result.(T)
// 转字符串
case "string":
// 先转成数字
result := anyToNumber(source, sourceMeta.Kind, sourceMeta.ZeroValue, sourceMeta.Kind)
result = NumberToString(result, sourceMeta.Kind)
return result.(T)
}
}

// 数字转...
if sourceMeta.IsNumber {
switch defValMeta.TypeIdentity {
Expand All @@ -51,7 +67,6 @@ func Convert[T any](source any, defVal T) T {
case "string":
var result any = NumberToString(source, sourceMeta.Kind)
return result.(T)
//return *(*T)(unsafe.Pointer(&str))
// 转bool
case "bool":
var result any = EqualTo1(source, sourceMeta.Kind)
Expand Down

0 comments on commit 80082d7

Please sign in to comment.