parser

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ARRAY represent a array value.
	ARRAY = "array"
	// OBJECT represent a object value.
	OBJECT = "object"
	// BOOLEAN represent a boolean value.
	BOOLEAN = "boolean"
	// INTEGER represent a integer value.
	INTEGER = "integer"
	// NUMBER represent a number value.
	NUMBER = "number"
	// STRING represent a string value.
	STRING = "string"
	// FUNC represent a function value.
	FUNC = "func"
	// INTERFACE represent a interface value.
	INTERFACE = "interface{}"
	// ANY represent a any value.
	ANY = "any"
	// NIL represent a empty value.
	NIL  = "nil"
	NULL = "null"
)

Variables

This section is empty.

Functions

func CheckSchemaType

func CheckSchemaType(typeName string) error

CheckSchemaType checks if typeName is not a name of primitive type.

func IsGolangPrimitiveType

func IsGolangPrimitiveType(typeName string) bool

IsGolangPrimitiveType determine whether the type name is a golang primitive type.

func IsNumericType

func IsNumericType(typeName string) bool

IsNumericType determines whether the type name is a numeric type.

func IsPrimitiveType

func IsPrimitiveType(typeName string) bool

IsPrimitiveType determine whether the type name is a primitive type.

func IsSimplePrimitiveType

func IsSimplePrimitiveType(typeName string) bool

IsSimplePrimitiveType determine whether the type name is a simple primitive type.

func SetExcludedDirsAndFiles

func SetExcludedDirsAndFiles(excludes string) func(*Parser)

func TransToValidSchemeType

func TransToValidSchemeType(typeName string) string

TransToValidSchemeType indicates type will transfer golang basic type to supported type.

Types

type ApiDocSpec

type ApiDocSpec struct {
	Service       string
	Title         string
	Version       string
	Description   string
	BaseURL       string
	Groups        []*ApiGroupSpec
	UngroupedApis []*ApiSpec
	TotalCount    int
}

type ApiGroupSpec

type ApiGroupSpec struct {
	Group       string
	Title       string
	Description string
	Apis        []*ApiSpec
	Order       int //sort
}

type ApiParameterSpec

type ApiParameterSpec struct {
	Name        string
	DataType    string
	Required    bool
	Description string
	Validate    string
	Example     string
	// contains filtered or unexported fields
}

func (ApiParameterSpec) ParameterTypes

func (p ApiParameterSpec) ParameterTypes() string

type ApiRequestSpec

type ApiRequestSpec struct {
	Parameters map[string]*ApiParameterSpec
	Accept     string //accept format
	Schema     *TypeSchema
	// contains filtered or unexported fields
}

func (*ApiRequestSpec) Body

func (s *ApiRequestSpec) Body() string

type ApiResponseSpec

type ApiResponseSpec struct {
	StatusCode  int
	Format      string //json xml
	Schema      *TypeSchema
	Description string
	IsMock      bool
	// contains filtered or unexported fields
}

func (*ApiResponseSpec) Body

func (s *ApiResponseSpec) Body() string

func (*ApiResponseSpec) PureBody

func (s *ApiResponseSpec) PureBody() string

type ApiSpec

type ApiSpec struct {
	Title       string
	HTTPMethod  string
	Api         string
	Version     string
	Accept      string //json,xml,form
	Format      string //json,xml
	Description string
	Author      string
	Deprecated  bool
	Group       string
	Responses   []*ApiResponseSpec
	Requests    ApiRequestSpec
	Order       int //sort
	// contains filtered or unexported fields
}

func (*ApiSpec) FullURL

func (a *ApiSpec) FullURL() string

type AstFileInfo

type AstFileInfo struct {
	// File ast.File
	File *ast.File
	// Path the path of the ast.File
	Path string
	// PackagePath package import path of the ast.File
	PackagePath string
}

type Operation

type Operation struct {
	ApiSpec
	// contains filtered or unexported fields
}

Operation describes a single API operation on a path.

func NewOperation

func NewOperation(parser *Parser, options ...func(*Operation)) *Operation

NewOperation creates a new Operation with default properties. map[int]Response.

func (*Operation) ParseComment

func (operation *Operation) ParseComment(comment string, astFile *ast.File) error

func (*Operation) ParseDescriptionComment

func (operation *Operation) ParseDescriptionComment(lineRemainder string)

ParseDescriptionComment godoc.

func (*Operation) ParseParametersComment

func (operation *Operation) ParseParametersComment(parameterType, commentLine string, astFile *ast.File) error

ParseParametersComment parses parameters (@header, @param, @query, @form) @param [name] [type] [required] [comment] @query demo int true "测试参数"

func (*Operation) ParseRequestComment

func (operation *Operation) ParseRequestComment(commentLine string, astFile *ast.File) error

func (*Operation) ParseResponseComment

