Documentation
¶
Overview ¶
Package swgen (Swagger Generator) is a library which helps to generate [Swagger Specification](http://swagger.io/specification/) in JSON format on-the-fly.
Index ¶
- Constants
- func GenDocument() ([]byte, error)
- func ReflectTypeHash(t reflect.Type) uint32
- func ReflectTypeReliableName(t reflect.Type) string
- func ResetDefinitions()
- func ResetPaths()
- func ServeHTTP(w http.ResponseWriter, r *http.Request)
- func SetPathItem(info PathItemInfo, params interface{}, body interface{}, response interface{}) error
- type ContactObj
- type Definition
- type Document
- type Enum
- type Generator
- func AddExtendedField(name string, value interface{}) *Generator
- func AddTypeMap(src interface{}, dst interface{}) *Generator
- func EnableCORS(b bool, allowHeaders ...string) *Generator
- func NewGenerator() *Generator
- func SetBasePath(basePath string) *Generator
- func SetContact(name, url, email string) *Generator
- func SetHost(host string) *Generator
- func SetInfo(title, description, term, version string) *Generator
- func SetLicense(name, url string) *Generator
- func (g *Generator) AddExtendedField(name string, value interface{}) *Generator
- func (g *Generator) AddSecurityDefinition(name string, def SecurityDef) *Generator
- func (g *Generator) AddTypeMap(src interface{}, dst interface{}) *Generator
- func (g *Generator) EnableCORS(b bool, allowHeaders ...string) *Generator
- func (g *Generator) GenDocument() ([]byte, error)
- func (g *Generator) IndentJSON(enabled bool) *Generator
- func (g *Generator) ParseDefinition(i interface{}) (schema SchemaObj, err error)
- func (g *Generator) ParseParameter(i interface{}) (name string, params []ParamObj, err error)
- func (g *Generator) ReflectGoTypes(enabled bool) *Generator
- func (g *Generator) ResetDefinitions()
- func (g *Generator) ResetPaths()
- func (g *Generator) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (g *Generator) SetBasePath(basePath string) *Generator
- func (g *Generator) SetContact(name, url, email string) *Generator
- func (g *Generator) SetHost(host string) *Generator
- func (g *Generator) SetInfo(title, description, term, version string) *Generator
- func (g *Generator) SetLicense(name, url string) *Generator
- func (g *Generator) SetPathItem(info PathItemInfo, params interface{}, body interface{}, response interface{}) error
- func (g *Generator) SetSchemes(schemes []string) *Generator
- type IDefinition
- type IParameter
- type InfoObj
- type LicenseObj
- type OperationObj
- type ParamItemObj
- type ParamObj
- type PathItem
- type PathItemInfo
- type ResponseObj
- type Responses
- type SchemaObj
- type SecurityDef
- type ServiceType
Examples ¶
Constants ¶
const ( // CommonNameInteger data type is integer, format int32 (signed 32 bits) CommonNameInteger commonName = "integer" // CommonNameLong data type is integer, format int64 (signed 64 bits) CommonNameLong commonName = "long" // CommonNameFloat data type is number, format float CommonNameFloat commonName = "float" // CommonNameDouble data type is number, format double CommonNameDouble commonName = "double" // CommonNameString data type is string CommonNameString commonName = "string" // CommonNameByte data type is string, format byte (base64 encoded characters) CommonNameByte commonName = "byte" // CommonNameBinary data type is string, format binary (any sequence of octets) CommonNameBinary commonName = "binary" // CommonNameBoolean data type is boolean CommonNameBoolean commonName = "boolean" // CommonNameDate data type is string, format date (As defined by full-date - RFC3339) CommonNameDate commonName = "date" // CommonNameDateTime data type is string, format date-time (As defined by date-time - RFC3339) CommonNameDateTime commonName = "dateTime" // CommonNamePassword data type is string, format password CommonNamePassword commonName = "password" )
const ( // SecurityBasicAuth is a HTTP Basic Authentication security type SecurityBasicAuth securityType = "basic" // SecurityAPIKey is an API key security type SecurityAPIKey securityType = "apiKey" // SecurityOAuth2 is an OAuth2 security type SecurityOAuth2 securityType = "oauth2" // SecurityHTTP is an http security type SecurityHTTP securityType = "http" )
const ( // APIKeyInHeader defines API key in header APIKeyInHeader apiKeyIn = "header" // APIKeyInQuery defines API key in query parameter APIKeyInQuery apiKeyIn = "query" )
const ( // Oauth2AccessCode is access code Oauth2 flow Oauth2AccessCode oauthFlow = "accessCode" // Oauth2Application is application Oauth2 flow Oauth2Application oauthFlow = "application" // Oauth2Implicit is implicit Oauth2 flow Oauth2Implicit oauthFlow = "implicit" // Oauth2Password is password Oauth2 flow Oauth2Password oauthFlow = "password" )
const ( AuthenticationSchemeBasic authenticationScheme = "basic" AuthenticationSchemeBearer authenticationScheme = "bearer" AuthenticationSchemeDigest authenticationScheme = "digest" AuthenticationSchemeHOBA authenticationScheme = "hoba" )
const (
BearerFormatJWT bearerFormat = "JWT"
)
Variables ¶
This section is empty.
Functions ¶
func GenDocument ¶
GenDocument returns document specification in JSON string (in []byte)
func ReflectTypeHash ¶ added in v0.3.0
ReflectTypeHash returns private (unexported) `hash` field of the Golang internal reflect.rtype struct for a given reflect.Type This hash is used to (quasi-)uniquely identify a reflect.Type value
func ReflectTypeReliableName ¶ added in v0.3.0
ReflectTypeReliableName returns real name of given reflect.Type, if it is non-empty, or auto-generates "anon_*"] name for anonymous structs
func ResetDefinitions ¶
func ResetDefinitions()
ResetDefinitions will remove all exists definitions and init again
func ServeHTTP ¶
func ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.HandleFunc to server swagger.json document
func SetPathItem ¶
func SetPathItem(info PathItemInfo, params interface{}, body interface{}, response interface{}) error
SetPathItem register path item with some information and input, output
Types ¶
type ContactObj ¶
type ContactObj struct {
Name string `json:"name"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
}
ContactObj contains contact information for the exposed API
type Definition ¶ added in v0.2.0
Definition is a helper that implements interface IDefinition
func (Definition) SwgenDefinition ¶ added in v0.2.0
func (s Definition) SwgenDefinition() (typeName string, typeDef SchemaObj, err error)
SwgenDefinition return type name and definition that was set
type Document ¶
type Document struct {
Version string `json:"swagger"` // Specifies the Swagger Specification version being used
Info InfoObj `json:"info"` // Provides metadata about the API
Host string `json:"host,omitempty"` // The host (name or ip) serving the API
BasePath string `json:"basePath,omitempty"` // The base path on which the API is served, which is relative to the host
Schemes []string `json:"schemes,omitempty"` // Values MUST be from the list: "http", "https", "ws", "wss"
Paths map[string]PathItem `json:"paths"` // The available paths and operations for the API
Definitions map[string]SchemaObj `json:"definitions"` // An object to hold data types produced and consumed by operations
SecurityDefinitions map[string]SecurityDef `json:"securityDefinitions,omitempty"` // An object to hold available security mechanisms
// contains filtered or unexported fields
}
Document represent for a document object of swagger data see http://swagger.io/specification/
func (*Document) AddExtendedField ¶
func (ad *Document) AddExtendedField(name string, value interface{})
AddExtendedField add field to additional data map
func (Document) MarshalJSON ¶
MarshalJSON marshal Document with additionalData inlined
type Enum ¶
type Enum struct {
Enum []interface{} `json:"enum,omitempty"`
EnumNames []string `json:"x-enum-names,omitempty"`
}
Enum can be use for sending Enum data that need validate
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator create swagger document
func AddExtendedField ¶ added in v0.2.0
AddExtendedField add vendor extension field to document
func AddTypeMap ¶
func AddTypeMap(src interface{}, dst interface{}) *Generator
AddTypeMap add rule to use dst interface instead of src
func EnableCORS ¶ added in v0.2.0
EnableCORS enable HTTP handler support CORS
func SetBasePath ¶
SetBasePath set host info for swagger specification
func SetContact ¶
SetContact set contact information for API
func SetLicense ¶
SetLicense set license information for API
func (*Generator) AddExtendedField ¶ added in v0.2.0
AddExtendedField add vendor extension field to document
func (*Generator) AddSecurityDefinition ¶ added in v0.3.0
func (g *Generator) AddSecurityDefinition(name