maker

package
v0.0.0-...-259b9b2 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatCode

func FormatCode(code string) ([]byte, error)

FormatCode sets the options of the imports pkg and then applies the Process method which by default removes all of the imports not used and formats the remaining docs, imports and code like `gofmt`. It will e.g. remove paranthesis around a unnamed single return type

func FormatFieldList

func FormatFieldList(src []byte, fl *ast.FieldList, pkgName string, declaredTypes []declaredType) []string

FormatFieldList takes in the source code as a []byte and a FuncDecl parameters or return values as a FieldList. It then returns a []string with each param or return value as a single string. If the FieldList input is nil, it returns nil

func GetReceiverType

func GetReceiverType(fd *ast.FuncDecl) (ast.Expr, error)

GetReceiverType checks if the FuncDecl is a function or a method. If it is a function it returns a nil ast.Expr and a non-nil err. If it is a method it uses a hardcoded 0 index to fetch the receiver because a method can only have 1 receiver. Which can make you wonder why it is a list in the first place, but this type from the `ast` pkg is used in other places than for receivers

func GetReceiverTypeName

func GetReceiverTypeName(src []byte, fl ast.Decl) (string, *ast.FuncDecl)

GetReceiverTypeName takes in the entire source code and a single declaration. It then checks if the declaration is a function declaration, if it is, it uses the GetReceiverType to check whether the declaration is a method or a function if it is a function we fatally stop. If it is a method we retrieve the type of the receiver based on the types start and end pos in combination with the actual source code. It then returns the name of the receiver type and the function declaration

Behavior is undefined for a src []byte that isn't the source of the possible FuncDecl fl

func GetTypeDeclarationName

func GetTypeDeclarationName(decl ast.Decl) string

GetTypeDeclarationName extract the name of the type of this declaration if it refers to a type declaration. Otherwise, it returns an empty string.

func Make

func Make(options MakeOptions) ([]byte, error)

func MakeInterface

func MakeInterface(comment, pkgName, ifaceName, ifaceComment string, methods []string, imports []string) ([]byte, error)

MakeInterface takes in all of the items required for generating the interface, it then simply concatenates them all to an array, joins this array to a string with newline and passes it on to FormatCode which then directly returns the result

func ParseDeclaredTypes

func ParseDeclaredTypes(src []byte) (declaredTypes []declaredType)

ParseDeclaredTypes inspect given src code to find type declaractions.

Types

type MakeOptions

type MakeOptions struct {
	Files          []string
	StructType     string
	Comment        string
	PkgName        string
	IfaceName      string
	IfaceComment   string
	ImportModule   string
	CopyDocs       bool
	CopyTypeDoc    bool
	ExcludeMethods []string
}

MakeOptions contains options for the Make function.

type Method

type Method struct {
	Name string
	Code string
	Docs []string
}

Method describes the code and documentation tied into a method

func ParseStruct

func ParseStruct(src []byte, structName string, copyDocs bool, copyTypeDocs bool, pkgName string, declaredTypes []declaredType, importModule string) (methods []Method, imports []string, typeDoc string)

ParseStruct takes in a piece of source code as a []byte, the name of the struct it should base the interface on and a bool saying whether it should include docs. It then returns an []Method where Method contains the method declaration(not the code) that is required for the interface and any documentation if included. It also returns a []string containing all of the imports including their aliases regardless of them being used or not, the imports not used will be removed later using the 'imports' pkg If anything goes wrong, this method will fatally stop the execution

func (*Method) Lines

func (m *Method) Lines() []string

Lines return a []string consisting of the documentation and code appended in chronological order

Jump to

Keyboard shortcuts

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