swagger

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2021 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Constants

View Source
const IndexTempl = `` /* 3501-byte string literal not displayed */

Variables

This section is empty.

Functions

func AddNewDefinition

func AddNewDefinition(objName string, s interface{}, sw *Doc)

AddNewDefinition is a helper for add new definition in map

func Fill

func Fill(url, name string) func(c *Config)

Fill presents the url pointing to API definition and the name of swagger object

func ParseKind

func ParseKind(kind interface{}) (typeName, format string)

ParseKind parse simple types and return it TypeName and Format

func ParseRootType

func ParseRootType(obj Schemater, sw Doc)

ParseRootType is a method for analyzing Types and Schemas of Parameters and Response

func ReadDoc

func ReadDoc(name string) (string, error)

ReadDoc reads swagger document.

func Register

func Register(name string, swagger Swagger)

Register registers swagger for given name.

func URL

func URL(url string) func(c *Config)

URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).

Types

type AdderInParameter

type AdderInParameter interface {
	// AddInBodyParameter - adds a request in body parameter
	AddInPathParameter(name, description string, t reflect.Kind) AdderInParameter
	// AddInQueryParameter - adds a request in query parameter
	AddInQueryParameter(name, description string, t reflect.Kind, required bool) AdderInParameter
	// AddInHeaderParameter - adds a request in header parameter
	AddInHeaderParameter(name, description string, t reflect.Kind, required bool) AdderInParameter
	// AddInCookieParameter - adds a request in cookie parameter
	AddInCookieParameter(name, description string, t reflect.Kind, required bool) AdderInParameter
	// AddInFileParameter - adds a request in file parameter
	AddInFileParameter(name, description string) AdderInParameter
	Responser
}

type AdditionalProperties

type AdditionalProperties struct {
	// Object format, for example: int64 is integer with format int64
	Format string `json:"format,omitempty"`
	// Reference to a schema definition
	Ref string `json:"$ref,omitempty"`
	// Type name
	TypeName string `json:"type,omitempty"`
}

type ArrayParameters

type ArrayParameters []*Parameter

type BaseAPI

type BaseAPI struct {
	Version string `json:"swagger,omitempty"`
	Info    IInfo  `json:"info,omitempty"`
	// Base path to API, it consist the prefix of endpoint path
	BasePath string `json:"basePath,omitempty"`
}

Base object for describing the builded API

func (*BaseAPI) NewSwagger

func (s *BaseAPI) NewSwagger() BasePather

func (*BaseAPI) SetBasePath

func (s *BaseAPI) SetBasePath(p string) Informer

func (*BaseAPI) SetInfo

func (s *BaseAPI) SetInfo(i IInfo) ISwaggerAPI

type BaseObject

type BaseObject struct {
	// Type name
	TypeName string `json:"type,omitempty"`
	// List of enumeration values
	Enum []interface{} `json:"enum,omitempty"`
	// Default value
	Default interface{} `json:"default,omitempty"`
	// Example of value
	Example interface{} `json:"example,omitempty"`
	// Could it be null?
	Nullable string `json:"nullable,omitempty"`
	// Detailed object description
	Description string `json:"description,omitempty"`
	// Name of object
	Name string `json:"name,omitempty"`
	// Object format, for example: int64 is integer with format int64
	Format string `json:"format,omitempty"`
	// Reference to a schema definition
	Ref string `json:"$ref,omitempty"`
	// The object schema
	Schema *Schema `json:"schema,omitempty"`
	// Kind of object
	Type reflect.Kind `json:"-"`
	//
	AdditionalProperties *AdditionalProperties `json:"additionalProperties,omitempty"`
}

Base object include all common fields of responses and parameters

func NewBaseObject

func NewBaseObject(name, description string, t interface{}) *BaseObject

type BasePather

type BasePather interface {
	SetBasePath(p string) Informer
}

func NewSwagger

func NewSwagger() BasePather

type Config

type Config struct {
	// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
	URL string
	// Name of swagger object
	Name string
}

Config stores httpSwagger configuration variables.

type Consumer

type Consumer interface {
	// SetDescription - sets a description of endpoint
	SetDescription(d string) Summarer
}

type Contact

type Contact struct {
	Name  string `json:"name,omitempty"`
	URL   string `json:"url,omitempty"`
	Email string `json:"email,omitempty"`
}

func (*Contact) SetEmail

func (c *Contact) SetEmail(e string) IContact

func (*Contact) SetName

func (c *Contact) SetName(n string) IContact

func (*Contact) SetURL

func (c *Contact) SetURL(url string) IContact

type Definition

type Definition struct {
	// Type name
	TypeName string `json:"type,omitempty"`
	// List of properties of definition object
	Properties map[string]*Property `json:"properties,omitempty"`
}

func (*Definition) Parse

func (d *Definition) Parse(s interface{}, sw *Doc)

Parse definition of object

type Doc

type Doc struct {
	BaseAPI
	// List of paths to endpoints
	Paths map[string]Methods `json:"paths,omitempty"`
	// List of definitions
	Definitions map[string]*Definition `json:"definitions,omitempty"`
}

