Documentation
¶
Index ¶
- func AppendImportStatements(src []byte, appendImports []byte) []byte
- func ElementType(t string) string
- func ExprString(expr ast.Expr) string
- func FixImport(src []byte, file string)
- func GetImportPath(dir string) string
- func GetImportStatements(input []byte) []byte
- func GetMod() string
- func GrpcRelatedModify(src []byte, metaName string, grpcSvcName string) []byte
- func IsEnum(methods []MethodMeta) bool
- func IsSlice(t string) bool
- func IsVarargs(t string) bool
- func RestRelatedModify(src []byte, metaName string) []byte
- func RewriteTag(config RewriteTagConfig) (string, error)
- func ToSlice(t string) string
- func Visit(files *[]string) filepath.WalkFunc
- type Annotation
- type EnumCollector
- type EnumMeta
- type FieldMeta
- type InterfaceCollector
- type InterfaceMeta
- type MethodMeta
- type PackageMeta
- type RewriteTagConfig
- type StaticMethodCollector
- type StaticMethodCollectorOption
- type StructCollector
- type StructCollectorOption
- type StructMeta
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendImportStatements ¶ added in v1.1.3
func ElementType ¶ added in v1.1.4
ElementType get element type string from slice
func ExprString ¶
ExprString return string representation from ast.Expr
func GetImportPath ¶
GetImportPath get import path of pkg from dir
func GetImportStatements ¶ added in v1.1.3
func GrpcRelatedModify ¶ added in v1.3.0
func IsEnum ¶ added in v1.0.5
func IsEnum(methods []MethodMeta) bool
func RestRelatedModify ¶ added in v1.3.0
func RewriteTag ¶ added in v1.2.1
func RewriteTag(config RewriteTagConfig) (string, error)
RewriteTag overwrites json tag by convert function and return formatted source code
Example ¶
file := pathutils.Abs("testdata/rewritejsontag.go")
config := RewriteTagConfig{
File: file,
Omitempty: true,
ConvertFunc: strcase.ToLowerCamel,
Form: false,
}
result, err := RewriteTag(config)
if err != nil {
panic(err)
}
fmt.Println(result)
Output: package main type base struct { Index string `json:"index,omitempty"` Type string `json:"type,omitempty"` } type struct1 struct { base Name string `json:"name,omitempty"` StructType int `json:"structType,omitempty" dd:"awesomtag"` Format string `dd:"anothertag" json:"format,omitempty"` Pos int `json:"pos,omitempty"` }
Types ¶
type Annotation ¶ added in v1.1.7
func GetAnnotations ¶ added in v1.1.7
func GetAnnotations(text string) []Annotation
Example ¶
ret := GetAnnotations(`// <b style="color: red">NEW</b> 删除数据接口(不删数据文件)@role(SUPER_ADMIN)@permission(create,update)这是几个注解@sss()`) fmt.Println(ret)
Output: [{@role [SUPER_ADMIN]} {@permission [create update]} {@sss []}]
type EnumCollector ¶ added in v1.0.5
type EnumCollector struct {
Methods map[string][]MethodMeta
Package PackageMeta
Consts map[string][]string
Enums map[string]EnumMeta
// contains filtered or unexported fields
}
func NewEnumCollector ¶ added in v1.0.5
func NewEnumCollector(exprString func(ast.Expr) string) *EnumCollector
NewEnumCollector initializes an EnumCollector
type FieldMeta ¶
type FieldMeta struct {
Name string
Type string
Tag string
Comments []string
IsExport bool
// used in OpenAPI 3.0 spec as property name
DocName string
// Annotations of the field
Annotations []Annotation
// ValidateTag based on https://github.com/go-playground/validator
// please refer to its documentation https://pkg.go.dev/github.com/go-playground/validator/v10
ValidateTag string
}
FieldMeta wraps field info
type InterfaceCollector ¶
type InterfaceCollector struct {
Interfaces []InterfaceMeta
Package PackageMeta
// contains filtered or unexported fields
}
InterfaceCollector collect interfaces by parsing source code
func BuildInterfaceCollector ¶
func BuildInterfaceCollector(file string, exprString func(ast.Expr) string) InterfaceCollector
BuildInterfaceCollector initializes an InterfaceCollector and collects interfaces
func NewInterfaceCollector ¶
func NewInterfaceCollector(exprString func(ast.Expr) string) *InterfaceCollector
NewInterfaceCollector initializes an InterfaceCollector
type InterfaceMeta ¶
type InterfaceMeta struct {
Name string
Methods []MethodMeta
Comments []string
}
InterfaceMeta wraps interface info
type MethodMeta ¶
type MethodMeta struct {
// Recv method receiver
Recv string
// Name method name
Name string
// Params when generate client code from openapi3 spec json file, Params holds all method input parameters.
// when generate client code from service interface in svc.go file, if there is struct type param, this struct type param will put into request body,
// then others will be put into url as query string. if there is no struct type param and the api is a get request, all will be put into url as query string.
// if there is no struct type param and the api is Not a get request, all will be put into request body as application/x-www-form-urlencoded data.
// specially, if there is one or more v3.FileModel or []v3.FileModel params,
// all will be put into request body as multipart/form-data data.
Params []FieldMeta
// Results response
Results []FieldMeta
// PathVars not support when generate client code from service interface in svc.go file
// when generate client code from openapi3 spec json file, PathVars is parameters in url as path variable.
PathVars []FieldMeta
// HeaderVars not support when generate client code from service interface in svc.go file
// when generate client code from openapi3 spec json file, HeaderVars is parameters in header.
HeaderVars []FieldMeta
// BodyParams not support when generate client code from service interface in svc.go file
// when generate client code from openapi3 spec json file, BodyParams is parameters in request body as query string.
BodyParams *FieldMeta
// BodyJSON not support when generate client code from service interface in svc.go file
// when generate client code from openapi3 spec json file, BodyJSON is parameters in request body as json.
BodyJSON *FieldMeta
// Files not support when generate client code from service interface in svc.go file
// when generate client code from openapi3 spec json file, Files is parameters in request body as multipart file.
Files []FieldMeta
// Comments of the method
Comments []string
// Path api path
// not support when generate client code from service interface in svc.go file
Path string
// QueryParams not support when generate client code from service interface in svc.go file
// when generate client code from openapi3 spec json file, QueryParams is parameters in url as query string.
QueryParams *FieldMeta
// Annotations of the method
Annotations []Annotation
}
MethodMeta represents an api
func GetMethodMeta ¶
func GetMethodMeta(spec *ast.FuncDecl) MethodMeta
GetMethodMeta get method name then new MethodMeta struct from *ast.FuncDecl
func NewMethodMeta ¶
NewMethodMeta new MethodMeta struct from *ast.FuncDecl
func (MethodMeta) String ¶
func (mm MethodMeta) String() string
type RewriteTagConfig ¶ added in v1.2.1
type StaticMethodCollector ¶ added in v1.2.2
type StaticMethodCollector struct {
Methods []MethodMeta
Package PackageMeta
// contains filtered or unexported fields
}
StaticMethodCollector collect methods by parsing source code
func BuildStaticMethodCollector ¶ added in v1.2.2
func BuildStaticMethodCollector(file string, exprString func(ast.Expr) string) StaticMethodCollector
BuildStaticMethodCollector initializes an StaticMethodCollector and collects static methods
func NewStaticMethodCollector ¶ added in v1.2.2
func NewStaticMethodCollector(exprString func(ast.Expr) string, opts ...StaticMethodCollectorOption) *StaticMethodCollector
NewStaticMethodCollector initializes an StaticMethodCollector
type StaticMethodCollectorOption ¶ added in v1.2.2
type StaticMethodCollectorOption func(collector *StaticMethodCollector)
type StructCollector ¶
type StructCollector struct {
Structs []StructMeta
Methods map[string][]MethodMeta
Package PackageMeta
NonStructTypeMap map[string]ast.Expr
// contains filtered or unexported fields
}
StructCollector collect structs by parsing source code
func BuildStructCollector ¶
func BuildStructCollector(file string, exprString func(ast.Expr) string) StructCollector
BuildStructCollector initializes an StructCollector and collects structs
func NewStructCollector ¶
func NewStructCollector(exprString func(ast.Expr) string, opts ...StructCollectorOption) *StructCollector
NewStructCollector initializes an StructCollector
func (*StructCollector) Collect ¶
func (sc *StructCollector) Collect(n ast.Node) ast.Visitor
Collect collects all structs from source code
func (*StructCollector) DocFlatEmbed ¶
func (sc *StructCollector) DocFlatEmbed() []StructMeta
DocFlatEmbed flatten embed struct fields
type StructCollectorOption ¶ added in v1.0.5
type StructCollectorOption func(collector *StructCollector)
func WithEnums ¶ added in v1.0.5
func WithEnums(enums map[string]EnumMeta) StructCollectorOption
type StructMeta ¶
type StructMeta struct {
Name string
Fields []FieldMeta
Comments []string
Methods []MethodMeta
IsExport bool
// go-doudou version
Version string
}
StructMeta wraps struct info
func NewStructMeta ¶
func NewStructMeta(structType *ast.StructType, exprString func(ast.Expr) string) StructMeta
NewStructMeta new StructMeta from *ast.StructType