parse

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package parse provides functionality to parse go files and create representations of code elements suitable for code generation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSimpleType

func IsSimpleType(p ParamType, typeName, packageName string) bool

Types

type ArrayType

type ArrayType struct {
	Type ParamType
}

func (ArrayType) Packages

func (at ArrayType) Packages() []string

type Interface

type Interface struct {
	// Name of the interface
	Name string
	// Package the interface is defined in
	Package string
	// Methods of the interface
	Methods []Method
	// Comments belonging to this interface, i.e. the comments directly above the type definition in the source code.
	Comments []string

	// File (path) this interface is defined in
	File string
}

Interface represents an interface in a more compact way than a tree from the "go/ast" package, which makes it easier to work with for code generation.

func ParseDir

func ParseDir(path string, module Module) ([]Interface, error)

Recursively searches the directory given by path and parses any interfaces.

type MapType

type MapType struct {
	KeyType   ParamType
	ValueType ParamType
}

Represents a map type recursively.

func (MapType) Packages

func (t MapType) Packages() []string

type Method

type Method struct {
	// Name of the method
	Name string
	// Parameters/Arguments
	Params []Param
	// Return values
	Returns []Param
	// Comments belonging to this method, i.e. the comments directly above the method definition in the source code.
	Comments []string
}

Method represents a method of an interface.

type Module

type Module struct {
	// root path of the module in the filesystem
	Path string
	// name of the module, e.g. "github.com/xyz/abc"
	Name string
}

Module provides functions to convert relative package names and file paths to absolute ones, based on a module.

func NewModuleFromDir

func NewModuleFromDir(dir string) (Module, error)

Will find the module the given directory belongs to by searching for a go.mod file in the directory and its parents.

func (Module) FileName

func (m Module) FileName(packageName, fileName string) string

Absolute file path for the file in the given package.

func (Module) FullPackagePath

func (m Module) FullPackagePath(p string) string

Returns the full package path for the given relative package, i.e. the module name is added as a prefix.

func (Module) PackagePathFromFilePath

func (m Module) PackagePathFromFilePath(filePath string) (string, error)

Returns the package path from a given file path. E.g. if the root module path is /abc/xyz/somemodule, the module name is somemodule and the file path is "/abc/xyz/somemodule/internal/xyz/file.go" then the resulting package path would be "somemodule/internal/xyz". Note that for this to work the given file path must have the root path of the module as a prefix

func (Module) PackagePathWithoutModule

func (m Module) PackagePathWithoutModule(p string) string

Returns a package path without the module prefix. E.g. when called with "example.com/xyz/abc" on a module with name "example.com/xyz" will return "abc".

type Param

type Param struct {
	// Name of the parameter, e.g. "ctx" for the parameter definition "ctx context.Context".
	// Can be empty e.g. for unnamed return values.
	Name string
	// Type of the parameter
	Type ParamType
}

Param represents a method parameter or return value

type ParamType

type ParamType interface {
	// Returns a list of all the packages required by this type.
	// E.g. a type map[context.Context]*http.Request requires the pacakges "context" and "http".
	Packages() []string
}

Represents the type of a parameter. Note: this does not support some types like function types, channels and anonymous structs.

type SimpleType

type SimpleType struct {
	// Name of the type, e.g. "string" or "Request" for "http.Request"
	Type string
	// Package the type is defined in, e.g. "http" for "http.Request"
	// Empty for built-in types.
	Package string
}

Simple types are: bool, string, error, int, float (and all the variations of the numeric types), interface{}, structs like http.Request. They consist of just the type name and possibly a package prefix. On the other hand pointers (*http.Request), slices, maps, function types, channels, anonymous structs, ... are composite types.

func (SimpleType) Packages

func (t SimpleType) Packages() []string

type StarType

type StarType struct {
	Type ParamType
}

func (StarType) Packages

func (st StarType) Packages() []string

Jump to

Keyboard shortcuts

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