types

package
v1.3.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2016 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package types contains go type information, packaged in a way that makes auto-generation convenient, whether by template or straight go functions.

Package types contains go type information, packaged in a way that makes auto-generation convenient, whether by template or straight go functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	String = &Type{
		Name: Name{Name: "string"},
		Kind: Builtin,
	}
	Int64 = &Type{
		Name: Name{Name: "int64"},
		Kind: Builtin,
	}
	Int32 = &Type{
		Name: Name{Name: "int32"},
		Kind: Builtin,
	}
	Int16 = &Type{
		Name: Name{Name: "int16"},
		Kind: Builtin,
	}
	Int = &Type{
		Name: Name{Name: "int"},
		Kind: Builtin,
	}
	Uint64 = &Type{
		Name: Name{Name: "uint64"},
		Kind: Builtin,
	}
	Uint32 = &Type{
		Name: Name{Name: "uint32"},
		Kind: Builtin,
	}
	Uint16 = &Type{
		Name: Name{Name: "uint16"},
		Kind: Builtin,
	}
	Uint = &Type{
		Name: Name{Name: "uint"},
		Kind: Builtin,
	}
	Uintptr = &Type{
		Name: Name{Name: "uintptr"},
		Kind: Builtin,
	}
	Float64 = &Type{
		Name: Name{Name: "float64"},
		Kind: Builtin,
	}
	Float32 = &Type{
		Name: Name{Name: "float32"},
		Kind: Builtin,
	}
	Float = &Type{
		Name: Name{Name: "float"},
		Kind: Builtin,
	}
	Bool = &Type{
		Name: Name{Name: "bool"},
		Kind: Builtin,
	}
	Byte = &Type{
		Name: Name{Name: "byte"},
		Kind: Builtin,
	}
)

Built in types.

Functions

func ExtractCommentTags

func ExtractCommentTags(marker, allLines string) map[string]string

ExtractCommentTags parses comments for lines of the form:

'marker'+"key1=value1,key2=value2".

Values are optional; 'true' is the default. If a key is set multiple times, the last one wins.

Example: if you pass "+" for 'marker', and the following two lines are in the comments:

+foo=value1,bar
+foo=value2,baz="frobber"

Then this function will return:

map[string]string{"foo":"value2", "bar": "true", "baz": "frobber"}

TODO: Basically we need to define a standard way of giving instructions to autogenerators in the comments of a type. This is a first iteration of that. TODO: allow multiple values per key?

func IsInteger added in v1.3.0

func IsInteger(t *Type) bool

Types

type Kind

type Kind string

The possible classes of types.

const (
	// Builtin is a primitive, like bool, string, int.
	Builtin Kind = "Builtin"
	Struct  Kind = "Struct"
	Map     Kind = "Map"
	Slice   Kind = "Slice"
	Pointer Kind = "Pointer"

	// Alias is an alias of another type, e.g. in:
	//   type Foo string
	//   type Bar Foo
	// Bar is an alias of Foo.
	//
	// In the real go type system, Foo is a "Named" string; but to simplify
	// generation, this type system will just say that Foo *is* a builtin.
	// We then need "Alias" as a way for us to say that Bar *is* a Foo.
	Alias Kind = "Alias"

	// Interface is any type that could have differing types at run time.
	Interface Kind = "Interface"

	// The remaining types are included for completeness, but are not well
	// supported.
	Array Kind = "Array" // Array is just like slice, but has a fixed length.
	Chan  Kind = "Chan"
	Func  Kind = "Func"

	// DeclarationOf is different from other Kinds; it indicates that instead of
	// representing an actual Type, the type is a declaration of an instance of
	// a type. E.g., a top-level function, variable, or constant. See the
	// comment for Type.Name for more detail.
	DeclarationOf Kind = "DeclarationOf"
	Unknown       Kind = ""
	Unsupported   Kind = "Unsupported"

	// Protobuf is protobuf type.
	Protobuf Kind = "Protobuf"
)

type Member

type Member struct {
	// The name of the member.
	Name string

	// If the member is embedded (anonymous) this will be true, and the
	// Name will be the type name.
	Embedded bool

	// If there are comment lines immediately before the member in the type
	// definition, they will be recorded here.
	CommentLines string

	// If there are tags along with this member, they will be saved here.
	Tags string

	// The type of this member.
	Type *Type
}

A single struct member

func FlattenMembers

func FlattenMembers(m []Member) []Member

FlattenMembers recursively takes any embedded members and puts them in the top level, correctly hiding them if the top level hides them. There must not be a cycle-- that implies infinite members.

This is useful for e.g. computing all the valid keys in a json struct, properly considering any configuration of embedded structs.

func (Member) String

func (m Member) String() string

String returns the name and type of the member.

type Name

type Name struct {
	// Empty if embedded or builtin. This is the package path unless Path is specified.
	Package string
	// The type name.
	Name string
	// An optional location of the type definition for languages that can have disjoint
	// packages and paths.
	Path string
}

