gotype

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2021 License: MIT Imports: 15 Imported by: 2

README

GoType

GoType parses Golang's source code files and generates a Golang's type representation statically.

Motivation

GoType is used to manipulate Golang's type without using Golang's reflection. The problem with reflection is we can't get a Golang's type representation which identified by the package path and type's name.

Documentation

Overview

gotype package provides functions to parse Golang's source code files and generates a Golang's type representation statically.

Index

Constants

View Source
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.

func (ArrayType) Type

func (t ArrayType) Type() Type

Type converts the ArrayType to a Type.

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.

func (ChanType) Type

func (t ChanType) Type() Type

Type converts the ChanType to a Type.

type ChanTypeDir

type ChanTypeDir int

ChanTypeDir represents the direction of 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.

func (FuncType) Type

func (t FuncType) Type() Type

Type converts the FuncType to a Type.

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.

func (MapType) Type

func (t MapType) Type() Type

Type converts the MapType to a Type.

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.

func (PtrType) Type

func (t PtrType) Type() Type

Type converts the PtrType to a Type.

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.

func (QualType) Type

func (t QualType) Type() Type

Type converts the QualType to a Type.

type SliceType

type SliceType struct {
	// Elem represents the element's type of the slice.
	Elem Type
}

SliceType represents a Golang's slice.

func (SliceType) Type

func (t SliceType) Type() Type

Type converts the SliceType to a Type.

type StructType

type StructType struct {
	// Fields contains all the fields inside the struct.
	Fields []TypeField
}

StructType represents Golang's struct.

func (StructType) Type

func (t StructType) Type() Type

Type converts the StructType to a Type.

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

func GenerateTypesFromSpecs(typeSpecs ...TypeSpec) ([]Type, error)

GenerateTypesFromSpecs find and parses Golang's source code to generate the `Type`s specified by the `typeSpecs`.

func (Type) IsArray

func (t Type) IsArray() bool

IsArray returns true if the Type is a ArrayType.

func (Type) IsChan

func (t Type) IsChan() bool

IsChan returns true if the Type is a ChanType.

func (Type) IsFunc

func (t Type) IsFunc() bool

IsFunc returns true if the Type is a FuncType.

func (Type) IsInterface

func (t Type) IsInterface() bool

IsInterface returns true if the Type is a InterfaceType.

func (Type) IsMap

func (t Type) IsMap() bool

IsMap returns true if the Type is a MapType.

func (Type) IsPrimitive

func (t Type) IsPrimitive() bool

IsPrimitive returns true if the Type is a PrimitiveType.

func (Type) IsPtr

func (t Type) IsPtr() bool

IsPtr returns true if the Type is a PtrType.

func (Type) IsQual

func (t Type) IsQual() bool

IsQual returns true if the Type is a QualType.

func (Type) IsSlice

func (t Type) IsSlice() bool

IsSlice returns true if the Type is a SliceType.

func (Type) IsStruct

func (t Type) IsStruct() bool

IsStruct returns true if the Type is a StructType.

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`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL