types

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Implements

func Implements(t Type, interfaceType *Interface) bool

Implements checks if the type t implements interface 'interfaceType'.

func IsEmptyInterface

func IsEmptyInterface(tp Type) bool

IsEmptyInterface checks if the input type is an empty interface.

Types

type Alias

type Alias struct {
	Comment   string
	Pkg       *Package
	AliasName string
	Type      Type
	Methods   []Function
}

Alias is the type that represents wrapped and named another type. I.e.: 'type Custom int' would be an Alias over BuiltIn(int) type.

func AliasOf

func AliasOf(pkg *Package, name string, aliasType Type) (*Alias, error)

AliasOf creates an Alias of given aliasType at given package with given name.

func (*Alias) Elem

func (a *Alias) Elem() Type

Elem implements Type interface.

func (*Alias) Equal

func (a *Alias) Equal(another Type) bool

Equal implements Type interface.

func (*Alias) FullName

func (a *Alias) FullName() string

FullName implements Type interface.

func (*Alias) Implements added in v0.1.0

func (a *Alias) Implements(interfaceType *Interface, isPointer bool) bool

Implements checks if the alias types implements provided interface. The argument isPointer states if given the pointer to alias or an alias by itself implements given interface.

func (*Alias) Kind

func (a *Alias) Kind() Kind

Kind implements Type interface.

func (*Alias) Name

func (a *Alias) Name(identified bool, packageContext string) string

Name implements Type interface.

func (*Alias) Package

func (a *Alias) Package() *Package

Package implements Type interface.

func (Alias) String

func (a Alias) String() string

KindString implements Type interface.

func (*Alias) Zero

func (a *Alias) Zero(identified bool, packageContext string) string

Zero implements Type interface.

type Array

type Array struct {
	ArrayKind Kind // Could be either KindSlice or KindArray
	Type      Type
	ArraySize int
}

Array is the array or slice representing type.

func ArrayOf

func ArrayOf(inner Type, size int) *Array

ArrayOf creates the KindArray of given size with inner type.

func SliceOf

func SliceOf(inner Type) *Array

SliceOf creates the KindSlice of given inner types.

func (*Array) Elem

func (a *Array) Elem() Type

Elem implements Type interface.

func (*Array) Equal

func (a *Array) Equal(another Type) bool

Equal implements Type interface.

func (*Array) FullName

func (a *Array) FullName() string

FullName implements Type interface.

func (*Array) Kind

func (a *Array) Kind() Kind

Kind implements Type interface.

func (*Array) Name

func (a *Array) Name(identifier bool, packageContext string) string

Name implements Type interface.

func (Array) String

func (a Array) String() string

KindString implements Type interface.

func (*Array) Zero

func (a *Array) Zero(identified bool, packageContext string) string

Zero implements Type interface.

type BuiltInType

type BuiltInType struct {
	BuiltInKind Kind
}

BuiltInType is the built in type definition.

func (*BuiltInType) Elem

func (b *BuiltInType) Elem() Type

Elem implements Type interface.

func (*BuiltInType) Equal

func (b *BuiltInType) Equal(another Type) bool

Equal checks if given built in type is equal to another Type.

func (*BuiltInType) FullName

func (b *BuiltInType) FullName() string

FullName implements Type interface.

func (*BuiltInType) Kind

func (b *BuiltInType) Kind() Kind

Kind implements Type interface.

func (*BuiltInType) Name

func (b *BuiltInType) Name(_ bool, _ string) string

Name implements Type interface.

func (BuiltInType) String

func (b BuiltInType) String() string

KindString implements Type interface.

func (BuiltInType) Zero

func (b BuiltInType) Zero(_ bool, _ string) string

Zero implements Type interface.

type Chan

type Chan struct {
	Type Type
	Dir  ChanDir
}

Chan is the type representing channel.

func ChanOf

func ChanOf(dir ChanDir, chanType Type) *Chan

ChanOf creates the channel of given type with given direction.

func (*Chan) Elem

func (c *Chan) Elem() Type

Elem gets the channel element type.

func (*Chan) Equal

func (c *Chan) Equal(another Type) bool

Equal implements Type interface.

func (*Chan) FullName