High level object for describing the builded API

func (*Doc) JSON

func (s *Doc) JSON() (jsonData []byte)

func (*Doc) ReadDoc

func (s *Doc) ReadDoc() string

type EmptyWriter

type EmptyWriter struct{}

func (*EmptyWriter) Header

func (e *EmptyWriter) Header() http.Header

func (*EmptyWriter) Write

func (e *EmptyWriter) Write(data []byte) (int, error)

func (*EmptyWriter) WriteHeader

func (e *EmptyWriter) WriteHeader(statusCode int)

type IContact

type IContact interface {
	SetEmail(e string) IContact
	SetName(n string) IContact
	SetURL(url string) IContact
}

func NewContact

func NewContact() IContact

type IInfo

type IInfo interface {
	SetVersion(v string) IInfo
	SetContact(c IContact) IInfo
	SetDescription(d string) IInfo
	SetTitle(t string) IInfo
	SetLicense(l ILicense) IInfo
	SetTermOfService(tos string) IInfo
}

func NewInfo

func NewInfo() IInfo

type ILicense

type ILicense interface {
	SetURL(url string) ILicense
	SetName(n string) ILicense
}

func NewLicense

func NewLicense() ILicense

type IMethod

type IMethod interface {
	// SetConsumes - sets the MIME types of accept data for the endpoint
	SetConsumes(c ...string) Producer
	// SetConsumes - sets the MIME types of return data for the endpoint
	SetProduces(p ...string) Consumer
	// AddResponse - adds a response
	AddResponse(сode int, description string, schema interface{}) Responser
	// AddFileParameter - adds a file response
	AddFileResponse(responseCode int, description string) Responser
}

First, programmer set Consumes/Produces/Response

type ISwaggerAPI

type ISwaggerAPI interface{}

type InType

type InType string
const (
	InBody   InType = "body"
	InQuery  InType = "query"
	InPath   InType = "path"
	InHeader InType = "header"
	InCookie InType = "cookie"
	InFile   InType = "formData"
)

type Info

type Info struct {
	Description    string   `json:"description,omitempty"`
	Title          string   `json:"title,omitempty"`
	TermsOfService string   `json:"termsOfService,omitempty"`
	Contact        IContact `json:"contact,omitempty"`
	License        ILicense `json:"license,omitempty"`
	Version        string   `json:"version,omitempty"`
}

Information about the created swagger

func (*Info) SetContact

func (i *Info) SetContact(c IContact) IInfo

func (*Info) SetDescription

func (i *Info) SetDescription(d string) IInfo

func (*Info) SetLicense

func (i *Info) SetLicense(l ILicense) IInfo

func (*Info) SetTermOfService

func (i *Info) SetTermOfService(tos string) IInfo

func (*Info) SetTitle

func (i *Info) SetTitle(t string) IInfo

func (*Info) SetVersion

func (i *Info) SetVersion(v string) IInfo

type Informer

type Informer interface {
	SetInfo(i IInfo) ISwaggerAPI
}

type License

type License struct {
	Name string `json:"name,omitempty"`
	URL  string `json:"url,omitempty"`
}

func (*License) SetName

func (l *License) SetName(n string) ILicense

func (*License) SetURL

func (l *License) SetURL(url string) ILicense

type MapProperty

type MapProperty map[string]*Property

type MapResponse

type MapResponse map[string]*Response

type MapResponseCode

type MapResponseCode map[int]int

func (MapResponseCode) Append

func (arc MapResponseCode) Append(respCode int) string

type Method

type Method struct {
	Description string   `json:"description,omitempty"`
	Consumes    []string `json:"consumes,omitempty"`
	Produces    []string `json:"produces,omitempty"`
	Summary     string   `json:"summary,omitempty"`
	OperationID string   `json:"operationId,omitempty"`
	// The parameters of requests
	Parameters ArrayParameters `json:"parameters,omitempty"`
	// The endpoint responses
	Responses MapResponse `json:"responses,omitempty"`
	// Counters for same codes for multiple responses
	ResponseCode MapResponseCode `json:"-"`
}

Description the REST-method of endpoint

func NewMethod

func NewMethod() *Method

NewMethod - create a new instance of the Method

func (*Method) AddFileResponse added in v1.2.0

func (m *Method) AddFileResponse(responseCode int, description string) Responser

func (*Method) AddInBodyParameter

func (m *Method) AddInBodyParameter(name, description string, t interface{}, required bool) AdderInParameter

func (*Method) AddInCookieParameter

func (m *Method) AddInCookieParameter(name, description string, t reflect.Kind, required bool) AdderInParameter

func (*Method) AddInFileParameter added in v1.2.0

func (m *Method) AddInFileParameter(name, description string) AdderInParameter

func (*Method) AddInHeaderParameter

func (m *Method) AddInHeaderParameter(name, description string, t reflect.Kind, required bool) AdderInParameter

func (*Method) AddInPathParameter

