q/src/types/ByName.go
2025-02-17 14:31:47 +01:00

72 lines
1.1 KiB
Go

package types
import (
"strings"
)
// ByName returns the type with the given name or `nil` if it doesn't exist.
func ByName(name string, pkg string, structs map[string]*Struct) Type {
if strings.HasPrefix(name, "*") {
to := strings.TrimPrefix(name, "*")
typ := ByName(to, pkg, structs)
if typ == Any {
return AnyPointer
}
return &Pointer{To: typ}
}
if strings.HasPrefix(name, "[]") {
to := strings.TrimPrefix(name, "[]")
typ := ByName(to, pkg, structs)
if typ == Any {
return AnyArray
}
return &Array{Of: typ}
}
switch name {
case "int":
return Int
case "int64":
return Int64
case "int32":
return Int32
case "int16":
return Int16
case "int8":
return Int8
case "uint":
return UInt
case "uint64":
return UInt64
case "uint32":
return UInt32
case "uint16":
return UInt16
case "uint8":
return UInt8
case "float":
return Float
case "float64":
return Float64
case "float32":
return Float32
case "bool":
return Bool
case "any":
return Any
}
typ, exists := structs[pkg+"."+name]
if !exists {
return nil
}
return typ
}