func (c *Chan) FullName() string

FullName implements Type interface.

func (*Chan) Kind

func (c *Chan) Kind() Kind

Kind gets the kind of the type.

func (*Chan) Name

func (c *Chan) Name(identified bool, packageContext string) string

Name implements Type interface.

func (Chan) String

func (c Chan) String() string

KindString implements fmt.Stringer interface.

func (*Chan) Zero

func (c *Chan) Zero(_ bool, _ string) string

Zero implements Type interface.

type ChanDir

type ChanDir int

A ChanDir value indicates a channel direction.

const (
	SendRecv ChanDir = iota
	SendOnly
	RecvOnly
)

The direction of a channel is indicated by one of these constants.

func (ChanDir) String added in v0.1.7

func (c ChanDir) String() string

type Declaration

type Declaration struct {
	Comment  string
	Name     string
	Type     Type
	Constant bool
	Val      constant.Value
	Package  *Package
}

Declaration is the variable or constant declaration.

func (Declaration) ConstValue added in v0.1.0

func (d Declaration) ConstValue() interface{}

ConstValue gets the basic value of given constant declaration type. The method panics if the Declaration is not a constant but variable. For selected field kind it returns following Value Types:

  • KindString - string
  • KindBool - bool
  • KindInt, KindInt8, KindInt16, KindInt32, KindInt64: - int
  • KindUint, KindUint8, KindUint16, KindUint32, KindUint64: - uint
  • KindFloat64, KindFloat32: - float64
  • KindComplex64, KindComplex128: - complex128

func (Declaration) String added in v0.1.0

func (d Declaration) String() string

String provides a string visual form of the declaration.

type FuncParam

type FuncParam struct {
	Name string
	Type Type
}

FuncParam is the input/output parameter of functions and methods.

func (FuncParam) String

func (f FuncParam) String() string

KindString implements fmt.Stringer interface.

type Function

type Function struct {
	Comment  string
	Pkg      *Package
	Receiver *Receiver
	FuncName string
	In       []FuncParam
	Out      []FuncParam
	Variadic bool
}

Function is the function type used for getting.

func (*Function) Elem

func (f *Function) Elem() Type

Elem implements Type interface.

func (*Function) Equal

func (f *Function) Equal(another Type) bool

Equal implements Type interface.

func (*Function) FullName

func (f *Function) FullName() string

FullName implements Type interface.

func (*Function) Kind

func (f *Function) Kind() Kind

Kind implements Type interface.

func (Function) Name

func (f Function) Name(identified bool, packageContext string) string

Name implements Type interface.

func (*Function) Package

func (f *Function) Package() *Package

Package implements Type interface.

func (Function) String

func (f Function) String() string

KindString implements Type interface.

func (*Function) Zero

func (f *Function) Zero(_ bool, _ string) string

Zero implements Type interface.

type Interface

type Interface struct {
	Pkg           *Package
	Comment       string
	InterfaceName string
	Methods       []Function
}

Interface is the interface type model definition.

func (*Interface) Elem

func (i *Interface) Elem() Type

Elem implements Type interface.

func (*Interface) Equal

func (i *Interface) Equal(another Type) bool

Equal implements Type interface.

func (Interface) FullName

func (i Interface) FullName() string

FullName implements Type interface.

func (*Interface) Implements

func (i *Interface) Implements(another *Interface) bool

Implements checks if given interface implements another interface.

func (*Interface) IsEmpty

func (i *Interface) IsEmpty() bool

IsEmpty checks if it is an empty interface -> 'interface{}'

func (*Interface) Kind

func (i *Interface) Kind() Kind

Kind implements Type interface.

func (Interface) Name

func (i Interface) Name(identified bool, packageContext string) string

Name implements Type interface.

func (*Interface) Package

func (i *Interface) Package() *Package

Package implements Type interface

func (Interface) String

func (i Interface) String() string

KindString implements Type interface.

func (Interface) Zero

func (i Interface) Zero(_ bool, _ string) string

Zero implements Type interface.

type Kind

type Kind uint

A Kind represents the specific kind of type that a Type represents. The zero Kind is not a valid kind.

