github.com/go-generalize/volcago@v1.7.0/generator/type.go (about)

     1  package generator
     2  
     3  import (
     4  	"go/types"
     5  	"reflect"
     6  	"strings"
     7  
     8  	eptypes "github.com/go-generalize/go-easyparser/types"
     9  )
    10  
    11  const (
    12  	typeString       = "string"
    13  	typeInt          = "int"
    14  	typeInt64        = "int64"
    15  	typeFloat64      = "float64"
    16  	typeBool         = "bool"
    17  	_                = "interface{}"
    18  	_                = "time.Time"
    19  	_                = "*time.Time"
    20  	typeLatLng       = "latlng.LatLng"
    21  	typeReference    = "firestore.DocumentRef"
    22  	typeMap          = "map[string]"
    23  	_                = "map[string]string"
    24  	_                = "map[string]int"
    25  	_                = "map[string]int64"
    26  	_                = "map[string]float64"
    27  	typeBoolMap      = "map[string]bool"
    28  	typeInterfaceMap = "map[string]interface{}"
    29  )
    30  
    31  func getGoTypeFromEPTypes(t eptypes.Type) string {
    32  	switch t := t.(type) {
    33  	case *eptypes.String:
    34  		return "string"
    35  	case *eptypes.Number:
    36  		switch t.RawType {
    37  		case types.Int:
    38  			return "int"
    39  		case types.Int8:
    40  			return "int8"
    41  		case types.Int16:
    42  			return "int16"
    43  		case types.Int32:
    44  			return "int32"
    45  		case types.Int64:
    46  			return "int64"
    47  		case types.Uint:
    48  			return "uint"
    49  		case types.Uint8:
    50  			return "uint8"
    51  		case types.Uint16:
    52  			return "uint16"
    53  		case types.Uint32:
    54  			return "uint32"
    55  		case types.Uint64:
    56  			return "uint64"
    57  		case types.Uintptr:
    58  			return "uintptr"
    59  		case types.Float32:
    60  			return "float32"
    61  		case types.Float64:
    62  			return "float64"
    63  		}
    64  	case *eptypes.Boolean:
    65  		return "bool"
    66  	case *eptypes.Nullable:
    67  		r := getGoTypeFromEPTypes(t.Inner)
    68  
    69  		if r == "" {
    70  			return ""
    71  		}
    72  
    73  		if strings.HasPrefix(r, "[]") || strings.HasPrefix(r, "map[") {
    74  			return r
    75  		}
    76  
    77  		return "*" + r
    78  	case *eptypes.Array:
    79  		return "[]" + getGoTypeFromEPTypes(t.Inner)
    80  	case *eptypes.Date:
    81  		return "time.Time"
    82  	case *eptypes.Object:
    83  		names := strings.Split(t.Name, ".")
    84  		return names[len(names)-1]
    85  	case *eptypes.Map:
    86  		return "map[" + getGoTypeFromEPTypes(t.Key) + "]" + getGoTypeFromEPTypes(t.Value)
    87  	case *eptypes.Any:
    88  		return "interface{}"
    89  	case *documentRef:
    90  		return typeReference
    91  	case *latLng:
    92  		return typeLatLng
    93  	}
    94  
    95  	panic("unsupported: " + reflect.TypeOf(t).String())
    96  }