Documentation ¶
Overview ¶
gotype package provides functions to parse Golang's source code files and generates a Golang's type representation statically.
Index ¶
Constants ¶
const ( // ChanTypeDirRecv represents a `<-chan` ChanTypeDirRecv = iota // ChanTypeDirSend represents a `chan<-` ChanTypeDirSend // ChanTypeDirSend represents a `chan` ChanTypeDirBoth )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArrayType ¶
type ArrayType struct { // Len represents the length of the array. Len int // Elem represents the element's type of the array. Elem Type }
SliceType represents a Golang's array.
type ChanType ¶
type ChanType struct { // Dir represents the direction of channel. Dir ChanTypeDir // Elem represents the element's type of the channel. Elem Type }
ChanType represents Golang's channel.
type FuncType ¶
type FuncType struct { // Inputs contains the input parameters of the function. Inputs []TypeField // Outputs contains the output parameters of the function. Outputs []TypeField // IsVariadic is true if the final input parameters is a "..." parameter. IsVariadic bool }
FuncType represents a Golang's function.
type InterfaceType ¶
type InterfaceType struct { // Methods contains the methods inside the interface. Methods []InterfaceTypeMethod }
InterfaceType represents a Golang's interface.
func (InterfaceType) Type ¶
func (t InterfaceType) Type() Type
Type converts the InterfaceType to a Type.
type InterfaceTypeMethod ¶
type InterfaceTypeMethod struct { // Name contains the interface's method name. Name string // Func contains the type of the method. Func FuncType }
InterfaceTypeMethod represents a Golang's interface method.
type MapType ¶
type MapType struct { // Key represents the map's key type. Key Type // Elem represents the map's element type. Elem Type }
SliceType represents a Golang's map.
type PrimitiveKind ¶
type PrimitiveKind string
PrimitiveKind represents the type of primitive type.
const ( // PrimitiveKindBool represents Golang's bool PrimitiveKindBool PrimitiveKind = "bool" // PrimitiveKindByte represents Golang's byte PrimitiveKindByte PrimitiveKind = "byte" // PrimitiveKindInt represents Golang's int PrimitiveKindInt PrimitiveKind = "int" // PrimitiveKindInt8 represents Golang's int8 PrimitiveKindInt8 PrimitiveKind = "int8" // PrimitiveKindInt16 represents Golang's int16 PrimitiveKindInt16 PrimitiveKind = "int16" // PrimitiveKindInt32 represents Golang's int32 PrimitiveKindInt32 PrimitiveKind = "int32" // PrimitiveKindInt64 represents Golang's int64 PrimitiveKindInt64 PrimitiveKind = "int64" // PrimitiveKindUint represents Golang's uint PrimitiveKindUint PrimitiveKind = "uint" // PrimitiveKindUint8 represents Golang's uint8 PrimitiveKindUint8 PrimitiveKind = "uint8" // PrimitiveKindUint16 represents Golang's uint16 PrimitiveKindUint16 PrimitiveKind = "uint16" // PrimitiveKindUint32 represents Golang's uint32 PrimitiveKindUint32 PrimitiveKind = "uint32" // PrimitiveKindUint64 represents Golang's uint64 PrimitiveKindUint64 PrimitiveKind = "uint64" // PrimitiveKindUintptr represents Golang's uintptr PrimitiveKindUintptr PrimitiveKind = "uintptr" // PrimitiveKindFloat32 represents Golang's float32 PrimitiveKindFloat32 PrimitiveKind = "float32" // PrimitiveKindFloat64 represents Golang's float64 PrimitiveKindFloat64 PrimitiveKind = "float64" // PrimitiveKindComplex64 represents Golang's complex64 PrimitiveKindComplex64 PrimitiveKind = "complex64" // PrimiteKindComplex128 Golang's complex128 PrimitiveKindComplex128 PrimitiveKind = "complex128" // PrimitiveKindString represents Golang's string PrimitiveKindString PrimitiveKind = "string" // PrimitiveKindError represents Golang's error PrimitiveKindError PrimitiveKind = "error" )
type PrimitiveType ¶
type PrimitiveType struct { // Kind contains the kind of represented primitive. Kind PrimitiveKind }
PrimitiveType represents bool, byte, int, int8, int16, int64, uint, uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128, string, and error.
func (PrimitiveType) Type ¶
func (t PrimitiveType) Type() Type
Type converts the PrimitiveType to a Type.
type PtrType ¶
type PtrType struct { // Elem represents the type pointed by the pointer. Elem Type }
SliceType represents a Golang's pointer.
type QualType ¶
type QualType struct { // Package contains a defined type's package path, that is, the import path // that uniquely identifies the package, such as "encoding/base64". Package string // Name contains the type's name inside the package. // The combination of Package and Name uniquely indentifies a Golang's type. Name string }
QualType represents a pair of Golang's type identified by package path and the type's name within it's package.
type SliceType ¶
type SliceType struct { // Elem represents the element's type of the slice. Elem Type }
SliceType represents a Golang's slice.
type StructType ¶
type StructType struct { // Fields contains all the fields inside the struct. Fields []TypeField }
StructType represents Golang's struct.
type Type ¶
type Type struct { // PrimitiveType represents bool, byte, int, int8, int16, int64, uint, uint16, uint32, uint64, uintptr, float32, // float64, complex64, complex128, string, and error. PrimitiveType *PrimitiveType // QualType represents a pair of Golang's type identified by package path and the type's name within it's package. QualType *QualType // ChanType represents Golang's channel. ChanType *ChanType // SliceType represents Golang's slice. SliceType *SliceType // PtrType represents Golang's pointer. PtrType *PtrType // ArrayType represents Golang's array. ArrayType *ArrayType // MapType represents Golang's map. MapType *MapType // FuncType represents Golang's function. FuncType *FuncType // StructType represents Golang's struct. StructType *StructType // InterfaceType represents Golang's interface. InterfaceType *InterfaceType }
Type is the representation of a Golang static type. Type is similar to Golang's `reflect.Type` with a little difference. In contrast to `reflect.Type`, Type is not constructed on runtime. Type is generated by reading Golang's source files and parse the abstract syntax tree. Note that because Type is generated statically, it doesn't contain the internal representation of Golang's interface.
There are 10 kind of Golang's type supported:
- PrimitiveType: represents bool, byte, int, int8, int16, int64, uint, uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128, string, and error.
- QualType: represents a pair of Golang's type identified by package path and the type's name within it's package.
- ChanType: represents Golang's channel.
- SliceType: represents Golang's slice.
- PtrType: represents Golang's pointer.
- ArrayType: represents Golang's array.
- MapType: represents Golang's map.
- FuncType: represents Golang's function.
- StructType: represents Golang's struct.
- InterfaceType: represents Golang's interface.
Type contains a bunch of pointers which represents the information of each type. There is only one non-null pointer inside Type. For example, if the Type represents a Golang's map, the `MapType` field will be a non-null pointer and the others fields will be null pointer.
func GenerateTypesFromSpecs ¶
GenerateTypesFromSpecs find and parses Golang's source code to generate the `Type`s specified by the `typeSpecs`.
func (Type) IsInterface ¶
IsInterface returns true if the Type is a InterfaceType.
func (Type) IsPrimitive ¶
IsPrimitive returns true if the Type is a PrimitiveType.
type TypeField ¶
type TypeField struct { // Name represents the struct's field name/function's input parameter name/function's output parameter name. Name string // Type represents the type of the field/parameter. Type Type }
TypeField can represent several things:
- A single field inside a Golang's struct.
- A single input parameter of a Golang's function/method.
- A single output parameter of a Golang's function/method.
type TypeSpec ¶
type TypeSpec struct { // PackagePath contains a defined type's package path, that is, the import path // that uniquely identifies the package, such as "encoding/base64". PackagePath string // Name contains the type's name inside the package. Name string }
TypeSpec represents a combination of package path and the type's name which can uniquely identified Golang's type. TypeSpec is used as a query to `gotype`.