Documentation
¶
Overview ¶
Package parser is a simplified abstraction over go/parser, tailored for the go:generate resource/generation tool.
Index ¶
- func LoadPackage(packagePattern string) (*packages.Package, error)
- func LoadPackages(packagePatterns ...string) (map[string]*packages.Package, error)
- type Field
- func (f *Field) AddError(msg string)
- func (f *Field) AsStruct() *Struct
- func (f *Field) Comments() string
- func (f *Field) DerefResolvedType() string
- func (f *Field) HasTag(key string) bool
- func (f *Field) IsLocalType() bool
- func (f *Field) LookupTag(key string) (string, bool)
- func (f *Field) OriginType() string
- func (f *Field) ResolvedType() string
- func (f *Field) String() string
- func (f *Field) TypeArgs() string
- type Interface
- type NamedType
- type Package
- type Struct
- func (s *Struct) Comments() string
- func (s *Struct) Fields() []*Field
- func (s *Struct) HasErrors() bool
- func (s *Struct) HasMethod(methodName string) bool
- func (s *Struct) Implements(interfaceName string) bool
- func (s *Struct) NumFields() int
- func (s *Struct) Pos() token.Pos
- func (s *Struct) PrintErrors() string
- func (s *Struct) String() string
- type TypeInfo
- func (t *TypeInfo) DerefType() string
- func (t *TypeInfo) DerefUnqualifiedType() string
- func (t *TypeInfo) IsIterable() bool
- func (t *TypeInfo) IsPointer() bool
- func (t *TypeInfo) Name() string
- func (t *TypeInfo) Type() string
- func (t *TypeInfo) TypeName() string
- func (t *TypeInfo) UnqualifiedType() string
- func (t *TypeInfo) UnqualifiedTypeName() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadPackage ¶ added in v0.1.14
LoadPackage loads and type checks a single package. Package data contains package name, file names, ast, and types' info. Returns an error if a package pattern does not match any packages, no files are loaded in a matched package, or parsing & typechecking yield any errors. Useful for static type analysis with the types package instead of manually parsing the AST. A good explainer lives here: https://github.com/golang/example/tree/master/gotypes
func LoadPackages ¶
LoadPackages loads and type checks a list of packages. Returns package names mapped to a *packages.Package. Package data contains package name, file names, ast, and types' info. Returns an error if a package pattern does not match any packages, no files are loaded in a matched package, or parsing & typechecking yield any errors. Useful for static type analysis with the types package instead of manually parsing the AST. A good explainer lives here: https://github.com/golang/example/tree/master/gotypes
Types ¶
type Field ¶
type Field struct {
TypeInfo
// contains filtered or unexported fields
}
Field is an abstraction combining types.Var and ast.Field for simpler parsing.
func (*Field) AddError ¶ added in v0.8.0
AddError stores an error message to be printed in the parent struct's PrintErrors method
func (*Field) AsStruct ¶ added in v0.2.6
AsStruct converts this Field to a Struct. Returns nil if the Field's type is not a struct type.
func (*Field) Comments ¶ added in v0.1.14
Comments returns the godoc comment text on the field's declaration.
func (*Field) DerefResolvedType ¶ added in v0.2.1
DerefResolvedType returns this Field's unqualified type if it's local, or its qualified type otherwise.
func (*Field) HasTag ¶ added in v0.1.13
HasTag returns true if this field has a matching struct tag.
func (*Field) IsLocalType ¶
IsLocalType returns true if this Field's type originates from the same package its parent struct is defined in.
func (*Field) LookupTag ¶
LookupTag returns a struct tag's value and whether or not the struct tag exists on this field.
func (*Field) OriginType ¶ added in v0.1.14
OriginType returns the origin type if this Field's type is a generic instantiation. e.g. ccc.Foo[bool] returns ccc.Foo
func (*Field) ResolvedType ¶ added in v0.1.11
ResolvedType returns this Field's unqualified type if it's local, or its qualified type otherwise.
type Interface ¶ added in v0.1.14
type Interface struct {
Name string
// contains filtered or unexported fields
}
Interface is an abstraction over types.Interface
type NamedType ¶ added in v0.2.3
NamedType is any distinct named type e.g. type MyNamedType string, type MyOtherNamedType ccc.UUID
type Package ¶ added in v0.2.3
Package holds a slice of each high level type used in resource generation.
func ParsePackage ¶ added in v0.2.3
ParsePackage parses a package's ast and type info and returns data about structs and named types necessary for resource generation. We can iterate over the declarations at the package level a single time to extract all the data necessary for generation. Any new data that needs to be added to the Struct type can be extracted here.
type Struct ¶
type Struct struct {
*TypeInfo
// contains filtered or unexported fields
}
Struct is an abstraction combining types.Struct and ast.StructType for simpler parsing.
func FilterStructsByInterface ¶ added in v0.1.14
FilterStructsByInterface returns a filtered slice of structs that satisfy one or more from the list of interface names.
func (*Struct) Comments ¶ added in v0.1.14
Comments returns the godoc comment text on the struct's type declaration
func (*Struct) HasErrors ¶ added in v0.4.3
HasErrors returns true if a field error has been stored by AddFieldError
func (*Struct) HasMethod ¶ added in v0.1.14
HasMethod returns true if the method name matches a name in the set of methods belonging to the struct.
func (*Struct) Implements ¶ added in v0.0.18
Implements returns true if the interface's name matches a name in the set of interfaces the Struct satisfies.
func (*Struct) NumFields ¶ added in v0.1.14
NumFields is the number of fields in the struct's type declaration.
func (*Struct) PrintErrors ¶ added in v0.4.3
PrintErrors pretty-prints the struct annotated with field errors if any have been stored with AddFieldError.
type TypeInfo ¶ added in v0.0.18
type TypeInfo struct {
// contains filtered or unexported fields
}
TypeInfo provides convience methods over a go/types' Object.
func (*TypeInfo) DerefType ¶ added in v0.2.1
DerefType returns non-pointer qualified type. e.g. *ccc.UUID -> ccc.UUID
func (*TypeInfo) DerefUnqualifiedType ¶ added in v0.2.1
DerefUnqualifiedType is the non-pointer type name without package prefix. e.g. *ccc.UUID -> UUID
func (*TypeInfo) IsIterable ¶ added in v0.0.18
IsIterable returns true if type is slice or array
func (*TypeInfo) IsPointer ¶ added in v0.0.18
IsPointer returns true if the declaration is a pointer
func (*TypeInfo) TypeName ¶ added in v0.0.18
TypeName is the qualified type name without array/slice/pointer prefix. e.g. *ccc.UUID -> ccc.UUID, []ccc.UUID -> ccc.UUID
func (*TypeInfo) UnqualifiedType ¶ added in v0.0.18
UnqualifiedType is the type name without package prefix. e.g. ccc.UUID -> UUID, []ccc.UUID -> []UUID
func (*TypeInfo) UnqualifiedTypeName ¶ added in v0.0.18
UnqualifiedTypeName is the type name without array/slice/pointer or package prefix. e.g. *ccc.UUID -> UUID, []ccc.UUID -> UUID