parser

package
v0.9.4 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package parser is a simplified abstraction over go/parser, tailored for the go:generate resource/generation tool.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadPackage added in v0.1.14

func LoadPackage(packagePattern string) (*packages.Package, error)

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

func LoadPackages(packagePatterns ...string) (map[string]*packages.Package, error)

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

func (f *Field) AddError(msg string)

AddError stores an error message to be printed in the parent struct's PrintErrors method

func (*Field) AsStruct added in v0.2.6

func (f *Field) AsStruct() *Struct

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

func (f *Field) Comments() string

Comments returns the godoc comment text on the field's declaration.

func (*Field) DerefResolvedType added in v0.2.1

func (f *Field) DerefResolvedType() string

DerefResolvedType returns this Field's unqualified type if it's local, or its qualified type otherwise.

func (*Field) HasTag added in v0.1.13

func (f *Field) HasTag(key string) bool

HasTag returns true if this field has a matching struct tag.

func (*Field) IsLocalType

func (f *Field) IsLocalType() bool

IsLocalType returns true if this Field's type originates from the same package its parent struct is defined in.

func (*Field) LookupTag

func (f *Field) LookupTag(key string) (string, bool)

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

func (f *Field) OriginType() string

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

func (f *Field) ResolvedType() string

ResolvedType returns this Field's unqualified type if it's local, or its qualified type otherwise.

func (*Field) String

func (f *Field) String() string

func (*Field) TypeArgs added in v0.1.14

func (f *Field) TypeArgs() string

TypeArgs returns any type arguments if this field's type is a generic instantiation. e.g. ccc.Foo[bool] -> bool, ccc.Foo[ccc.UUID] -> ccc.UUID

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

type NamedType struct {
	TypeInfo
	Comments string
}

NamedType is any distinct named type e.g. type MyNamedType string, type MyOtherNamedType ccc.UUID

type Package added in v0.2.3

type Package struct {
	Structs    []*Struct
	NamedTypes []*NamedType
}

Package holds a slice of each high level type used in resource generation.

func ParsePackage added in v0.2.3

func ParsePackage(pkg *packages.Package) *Package

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

func FilterStructsByInterface(pStructs []*Struct, interfaceNames []string) []*Struct

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

func (s *Struct) Comments() string

Comments returns the godoc comment text on the struct's type declaration

func (*Struct) Fields

func (s *Struct) Fields() []*Field

Fields returns a slice of the fields belonging to the struct.

func (*Struct) HasErrors added in v0.4.3

func (s *Struct) HasErrors() bool

HasErrors returns true if a field error has been stored by AddFieldError

func (*Struct) HasMethod added in v0.1.14

func (s *Struct) HasMethod(methodName string) bool

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

func (s *Struct) Implements(interfaceName string) bool

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

func (s *Struct) NumFields() int

NumFields is the number of fields in the struct's type declaration.

func (*Struct) Pos added in v0.8.0

func (s *Struct) Pos() token.Pos

Pos returns the position of the struct keyword in its fileset.

func (*Struct) PrintErrors added in v0.4.3

func (s *Struct) PrintErrors() string

PrintErrors pretty-prints the struct annotated with field errors if any have been stored with AddFieldError.

func (*Struct) String

func (s *Struct) String() string

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

func (t *TypeInfo) DerefType() string

DerefType returns non-pointer qualified type. e.g. *ccc.UUID -> ccc.UUID

func (*TypeInfo) DerefUnqualifiedType added in v0.2.1

func (t *TypeInfo) DerefUnqualifiedType() string

DerefUnqualifiedType is the non-pointer type name without package prefix. e.g. *ccc.UUID -> UUID

func (*TypeInfo) IsIterable added in v0.0.18

func (t *TypeInfo) IsIterable() bool

IsIterable returns true if type is slice or array

func (*TypeInfo) IsPointer added in v0.0.18

func (t *TypeInfo) IsPointer() bool

IsPointer returns true if the declaration is a pointer

func (*TypeInfo) Name added in v0.0.18

func (t *TypeInfo) Name() string

Name is the type's local object name.

func (*TypeInfo) Type added in v0.0.18

func (t *TypeInfo) Type() string

Type is the qualified type name. e.g. ccc.UUID, []ccc.UUID

func (*TypeInfo) TypeName added in v0.0.18

func (t *TypeInfo) TypeName() string

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

func (t *TypeInfo) UnqualifiedType() string

UnqualifiedType is the type name without package prefix. e.g. ccc.UUID -> UUID, []ccc.UUID -> []UUID

func (*TypeInfo) UnqualifiedTypeName added in v0.0.18

func (t *TypeInfo) UnqualifiedTypeName() string

UnqualifiedTypeName is the type name without array/slice/pointer or package prefix. e.g. *ccc.UUID -> UUID, []ccc.UUID -> UUID

Directories

Path Synopsis
Package genlang provides parsing for godoc comment annotations
Package genlang provides parsing for godoc comment annotations

Jump to

Keyboard shortcuts

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