A type name may have a package qualifier.

func (Name) String

func (n Name) String() string

String returns the name formatted as a string.

type Package

type Package struct {
	// Canonical name of this package-- its path.
	Path string

	// Short name of this package; the name that appears in the
	// 'package x' line.
	Name string

	// Types within this package, indexed by their name (*not* including
	// package name).
	Types map[string]*Type

	// Functions within this package, indexed by their name (*not* including
	// package name).
	Functions map[string]*Type

	// Global variables within this package, indexed by their name (*not* including
	// package name).
	Variables map[string]*Type

	// Packages imported by this package, indexed by (canonicalized)
	// package path.
	Imports map[string]*Package
}

Package holds package-level information. Fields are public, as everything in this package, to enable consumption by templates (for example). But it is strongly encouraged for code to build by using the provided functions.

func (*Package) Function

func (p *Package) Function(funcName string) *Type

Get (or add) the given function. If a function is added, it's the caller's responsibility to finish construction of the function by setting Underlying to the correct type.

func (*Package) Has

func (p *Package) Has(name string) bool

Has returns true if the given name references a type known to this package.

func (*Package) HasImport

func (p *Package) HasImport(packageName string) bool

HasImport returns true if p imports packageName. Package names include the package directory.

func (*Package) Type

func (p *Package) Type(typeName string) *Type

Get (or add) the given type

func (*Package) Variable

func (p *Package) Variable(varName string) *Type

Get (or add) the given varaible. If a variable is added, it's the caller's responsibility to finish construction of the variable by setting Underlying to the correct type.

type Signature

type Signature struct {

	// If a method of some type, this is the type it's a member of.
	Receiver   *Type
	Parameters []*Type
	Results    []*Type

	// True if the last in parameter is of the form ...T.
	Variadic bool

	// If there are comment lines immediately before this
	// signature/method/function declaration, they will be recorded here.
	CommentLines string
}

Signature is a function's signature.

type Type

type Type struct {
	// There are two general categories of types, those explicitly named
	// and those anonymous. Named ones will have a non-empty package in the
	// name field.
	//
	// An exception: If Kind == DeclarationOf, then this name is the name of a
	// top-level function, variable, or const, and the type can be found in Underlying.
	// We do this to allow the naming system to work against these objects, even
	// though they aren't strictly speaking types.
	Name Name

	// The general kind of this type.
	Kind Kind

	// If there are comment lines immediately before the type definition,
	// they will be recorded here.
	CommentLines string

	// If there are comment lines preceding the `CommentLines`, they will be
	// recorded here. There are two cases:
	// ---
	// SecondClosestCommentLines
	// a blank line
	// CommentLines
	// type definition
	// ---
	//
	// or
	// ---
	// SecondClosestCommentLines
	// a blank line
	// type definition
	// ---
	SecondClosestCommentLines string

	// If Kind == Struct
	Members []Member

	// If Kind == Map, Slice, Pointer, or Chan
	Elem *Type

	// If Kind == Map, this is the map's key type.
	Key *Type

	// If Kind == Alias, this is the underlying type.
	// If Kind == DeclarationOf, this is the type of the declaration.
	Underlying *Type

	// If Kind == Interface, this is the list of all required functions.
	// Otherwise, if this is a named type, this is the list of methods that
	// type has. (All elements will have Kind=="Func")
	Methods []*Type

	// If Kind == func, this is the signature of the function.
	Signature *Signature
}

Type represents a subset of possible go types.

func (*Type) IsAssignable

func (t *Type) IsAssignable() bool

IsAssignable returns whether the type is assignable.

func (*Type) String

func (t *Type) String() string

String returns the name of the type.

type Universe

type Universe map[string]*Package

Universe is a map of all packages. The key is the package name, but you should use Get() or Package() instead of direct access.

func (Universe) AddImports

func (u Universe) AddImports(packagePath string, importPaths ...string)

AddImports registers import lines for packageName. May be called multiple times. You are responsible for canonicalizing all package paths.

func (Universe) Function

func (u Universe) Function(n Name) *Type

Function returns the canonical function for the given fully-qualified name. If a non-existing function is requested, u will create (a marker for) it. If a marker is created, it's the caller's responsibility to finish construction of the function by setting Underlying to the correct type.

func (Universe) Package

func (u Universe) Package(packagePath string) *Package

Get (create if needed) the package.

func (Universe) Type

func (u Universe) Type(n Name) *Type

Get returns the canonical type for the given fully-qualified name. Builtin types will always be found, even if they haven't been explicitly added to the map. If a non-existing type is requested, u will create (a marker for) it.

func (Universe) Variable

func (u Universe) Variable(n Name) *Type

Variable returns the canonical variable for the given fully-qualified name. If a non-existing variable is requested, u will create (a marker for) it. If a marker is created, it's the caller's responsibility to finish construction of the variable by setting Underlying to the correct type.

Jump to

Keyboard shortcuts

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