api

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2019 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CommentsOptionDescriptors is the option name of descriptors.
	CommentsOptionDescriptors = "descriptors"
	// CommentsOptionModifiers is the option name of modifiers.
	CommentsOptionModifiers = "modifiers"
	// CommentsOptionAlias is the option name of alias.
	CommentsOptionAlias = "alias"
	// CommentsOptionOrigin is the option name of original name.
	CommentsOptionOrigin = "origin"
)
View Source
const TypeNameInvalid = ""

TypeNameInvalid indicates an invalid type name.

Variables

This section is empty.

Functions

func NewPathDefinitions

func NewPathDefinitions(tc *TypeContainer, definitions map[string][]definition.Definition) (map[string][]Definition, error)

NewPathDefinitions creates a list of definitions with path.

Types

type Analyzer

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

Analyzer analyzes go packages.

func NewAnalyzer

func NewAnalyzer(root string) *Analyzer

NewAnalyzer creates a code analyzer.

func (*Analyzer) Comments

func (a *Analyzer) Comments(pos token.Pos) *ast.CommentGroup

Comments returns immediate comments above pos. Import package before calling this method.

func (*Analyzer) FindPackages

func (a *Analyzer) FindPackages(target string) []string

FindPackages returns packages which contain target. Import package before calling this method.

func (*Analyzer) Import

func (a *Analyzer) Import(path string) (*types.Package, error)

Import imports a package and all packages it depends on.

func (*Analyzer) ObjectOf

func (a *Analyzer) ObjectOf(pkg, name string) (types.Object, error)

ObjectOf returns declaration object of target.

func (*Analyzer) PackageComments

func (a *Analyzer) PackageComments(path string) []*ast.CommentGroup

PackageComments returns comments above package keyword. Import package before calling this method.

func (*Analyzer) Packages

func (a *Analyzer) Packages(parent string, vendor bool) []string

Packages returns packages under specified directory (including itself). Import package before calling this method.

type Buffer

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

Buffer provides a buffer to write data. The buffer will panic if an error occurs.

func NewBuffer

func NewBuffer() *Buffer

NewBuffer creates a buffer.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns a slice of length b.Len() holding the unread portion of the buffer.

func (*Buffer) String

func (b *Buffer) String() string

String returns the contents of the unread portion of the buffer as a string. If the Buffer is a nil pointer, it returns "<nil>".

func (*Buffer) Write

func (b *Buffer) Write(a ...interface{})

Write writes data to this buffer.

func (*Buffer) Writef

func (b *Buffer) Writef(format string, a ...interface{})

Writef writes data with format to this buffer.

func (*Buffer) Writeln

func (b *Buffer) Writeln(a ...interface{})

Writeln writes data with a newline to this buffer.

type Comments

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

Comments is parsed from go comments.

func ParseComments

func ParseComments(comments string) *Comments

ParseComments parses comments and extracts nirvana options.

func (*Comments) AddOption

func (c *Comments) AddOption(name, value string)

AddOption adds an option.

func (*Comments) BlockComments

func (c *Comments) BlockComments() string

BlockComments returns block style comments.

func (*Comments) CleanOptions

func (c *Comments) CleanOptions()

CleanOptions removes all options.

func (*Comments) LineComments

func (c *Comments) LineComments() string

LineComments returns line style comments.

func (*Comments) Option

func (c *Comments) Option(name string) []string

Option returns values of an option.

func (*Comments) Options

func (c *Comments) Options() map[string][]string

Options returns all options.

func (*Comments) Rename

func (c *Comments) Rename(origin, target string) bool

Rename replaces the first word of this comments. If its first word is not the same as origin, the method returns false.

func (*Comments) String

func (c *Comments) String() string

String returns comments.

type Container

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

Container contains informations to generate APIs.

func NewContainer

func NewContainer(root string) *Container

NewContainer creates API container.

func (*Container) AddDescriptor

func (ac *Container) AddDescriptor(descriptors ...definition.Descriptor)

AddDescriptor add descriptors to container.

func (*Container) AddModifier

func (ac *Container) AddModifier(modifiers ...service.DefinitionModifier)

AddModifier add definition modifiers to container.

func (*Container) Generate

func (ac *Container) Generate() (*Definitions, error)

Generate generates API definitions.

type Definition

