Documentation ¶
Index ¶
- func FormatCode(code string) ([]byte, error)
- func FormatFieldList(src []byte, fl *ast.FieldList, pkgName string, declaredTypes []declaredType) []string
- func GetReceiverType(fd *ast.FuncDecl) (ast.Expr, error)
- func GetReceiverTypeName(src []byte, fl ast.Decl) (string, *ast.FuncDecl)
- func GetTypeDeclarationName(decl ast.Decl) string
- func Make(options MakeOptions) ([]byte, error)
- func MakeInterface(comment, pkgName, ifaceName, ifaceComment string, methods []string, ...) ([]byte, error)
- func ParseDeclaredTypes(src []byte) (declaredTypes []declaredType)
- type MakeOptions
- type Method
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatCode ¶
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 ¶
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 ¶
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 ¶
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 ¶
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