componentparser

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanType

func CleanType(t string) string

Get type without point

func FindChildNode

func FindChildNode[T any](node ast.Node) T

Find the first child node of the specified type

func FindChildNodes

func FindChildNodes[T any](node ast.Node) []T

Grab the children nodes of the specified type

func ReplaceScopedNames added in v1.1.0

func ReplaceScopedNames(s, packageName string, scopedNames map[string]bool) string

Types

type Field

type Field struct {
	Name string
	Type string

	MockPkg  string
	MockNew  string
	MockType string
}

A field is a name and a type. If in a struct, it can also have a tag. If a tag is in this format, `component:"$TAG"`, it will be in the TAG field.

type Fields

type Fields []Field

func ConvertASTFieldList

func ConvertASTFieldList(fileString FileString, node *ast.FieldList) Fields

Convert a []*ast.Field -> Fields

func ConvertTypeString added in v1.1.0

func ConvertTypeString(typeString string) Fields

func (Fields) AddPointers added in v1.1.0

func (fields Fields) AddPointers(s string, asInterface bool) string

func (Fields) AsArgs

func (fields Fields) AsArgs(includeParenthesis bool) string

Return args in format $Arg1_Name $Arg1_Type, ..., $ArgN_Name $ArgN_Type

func (Fields) AsInterface

func (fields Fields) AsInterface(includeParenthesis bool) string

Return args in format $Arg1_Name interface{}, ..., $ArgN_Name interface{}

func (Fields) AsParams

func (fields Fields) AsParams() string

Return args in format $Arg1_Name, ..., $ArgN_Name

func (Fields) AsTypes

func (fields Fields) AsTypes(includeParenthesis bool) string

Return args in format $Arg1_Type, ..., $ArgN_Type

func (Fields) Generic added in v1.1.0

func (fields Fields) Generic(append bool) (string, string)

Get generic representations

type FileString

type FileString string

Helper wrapper for strings with some additional helper methods

func (FileString) Extract

func (f FileString) Extract(node ast.Node) string

Extract the node string from a file. Convert all the types accordingly and properly handles indexing of the node position.

type MethodData

type MethodData struct {
	Name    string
	Recv    Field
	Args    Fields
	Returns Fields
}

Name: The name of the method. Recv: The reciever for the method. Args: List of all the args for the method. Returns: List of al lthe returns

type Parser

type Parser struct {
	Args ParserArgs

	Structs        map[string]*StructData
	PackageImports map[string]map[string]bool // List of all the imports needed for a package

	ScopedNames map[string]bool // Map of all the scoped names in the package

	// Values that get updated as the parser is walking
	File          string     // Which file is currently being parsed
	FileString    FileString // The extracted file string corresponding to .File
	PackageFolder string     // Which package fodler is currently being parsed
	PackageName   string     // Which package is currently being parsed
	ModulePath    string     // Go Package Path

}

A parser looks through a directory and all it's children. It creates a group of StructData objects representing all the structs that should be generated.

func New

func New(cmd *cobra.Command) *Parser

func (*Parser) CreateBaseStructData

func (p *Parser) CreateBaseStructData(name string) *StructData

Only call when parsing

func (*Parser) Parse

func (p *Parser) Parse()

Parse everything in the specified directory according to the args.

func (*Parser) ParseDir

func (p *Parser) ParseDir(dir string)

Parse method that populates all the needed data for a specific package/dir.

func (*Parser) ParseFile

func (p *Parser) ParseFile()

Parser method that reads an individual file and adjusts the data saved in the Structs field as data is read in.

func (*Parser) ParseFuncDecl

func (p *Parser) ParseFuncDecl(node *ast.FuncDecl)

Add method data to a struct. Also handles reading the params convert function

func (*Parser) ParseGenDecl

func (p *Parser) ParseGenDecl(node *ast.GenDecl)

Parse a GenDecl node and add the data to the Parser. The two node types that are relevant are the imports and the structs.

func (*Parser) PopulateStructData

func (p *Parser) PopulateStructData(structData *StructData, node *ast.StructType)

Function to populate the base struct data

type ParserArgs

type ParserArgs struct {
	/*
		The directory to run the parser in. All child folders will be walked
		through automatically.
	*/
	Directory string

	/*
		The default config to use for mockery generation commands. Will be
		automatically passed into all child struct generate commands.
	*/
	Config string

	// Regex to match against struct tags
	Match string
}

type StructData

type StructData struct {
	Name       string // The text string representing the component as it was found
	Generic    Fields // Represent any generic types for the struct
	StructFile string // The file the struct was found in

	PackageName   string // The name of the package the struct resides in
	PackageFolder string // The enclosing folder of the struct file

	ConvertVar      string // The string that represents the reciever variable in the convert function
	ConvertFunction string // Full text of the params.Convert function

	ScopedNames map[string]bool // List of names that appear in the package

	/*
		All the imports required for all the files associated with the
		component. This includes all methods as well.
	*/
	Imports map[string]bool

	Fields  Fields       // All the fields for this component
	Methods []MethodData // All the public methods for this component

	// Generate flags
	Options StructOptions
}

Data corresponding to a particular struct representing a component. These structs are identified by the components package by the use of comment strings defined inside of the struct.

func (*StructData) ID

func (s *StructData) ID() string

type StructOptions

type StructOptions struct {
	Generate bool // Whether or not a "generate:component" tag was found in the comments of this struct

	InterfaceName    string // Name of the interface once generated
	InterfaceFolder  string // Folder for the exported interface to go
	InterfacePackage string // Name of the package for the generated interface.
	InterfaceFile    string // File Name for the generated interface to go

	MockFolder  string // Name of the generated mockery folder
	MockPackage string // Name of the generated mockery package
	MockFile    string // Location of the generated mockery files

	SkipTestFile bool // True if the test file should be created.

	/*
		Whether or not to generate the tests file for this struct in the same
		package or as a "blackbox" with the "_test" extension.
	*/
	Blackbox bool

	// The config to use for the mockery generation
	Config string

	// Whether or not to create shortcut expecters for each mocked subcomponent
	Expecters []string
}

Flags for the components generate function. These fields are set via a struct

Jump to

Keyboard shortcuts

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