MCPcopy Create free account
hub / github.com/goadesign/goa / toReflectType

Function toReflectType

expr/types.go:704–749  ·  view source on GitHub ↗

toReflectType converts the DataType to reflect.Type.

(dtype DataType)

Source from the content-addressed store, hash-verified

702
703// toReflectType converts the DataType to reflect.Type.
704func toReflectType(dtype DataType) reflect.Type {
705 switch dtype.Kind() {
706 case BooleanKind:
707 return reflect.TypeOf(true)
708 case IntKind:
709 return reflect.TypeOf(int(0))
710 case Int32Kind:
711 return reflect.TypeOf(int32(0))
712 case Int64Kind:
713 return reflect.TypeOf(int64(0))
714 case UIntKind:
715 return reflect.TypeOf(uint(0))
716 case UInt32Kind:
717 return reflect.TypeOf(uint32(0))
718 case UInt64Kind:
719 return reflect.TypeOf(uint64(0))
720 case Float32Kind:
721 return reflect.TypeOf(float32(0))
722 case Float64Kind:
723 return reflect.TypeOf(float64(0))
724 case StringKind:
725 return reflect.TypeOf("")
726 case BytesKind:
727 return reflect.TypeOf([]byte{})
728 case ObjectKind:
729 return reflect.TypeOf(map[string]any{})
730 case UserTypeKind:
731 return toReflectType(dtype.(*UserTypeExpr).Attribute().Type)
732 case ResultTypeKind:
733 return toReflectType(dtype.(*ResultTypeExpr).Attribute().Type)
734 case ArrayKind:
735 return reflect.SliceOf(toReflectType(dtype.(*Array).ElemType.Type))
736 case MapKind:
737 m := dtype.(*Map)
738 // avoid complication: not allow object as the map key
739 var ktype reflect.Type
740 if m.KeyType.Type.Kind() != ObjectKind {
741 ktype = toReflectType(m.KeyType.Type)
742 } else {
743 ktype = reflect.TypeOf([]any{}).Elem()
744 }
745 return reflect.MapOf(ktype, toReflectType(m.ElemType.Type))
746 default:
747 return reflect.TypeOf([]any{}).Elem()
748 }
749}

Callers 2

MakeSliceMethod · 0.85
MakeMapMethod · 0.85

Calls 2

KindMethod · 0.65
AttributeMethod · 0.65

Tested by

no test coverage detected