func (operation *Operation) ParseResponseComment(commentLine string, astFile *ast.File) error

ParseResponseComment parses comment for given `response` comment string.

func (*Operation) ParseRouterComment

func (operation *Operation) ParseRouterComment(commentLine string) error

ParseRouterComment parses comment for given `router` comment string.

type PackageDefinitions

type PackageDefinitions struct {
	// files in this package, map key is file's relative path starting package path
	Files map[string]*ast.File

	// definitions in this package, map key is typeName
	TypeDefinitions map[string]*TypeSpecDef

	// package name
	Name string
}

PackageDefinitions files and definition in a package.

type PackagesDefinitions

type PackagesDefinitions struct {
	// contains filtered or unexported fields
}

func NewPackagesDefinitions

func NewPackagesDefinitions() *PackagesDefinitions

NewPackagesDefinitions create object PackagesDefinitions.

func (*PackagesDefinitions) CollectAstFile

func (pkgDefs *PackagesDefinitions) CollectAstFile(packageDir, path string, astFile *ast.File) error

CollectAstFile collect ast.file.

func (*PackagesDefinitions) FindTypeSpec

func (pkgDefs *PackagesDefinitions) FindTypeSpec(typeName string, file *ast.File, parseDependency bool) *TypeSpecDef

FindTypeSpec finds out TypeSpecDef of a type by typeName @typeName the name of the target type, if it starts with a package name, find its own package path from imports on top of @file @file the ast.file in which @typeName is used @pkgPath the package path of @file.

func (*PackagesDefinitions) ParseTypes

func (pkgDefs *PackagesDefinitions) ParseTypes() error

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func New

func New() *Parser

func (*Parser) GetApiDoc

func (p *Parser) GetApiDoc() *ApiDocSpec

func (*Parser) Parse

func (p *Parser) Parse(searchDirs []string) error

func (*Parser) ParseDefinition

func (p *Parser) ParseDefinition(typeSpecDef *TypeSpecDef, parentSchema *TypeSchema) (*TypeSchema, error)

ParseDefinition parses given type spec that corresponds to the type under given name and package

func (*Parser) Skip

func (p *Parser) Skip(path string, f os.FileInfo) error

Skip returns filepath.SkipDir error if match vendor and hidden folder.

type TypeSchema

type TypeSchema struct {
	Name        string //xxRequest, xxResponse, for example: RegisterRequest
	Type        string //int, string, bool, object, array, any
	FullName    string
	PkgPath     string //for example: github.com/alovn/apidocgen/examples/svc-user/handler
	FullPath    string //for example: github.com/alovn/apidocgen/examples/svc-user/handler.RegisterRequest
	Comment     string
	ArraySchema *TypeSchema
	Properties  map[string]*TypeSchema //object
	Parent      *TypeSchema
	TagValue    string
	// contains filtered or unexported fields
}

func (*TypeSchema) ExampleValue

func (v *TypeSchema) ExampleValue() string

func (*TypeSchema) GetTag

func (v *TypeSchema) GetTag(name string) (value string, has bool)

func (*TypeSchema) IsRequired

func (v *TypeSchema) IsRequired() (required bool)

func (*TypeSchema) JSON

func (s *TypeSchema) JSON(withComment bool) string

func (*TypeSchema) JSONKey

func (v *TypeSchema) JSONKey() (key string, isOmitempty bool)

func (*TypeSchema) ParameterTags

func (v *TypeSchema) ParameterTags() map[string]string

func (*TypeSchema) ValidateTag

func (v *TypeSchema) ValidateTag() (validate string)

func (*TypeSchema) Write

func (s *TypeSchema) Write(format string, withComment bool) (body string)

func (*TypeSchema) XML

func (s *TypeSchema) XML(withComment bool) string

func (*TypeSchema) XMLAttrs

func (v *TypeSchema) XMLAttrs() map[string]string

func (*TypeSchema) XMLName

func (v *TypeSchema) XMLName() string

func (*TypeSchema) XMLTag

func (v *TypeSchema) XMLTag() (xmlTag string, hasTag, isAttr, isOmitempty, isInner bool)

type TypeSpecDef

type TypeSpecDef struct {
	// ast file where TypeSpec is
	File *ast.File

	// the TypeSpec of this type definition
	TypeSpec *ast.TypeSpec

	// path of package starting from under ${GOPATH}/src or from module path in go.mod
	PkgPath string
}

TypeSpecDef the whole information of a typeSpec.

func (*TypeSpecDef) FullName

func (t *TypeSpecDef) FullName() string

FullName full name of the typeSpec.

func (*TypeSpecDef) FullPath

func (t *TypeSpecDef) FullPath() string

FullPath of the typeSpec.

func (*TypeSpecDef) Name

func (t *TypeSpecDef) Name() string

Name the name of the typeSpec.

Jump to

Keyboard shortcuts

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