func (m *Method) AddInPathParameter(name, description string, t reflect.Kind) AdderInParameter

func (*Method) AddInQueryParameter

func (m *Method) AddInQueryParameter(name, description string, t reflect.Kind, required bool) AdderInParameter

func (*Method) AddResponse

func (m *Method) AddResponse(responseCode int, description string, schema interface{}) Responser

func (*Method) Parse

func (m *Method) Parse(path, methodName string, sw Doc)

func (*Method) SetConsumes

func (m *Method) SetConsumes(c ...string) Producer

func (*Method) SetDescription

func (m *Method) SetDescription(d string) Summarer

func (*Method) SetProduces

func (m *Method) SetProduces(p ...string) Consumer

func (*Method) SetSummary

func (m *Method) SetSummary(s string) ParameterAndResponser

type Methods

type Methods map[string]IMethod

List of methods (GET, POST,...) for endpoint

type Parameter

type Parameter struct {
	*BaseObject
	// How passed parameter - in body, in query or in path
	IN InType `json:"in,omitempty"`
	// Is a required parameter?
	Req bool `json:"required,omitempty"`
}

func NewFileParameter added in v1.2.0

func NewFileParameter(name, description string) *Parameter

func NewParameter

func NewParameter(name, description string, t interface{}, required bool, inType InType) *Parameter

func (*Parameter) GetSchema

func (p *Parameter) GetSchema() *Schema

func (*Parameter) GetType

func (p *Parameter) GetType() reflect.Kind

func (*Parameter) Parse

func (p *Parameter) Parse(sw Doc)

Parse a parameter structure for JSON generation

func (*Parameter) SetFormat

func (p *Parameter) SetFormat(format string)

func (*Parameter) SetTypeName

func (p *Parameter) SetTypeName(typeName string)

type ParameterAndResponser

type ParameterAndResponser interface {
	// AddParameter - adds a response
	AddResponse(Code int, description string, schema interface{}) Responser
	// AddFileParameter - adds a file response
	AddFileResponse(responseCode int, description string) Responser
	// AddInBodyParameter - adds a request in body parameter
	AddInBodyParameter(name, description string, t interface{}, required bool) AdderInParameter
	// AddInPathParameter - adds a request in path parameter
	AddInPathParameter(name, description string, t reflect.Kind) AdderInParameter
	// AddInQueryParameter - adds a request in query parameter
	AddInQueryParameter(name, description string, t reflect.Kind, required bool) AdderInParameter
	// AddInHeaderParameter - adds a request in header parameter
	AddInHeaderParameter(name, description string, t reflect.Kind, required bool) AdderInParameter
	// AddInCookieParameter - adds a request in cookie parameter
	AddInCookieParameter(name, description string, t reflect.Kind, required bool) AdderInParameter
	// AddInFileParameter - adds a request in file parameter
	AddInFileParameter(name, description string) AdderInParameter
}

type Producer

type Producer interface {
	// SetProduces - sets the MIME types of return data for the endpoint
	SetProduces(p ...string) Consumer
	// SetDescription - sets a description of endpoint
	SetDescription(d string) Summarer
}

type Property

type Property struct {
	BaseObject
	Item *BaseObject `json:"items,omitempty"`
}

Property definats a property item in Definition object

type Response

type Response struct {
	*BaseObject
}

func NewResponse

func NewResponse(description string) *Response

func (*Response) GetSchema

func (r *Response) GetSchema() *Schema

func (*Response) GetType

func (r *Response) GetType() reflect.Kind

func (*Response) Parse

func (r *Response) Parse(sw Doc)

Parse a response structure for JSON generation

func (*Response) SetFormat

func (r *Response) SetFormat(format string)

func (*Response) SetTypeName

func (r *Response) SetTypeName(typeName string)

type Responser

type Responser interface {
	// AddParameter - adds a response
	AddResponse(сode int, description string, schema interface{}) Responser
	// AddFileParameter - adds a file response
	AddFileResponse(responseCode int, description string) Responser
}

type Schema

type Schema struct {
	// Object format, for example: int64 is integer with format int64
	Format string `json:"format,omitempty"`
	// Reference to a schema definition
	Ref string `json:"$ref,omitempty"`
	// Type name
	TypeName string `json:"type,omitempty"`
	//
	AdditionalProperties *AdditionalProperties `json:"additionalProperties,omitempty"`
	//
	Item *BaseObject `json:"items,omitempty"`
	//
	Type interface{} `json:"-"`
}

func NewSchema

func NewSchema(schema interface{}) *Schema

type Schemater

type Schemater interface {
	GetSchema() *Schema
	SetTypeName(string)
	GetType() reflect.Kind
	SetFormat(string)
}

type Summarer

type Summarer interface {
	// SetDescription - sets a description of endpoint
	SetSummary(d string) ParameterAndResponser
}

type Swagger

type Swagger interface {
	ReadDoc() string
}

Swagger is a interface to read swagger document.

type TypeDictElement

type TypeDictElement struct {
	TypeName string
	Format   string
}

Jump to

Keyboard shortcuts

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