const (
	Invalid Kind = iota
	KindBool
	KindInt
	KindInt8
	KindInt16
	KindInt32
	KindInt64
	KindUint
	KindUint8
	KindUint16
	KindUint32
	KindUint64
	KindUintptr
	KindFloat32
	KindFloat64
	KindComplex64
	KindComplex128
	KindString
	KindArray
	KindChan
	KindFunc
	KindInterface
	KindMap
	KindPtr
	KindSlice
	KindStruct
	KindUnsafePointer
)

Enumerated kind representations.

func IsBuiltIn

func IsBuiltIn(kindName string) (Kind, bool)

IsBuiltIn checks if given name is a built in type.

func (Kind) BuiltInName

func (k Kind) BuiltInName() string

BuiltInName gets the name of the builtin kind.

func (Kind) IsBuiltin

func (k Kind) IsBuiltin() bool

IsBuiltin checks if given kind is

func (Kind) IsNumber

func (k Kind) IsNumber() bool

IsNumber checks if given kind is of number (integers, floats)

func (Kind) String

func (k Kind) String() string

KindString implements fmt.Stringer interface.

type Map

type Map struct {
	Key   Type
	Value Type
}

Map is the type wrapper for the standar key value map type.

func MapOf

func MapOf(key, value Type) *Map

MapOf creates a map of given key, value types.

func (*Map) Elem

func (m *Map) Elem() Type

Elem as the map has both the key and value it needs to be dereferenced manually.

func (*Map) Equal

func (m *Map) Equal(another Type) bool

Equal implements Type interface.

func (*Map) FullName

func (m *Map) FullName() string

FullName implements Type interface.

func (*Map) Kind

func (m *Map) Kind() Kind

Kind implements Type interface.

func (*Map) Name

func (m *Map) Name(identified bool, packageContext string) string

Name implements Type interface.

func (Map) String

func (m Map) String() string

KindString implements Type interface.

func (*Map) Zero

func (m *Map) Zero(_ bool, _ string) string

Zero implements Type interface.

type Package

type Package struct {
	Path         string
	Identifier   string
	Interfaces   []*Interface
	Structs      []*Struct
	Functions    []*Function
	Aliases      []*Alias
	Types        map[string]Type
	Declarations map[string]Declaration
	sync.Mutex
}

Package is the golang package reflection container. It contains all interfaces, structs, functions and type wrappers that are located inside of it.

func NewPackage

func NewPackage(pkgPath, identifier string) *Package

NewPackage creates new package definition.

func (*Package) GetAlias

func (p *Package) GetAlias(name string) (*Alias, bool)

GetAlias gets the wrapped type by it's name.

func (*Package) GetFunction

func (p *Package) GetFunction(name string) (*Function, bool)

GetFunction gets the function type by it's name.

func (*Package) GetInterfaceType

func (p *Package) GetInterfaceType(name string) (*Interface, bool)

GetInterfaceType gets the interface by it's name.

func (*Package) GetPkgPath

func (p *Package) GetPkgPath() PkgPath

GetPkgPath gets the PkgPath for given package.

func (*Package) GetStruct

func (p *Package) GetStruct(name string) (*Struct, bool)

GetStruct gets the struct type by it's name.

func (*Package) GetType

func (p *Package) GetType(name string) (Type, bool)

GetType gets concurrently safe package type.

func (*Package) IsStandard

func (p *Package) IsStandard() bool

IsStandard checks if given package is a standard package.

func (*Package) MustFunction

func (p *Package) MustFunction(name string) *Function

MustFunction gets the function declaration with the 'name'. If the function is not found it panics.

func (*Package) MustGetType

func (p *Package) MustGetType(name string) Type

MustGetType get the type with given 'name' from given package. If the type is not found the function panics.

func (*Package) MustStruct

func (p *Package) MustStruct(name string) *Struct

MustStruct gets the struct declaration with the 'name'. If the struct is not found it panics.

func (*Package) NewConstant

func (p *Package) NewConstant(name string, tp Type, value constant.Value) error

NewConstant adds new package constant Declaration.

func (*Package) NewNamedType

func (p *Package) NewNamedType(name string, namedType Type) error

NewNamedType adds new named type to the package definition.

func (*Package) NewVariable