type Definition struct {
	// Method is definition method.
	Method definition.Method
	// HTTPMethod is http method.
	HTTPMethod string
	// HTTPCode is http success code.
	HTTPCode int
	// Summary is a brief of this definition.
	Summary string
	// Description describes the API handler.
	Description string
	// Consumes indicates how many content types the handler can consume.
	// It will override parent descriptor's consumes.
	Consumes []string
	// Produces indicates how many content types the handler can produce.
	// It will override parent descriptor's produces.
	Produces []string
	// ErrorProduces is used to generate data for error. If this field is empty,
	// it means that this field equals to Produces.
	// In some cases, successful data and error data should be generated in
	// different ways.
	ErrorProduces []string
	// Function is a function handler. It must be func type.
	Function TypeName
	// Parameters describes function parameters.
	Parameters []Parameter
	// Results describes function retrun values.
	Results []Result
	// Examples contains many examples for the API handler.
	Examples []Example
}

Definition is complete version of def.Definition.

func NewDefinition

func NewDefinition(tc *TypeContainer, d *definition.Definition) (*Definition, error)

NewDefinition creates openapi.Definition from definition.Definition.

func NewDefinitions

func NewDefinitions(tc *TypeContainer, definitions []definition.Definition) ([]Definition, error)

NewDefinitions creates a list of definitions.

type Definitions

type Definitions struct {
	// Definitions holds mappings between path and API descriptions.
	Definitions map[string][]Definition
	// Types contains all types used by definitions.
	Types map[TypeName]*Type
}

Definitions describes all APIs and its related object types.

func (*Definitions) Subset

func (d *Definitions) Subset(require func(path string, def *Definition) bool) *Definitions

Subset returns a subset required by a definition filter.

type Example

type Example struct {
	// Description describes the example.
	Description string
	// Type is result object type.
	Type TypeName
	// Instance is encoded instance data.
	Instance []byte
}

Example is just an example.

type FuncField

type FuncField struct {
	// Name is the field name.
	Name string
	// Type is field type name.
	Type TypeName
}

FuncField describes a field of function.

type Parameter

type Parameter struct {
	// Source is the parameter value generated from.
	Source definition.Source
	// Name is the name to get value from a request.
	Name string
	// Description describes the parameter.
	Description string
	// Type is parameter object type.
	Type TypeName
	// Default is encoded default value.
	Default []byte
}

Parameter describes a function parameter.

type Result

type Result struct {
	// Destination is the target for the result.
	Destination definition.Destination
	// Description describes the result.
	Description string
	// Type is result object type.
	Type TypeName
}

Result describes a function result.

type StructField

type StructField struct {
	// Name is the field name.
	Name string
	// Comments of the type.
	Comments string
	// PkgPath is the package path that qualifies a lower case (unexported)
	// field name. It is empty for upper case (exported) field names.
	PkgPath string
	// Type is field type name.
	Type TypeName
	// Tag is field tag.
	Tag reflect.StructTag
	// Offset within struct, in bytes.
	Offset uintptr
	// Index sequence for Type.FieldByIndex.
	Index []int
	// Anonymous shows whether the field is an embedded field.
	Anonymous bool
}

StructField describes a field of a struct.

type Type

type Type struct {
	// Name is short type name.
	Name string
	// Comments of the type.
	Comments string
	// PkgPath is the package for this type.
	PkgPath string
	// Kind is type kind.
	Kind reflect.Kind
	// Key is map key type. Only used in map.
	Key TypeName
	// Elem is the element type of map, slice, array, pointer.
	Elem TypeName
	// Fields contains all struct fields of a struct.
	Fields []StructField
	// In presents fields of function input parameters.
	In []FuncField
	// Out presents fields of function output results.
	Out []FuncField
	// Conflict identifies the index of current type in a list of
	// types which have same type names. In most cases, this field is 0.
	Conflict int
}

Type describes an go type.

func (*Type) RawTypeName

func (t *Type) RawTypeName() TypeName

RawTypeName returns raw type name without confliction.

func (*Type) TypeName

func (t *Type) TypeName() TypeName

TypeName returns type unique name.

type TypeContainer

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

TypeContainer contains types.

func NewTypeContainer

func NewTypeContainer() *TypeContainer

NewTypeContainer creates a type container.

func (*TypeContainer) Complete

func (tc *TypeContainer) Complete(analyzer *Analyzer) error

Complete fills comments for all types.

func (*TypeContainer) NameOf

func (tc *TypeContainer) NameOf(typ reflect.Type) TypeName

NameOf gets an unique name of a type.

func (*TypeContainer) NameOfInstance

func (tc *TypeContainer) NameOfInstance(ins interface{}) TypeName

NameOfInstance gets type name of an instance.

func (*TypeContainer) Type

func (tc *TypeContainer) Type(name TypeName) *Type

Type gets type via type name.

func (*TypeContainer) Types

func (tc *TypeContainer) Types() map[TypeName]*Type

Types returns all types

type TypeName

type TypeName string

TypeName is unique name for go types.

Jump to

Keyboard shortcuts

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