func (p *Package) NewVariable(name string, tp Type) error

NewVariable adds new package variable Declaration.

func (*Package) SetIdentifier

func (p *Package) SetIdentifier(s string)

SetIdentifier sets the package identifier.

func (*Package) SetNamedType

func (p *Package) SetNamedType(name string, tp Type)

SetNamedType sets the type with given name to be stored withing given package.

func (*Package) TypeOf added in v0.1.7

func (p *Package) TypeOf(name string) (Type, bool)

TypeOf gets the package type by its name in a concurrent safe way.

type PackageMap

type PackageMap map[string]*Package

PackageMap is a slice wrapper over Package type.

func (PackageMap) MustGetByPath

func (p PackageMap) MustGetByPath(path string) *Package

func (PackageMap) NewPackage

func (p PackageMap) NewPackage(name, identifier string) (*Package, error)

NewPackage creates new package definition in given package map.

func (PackageMap) PackageByIdentifier

func (p PackageMap) PackageByIdentifier(identifier string) (*Package, bool)

PackageByIdentifier gets the package by provided identifier. If there is more than one package with given identifier The function would return the first matching package.

func (PackageMap) PackageByPath

func (p PackageMap) PackageByPath(path string) (*Package, bool)

PackageByPath gets the package by provided path.

func (*PackageMap) TypeOf

func (p *PackageMap) TypeOf(typeOf string, packageContext *Package) (Type, bool)

TypeOf gets the resulting type for provided 'typeOf'. If the packageContext is defined the values without identifier would be found within given package also.

type Packager

type Packager interface {
	// Package gets the Package for given type.
	Package() *Package
}

Packager is the interface that allows to get Type packages. Only the Types that contains the name stores the Package i.e.: Struct, Interface, Alias, Function.

type PkgPath

type PkgPath string

PkgPath is the string package that contains full package name.

func (PkgPath) FullName

func (p PkgPath) FullName() string

FullName gets the full name of given PkgPath in a string type.

func (PkgPath) Identifier

func (p PkgPath) Identifier() string

Identifier gets package identifier.

func (PkgPath) IsStandard

func (p PkgPath) IsStandard() bool

IsStandard checks if the package is standard.

type Pointer

type Pointer struct {
	PointedType Type
}

Pointer is the type implementation that defines pointer type.

func PointerTo

func PointerTo(pointsTo Type) *Pointer

PointerTo creates a pointer of given type.

func (*Pointer) Elem

func (p *Pointer) Elem() Type

Elem implements Type interface.

func (*Pointer) Equal

func (p *Pointer) Equal(another Type) bool

Equal implements Type interface.

func (*Pointer) FullName

func (p *Pointer) FullName() string

FullName implements Type interface.

func (*Pointer) Kind

func (p *Pointer) Kind() Kind

Kind implements Type interface.

func (*Pointer) Name

func (p *Pointer) Name(identified bool, pkgContext string) string

Name implements Type interface.

func (Pointer) String

func (p Pointer) String() string

KindString implements Type interface.

func (*Pointer) Zero

func (p *Pointer) Zero(_ bool, _ string) string

Zero implements Type interface.

type Receiver

type Receiver struct {
	Name string
	Type Type
}

Receiver is the function (method) receiver with the name and a pointer flag.

func (*Receiver) IsPointer

func (r *Receiver) IsPointer() bool

IsPointer checks if the receiver type is a pointer.

func (*Receiver) String

func (r *Receiver) String() string

type Struct

type Struct struct {
	Pkg      *Package
	Comment  string
	TypeName string
	Fields   []StructField
	Methods  []Function
}

Struct is the struct type reflection.

func (*Struct) Elem

func (s *Struct) Elem() Type

Elem implements Type interface.

func (*Struct) Equal

func (s *Struct) Equal(another Type) bool

Equal implements Type interface.

func (*Struct) FullName

func (s *Struct) FullName() string

FullName implements Type interface.

func (*Struct) Implements

func (s *Struct) Implements(interfaceType *Interface, pointer bool) bool

Implements checks if given structure implements provided interface.

func (*Struct) Kind

func (s *Struct) Kind() Kind

Kind implements Type interface.

func (*Struct) Name

func (s *Struct) Name(identifier bool, packageContext string) string

Name implements Type interface.

func (*Struct) Package

func (s *Struct) Package() *Package

Package implements Type interface.

func (*Struct) String

func (s *Struct) String() string

KindString implements Type interface.

func (*Struct) Zero

func (s *Struct) Zero(identified bool, packageContext string) string

Zero implements Type interface.

type StructField

type StructField struct {
	Name      string
	Comment   string
	Type      Type
	Tag       StructTag
	Index     []int
	Embedded  bool
	Anonymous bool
}

StructField is a structure field model.

func (StructField) String

func (s StructField) String() string

KindString implements fmt.Stringer interface.

type StructTag

type StructTag string

A StructTag is the tag string in a struct field.

By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' '), quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.

func (StructTag) Get

func (tag StructTag) Get(key string) string

Get returns the value associated with key in the tag string. If there is no such key in the tag, Get returns the empty string. If the tag does not have the conventional format, the value returned by Get is unspecified. To determine whether a tag is explicitly set to the empty string, use Lookup.

func (StructTag) Lookup

func (tag StructTag) Lookup(key string) (value string, ok bool)

Lookup returns the value associated with key in the tag string. If the key is present in the tag the value (which may be empty) is returned. Otherwise the returned value will be the empty string. The ok return value reports whether the value was explicitly set in the tag string. If the tag does not have the conventional format, the value returned by Lookup is unspecified.

func (StructTag) Split

func (tag StructTag) Split() (tuples StructTagTuples)

Split splits up the struct tag into key, value tuples.

type StructTagTuple

type StructTagTuple struct {
	Key, Value string
}

StructTagTuple is a tuple key,value for the struct tag.

type StructTagTuples

type StructTagTuples []StructTagTuple

StructTagTuples is the slice alias over the the structTag key, value tuples. It is used to recreate the StructTag.

func (StructTagTuples) Join

func (s StructTagTuples) Join() StructTag

Join joins the structTag tuples and creates a single StructTag.

type Type

type Type interface {
	// Name gets the type name with or without package identifier.
	// An optional packageContext parameter defines the name of the package (full package name) that is expected to be
	// within given context of search. This could be used to get the chain of names with respect to some package.
	// Example:
	//	Developer wants to generate some additional method for the type 'X' within package 'my.com/testing/pkg'.
	//	In order to generate valid names for the imported types the identity needs to be set to 'true'.
	//	But current package context ('my.com/testing/pkg') should not be used be prefixed with the identifier.
	//	Thus, an optional 'packageContext' parameter needs to be set to 'my.com/testing/pkg'.
	Name(identified bool, packageContext string) string
	// FullName gets the full name of given type with the full package name and a type.
	FullName() string
	// Kind gets the Kind of given type.
	Kind() Kind
	// Elem gets the wrapped, pointed, base of
	Elem() Type
	// String gets the full name string representation of given type.
	String() string
	// Zero gets zero value string of given type.
	Zero(identified bool, packageContext string) string
	// Equal checks if the types matches exact.
	Equal(another Type) bool
}

Type is the interface used by all golang type reflections in package.

var (
	Error         Type
	Byte          Type
	Rune          Type
	Bool          Type
	Int           Type
	Int8          Type
	Int16         Type
	Int32         Type
	Int64         Type
	Uint          Type
	Uint8         Type
	Uint16        Type
	Uint32        Type
	Uint64        Type
	Uintptr       Type
	Float32       Type
	Float64       Type
	Complex64     Type
	Complex128    Type
	String        Type
	UnsafePointer Type
)

Builtin types definitions.

func BuiltInOf

func BuiltInOf(x interface{}) Type

BuiltInOf gets the built in of specific kind.

func Dereference

func Dereference(t Type) Type

Dereference is getting Type dereferenced basic value. If the value is basic returns nil.

func GetBuiltInType

func GetBuiltInType(name string) (Type, bool)

GetBuiltInType gets the built in type with given name.

func MustGetBuiltInType

func MustGetBuiltInType(name string) Type

MustGetBuiltInType gets the built in type with given name. If the type is not found the function panics.

Jump to

Keyboard shortcuts

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