Documentation ¶
Overview ¶
Package sedoc is go self documented api package.
Example (Advanced) ¶
package main import ( "encoding/xml" "fmt" "log" "time" sedoc "github.com/nsemikov/go-sedoc" ) func main() { // create api api := sedoc.New() api.Description = "My self documented API" // create commands infoCmd := sedoc.Command{ Name: "info", Handler: func(c sedoc.Context) error { c.Response().Result = fmt.Sprintf("%s v%s", "mysrv", "1.0.0") return nil }, } // add examples to command (optionaly) infoCmd.Examples = sedoc.Examples{ sedoc.Example{ Name: "simple", Request: sedoc.ExampleRequest{ Object: sedoc.Request{ Datetime: func() time.Time { t, _ := time.Parse(time.RFC3339, "2018-10-16T09:58:03.487508407Z"); return t }(), Command: "info", }, }, Responses: sedoc.ExampleResponses{ sedoc.ExampleResponse{ Name: "simple", Object: sedoc.Response{ Datetime: func() time.Time { t, _ := time.Parse(time.RFC3339, "2018-10-16T09:58:03.487508407Z"); return t }(), Command: "info", Result: "MyService v0.1.0", }, }, }, }, } // add command to api api.AddCommand(infoCmd) // create request request := sedoc.NewRequest() request.Command = "help" // and execute it response := api.Execute(request) // at last marshal response to XML (currently supported are XML, JSON and YAML) if b, err := xml.MarshalIndent(response, "", " "); err != nil { log.Fatal(err) } else { log.Println(string(b)) } }
Output:
Example (Arguments) ¶
Use Arguments for other request parameters
package main import ( sedoc "github.com/nsemikov/go-sedoc" "github.com/nsemikov/go-sedoc/argument" ) var api = sedoc.New() func main() { // ... api.AddCommand(sedoc.Command{ Name: "user.get", Description: "Get existed single user or user list.", Handler: func(c sedoc.Context) error { // ... return nil }, Arguments: sedoc.Arguments{ argument.Count, argument.Offset, }, }) // ... }
Output:
Example (Basic) ¶
Here is basic example of sedoc api usage
package main import ( sedoc "github.com/nsemikov/go-sedoc" ) func main() { // ... a := sedoc.New() a.Description = "My Service API" a.AddCommand(sedoc.Command{ Name: "info", Description: "Get information about service.", Handler: func(c sedoc.Context) error { c.Response().Result = &struct { Name string `json:"name"` Description string `json:"description"` Version string `json:"version"` }{ Name: "mysrv", Description: "My Service", Version: "1.0.0", } return nil }, }) // ... }
Output:
Example (Set) ¶
Use Set for arguments that must be setted to new or exist items
package main import ( sedoc "github.com/nsemikov/go-sedoc" "github.com/nsemikov/go-sedoc/argument" ) var api = sedoc.New() func main() { // ... api.AddCommand(sedoc.Command{ Name: "user.add", Description: "Create new user (by another one).", Handler: func(c sedoc.Context) error { // ... return nil }, Set: sedoc.Arguments{ sedoc.Argument{ Name: "login", Type: sedoc.ArgumentTypeString, Description: "Login string.", Required: true, }, sedoc.Argument{ Name: "password", Type: sedoc.ArgumentTypeString, Description: "Password string.", Required: true, }, argument.Email, // you can use some args from "github.com/nsemikov/go-sedoc/argument" argument.Name, // you can use some args from "github.com/nsemikov/go-sedoc/argument" }, }) // ... }
Output:
Example (Where) ¶
Use Where for arguments which must be used to find exist items
package main import ( sedoc "github.com/nsemikov/go-sedoc" "github.com/nsemikov/go-sedoc/argument" ) var api = sedoc.New() func main() { // ... api.AddCommand(sedoc.Command{ Name: "user.get", Description: "Get existed single user or user list.", Handler: func(c sedoc.Context) error { // ... return nil }, Where: sedoc.Arguments{ argument.Required(argument.Count), }, }) // ... }
Output:
Index ¶
- Constants
- Variables
- func MarshallerXML(arr []interface{}) func(*xml.Encoder, xml.StartElement) error
- func SetArgumentParser(t ArgumentType, p ArgumentParser)
- type API
- func (api *API) AddCommand(cmd Command)
- func (api *API) Execute(request *Request) *Response
- func (api *API) GetCommand(name string) Command
- func (api API) NewError(code int, details ...interface{}) *Error
- func (api API) NewErrorInternal(code int, internal error, details ...interface{}) *Error
- func (api *API) RemoveCommand(name string)
- func (api API) RequestFromURL(r *Request, u *url.URL) *Request
- func (api *API) Use(middleware ...MiddlewareFunc)
- type Argument
- type ArgumentParser
- type ArgumentType
- type Arguments
- type Command
- type Commands
- type Context
- type CustomErrorHandlerFunc
- type Error
- type Errors
- type Example
- type ExampleRequest
- type ExampleResponse
- type ExampleResponses
- type Examples
- type HandlerFunc
- type InterfaceMap
- type MiddlewareFunc
- type Request
- type Response
- type StringMap
Examples ¶
Constants ¶
const ( // ErrUnknown means an unknown error occurred ErrUnknown = iota + 1 // ErrInvalidRequest means catched request is invalid ErrInvalidRequest // ErrUnknownCommand means catched unknown command ErrUnknownCommand // ErrInvalidArgumentRegExp means invalid command argument parameter regexp ErrInvalidArgumentRegExp // ErrArgumentRegExpMatchFails means match command argument parameter regexp fails ErrArgumentRegExpMatchFails // ErrRequiredArgumentMissing means require command argument parameter missing ErrRequiredArgumentMissing // ErrUnknownArgument means unknown command argument parameter in request ErrUnknownArgument // ErrInvalidArgumentValue means invalid command argument parameter value ErrInvalidArgumentValue // LastUsedErrorCode is last error code used in sedoc LastUsedErrorCode = 100 )
Variables ¶
var DefaultErrors = Errors{ {Code: ErrUnknown, Description: "unknown error occurred"}, {Code: ErrInvalidRequest, Description: "can't parse request"}, {Code: ErrUnknownCommand, Description: "unknown command"}, {Code: ErrInvalidArgumentRegExp, Description: "invalid command argument parameter regexp"}, {Code: ErrArgumentRegExpMatchFails, Description: "match command argument parameter regexp fails"}, {Code: ErrRequiredArgumentMissing, Description: "require command argument parameter missing"}, {Code: ErrUnknownArgument, Description: "unknown command argument parameter in request"}, {Code: ErrInvalidArgumentValue, Description: "invalid command argument parameter value"}, }
DefaultErrors is map of Mrror
Functions ¶
func MarshallerXML ¶
func MarshallerXML(arr []interface{}) func(*xml.Encoder, xml.StartElement) error
MarshallerXML for marshal []interface{} into XML
func SetArgumentParser ¶
func SetArgumentParser(t ArgumentType, p ArgumentParser)
SetArgumentParser func
Types ¶
type API ¶
type API struct { Description string `json:"description,omitempty" xml:"description,attr,omitempty" yaml:"description,omitempty"` RequestFormat Arguments `json:"request_format,omitempty" xml:"request_format,omitempty" yaml:"request_format,omitempty"` ResponseFormat Arguments `json:"response_format,omitempty" xml:"response_format,omitempty" yaml:"response_format,omitempty"` Commands Commands `json:"commands,omitempty" xml:"commands,omitempty" yaml:"commands,omitempty"` Errors Errors `json:"errors,omitempty" xml:"errors,omitempty" yaml:"errors,omitempty"` PrefixArguments string `json:"-" xml:"-" yaml:"-"` PrefixSet string `json:"-" xml:"-" yaml:"-"` PrefixWhere string `json:"-" xml:"-" yaml:"-"` ErrorHandler CustomErrorHandlerFunc `json:"-" xml:"-" yaml:"-"` // contains filtered or unexported fields }
API struct
func (*API) AddCommand ¶
AddCommand append handler with help to API. AddCommand will rewrite command with the same name
func (API) NewErrorInternal ¶
NewErrorInternal is Error constructor
func (*API) RemoveCommand ¶
RemoveCommand remove command from API
func (API) RequestFromURL ¶
RequestFromURL func
type Argument ¶
type Argument struct { XMLName xml.Name `json:"-" xml:"arg" yaml:"-"` Name string `json:"name" xml:"name,attr" yaml:"name"` Type ArgumentType `json:"type" xml:"type,attr" yaml:"type"` Description string `json:"description" xml:"description,attr" yaml:"description"` Nullable bool `json:"nullable,omitempty" xml:"nullable,attr,omitempty" yaml:"nullable,omitempty"` Multiple bool `json:"multiple,omitempty" xml:"multiple,attr,omitempty" yaml:"multiple,omitempty"` Required bool `json:"required,omitempty" xml:"required,attr,omitempty" yaml:"required,omitempty"` Disabled bool `json:"-" xml:"-" yaml:"-"` RegExp string `json:"regexp,omitempty" xml:"regexp,attr,omitempty" yaml:"regexp,omitempty"` }
Argument is api command argument
type ArgumentParser ¶
ArgumentParser is parser for Argument
type ArgumentType ¶
type ArgumentType string
ArgumentType is enum of supported types of Argument
const ( // ArgumentTypeBoolean is boolean Argument type ArgumentTypeBoolean ArgumentType = "boolean" // ArgumentTypeInteger is integer Argument type ArgumentTypeInteger ArgumentType = "integer" // ArgumentTypeFloat is integer Argument type ArgumentTypeFloat ArgumentType = "float" // ArgumentTypeString is string Argument type ArgumentTypeString ArgumentType = "string" // ArgumentTypeDuration is duration Argument type ArgumentTypeDuration ArgumentType = "duration" // ArgumentTypeUUID is duration Argument type ArgumentTypeUUID ArgumentType = "uuid" // ArgumentTypeTime is datetime Argument type ArgumentTypeTime ArgumentType = "datetime" // ArgumentTypeList is duration Argument type ArgumentTypeList ArgumentType = "list" )
func (ArgumentType) Parse ¶
func (t ArgumentType) Parse(v interface{}, list bool) (r interface{}, err error)
Parse func
func (ArgumentType) String ¶
func (t ArgumentType) String() string
String is string convertor for ArgumentType
type Arguments ¶
type Arguments []Argument
Arguments is array of Argument
func (Arguments) MarshalXML ¶
MarshalXML for marshal into XML
type Command ¶
type Command struct { XMLName xml.Name `json:"-" xml:"command" yaml:"-"` Name string `json:"name" xml:"name,attr" yaml:"name"` Description string `json:"description" xml:"description,attr" yaml:"description"` Arguments Arguments `json:"args,omitempty" xml:"args,omitempty" yaml:"args,omitempty"` Where Arguments `json:"where,omitempty" xml:"where,omitempty" yaml:"where,omitempty"` Set Arguments `json:"set,omitempty" xml:"set,omitempty" yaml:"set,omitempty"` Handler HandlerFunc `json:"-" xml:"-" yaml:"-"` Examples Examples `json:"examples,omitempty" xml:"examples,omitempty" yaml:"examples,omitempty"` }
Command is api command
type Commands ¶
type Commands []Command
Commands is array of Command
func (Commands) MarshalXML ¶
MarshalXML for marshal into XML
type Context ¶
type Context interface { // Request return instance of Request Request() *Request // Response return instance of Response Response() *Response // Command return copy of Command Command() *Command // Error method Error(code int, details ...interface{}) *Error // ErrorInternal method ErrorInternal(code int, internal error, details ...interface{}) *Error }
Context interface
Example (Custom) ¶
package main import ( "errors" sedoc "github.com/nsemikov/go-sedoc" "github.com/nsemikov/go-sedoc/argument" ) var api = sedoc.New() func main() { // ... type mycontext struct { sedoc.Context } // ... api.Use(func(next sedoc.HandlerFunc) sedoc.HandlerFunc { return func(c sedoc.Context) (err error) { return next(&mycontext{c}) } }) // ... api.AddCommand(sedoc.Command{ Name: "signin", Handler: func(c sedoc.Context) error { cc, ok := c.(*mycontext) if !ok || cc == nil { return errors.New("invalid context") } // ... return nil }, Set: sedoc.Arguments{ argument.Required(argument.Login), argument.Required(argument.Login), }, }) // ... }
Output:
type CustomErrorHandlerFunc ¶
CustomErrorHandlerFunc func
type Error ¶
type Error struct { XMLName xml.Name `json:"-" xml:"error" yaml:"-"` Code int `json:"code" xml:"code,attr" yaml:"code"` Description string `json:"desc" xml:"desc,attr" yaml:"desc"` Internal error `json:"-" xml:"-" yaml:"-"` }
Error is standard quickq error type
type Errors ¶
type Errors []Error
Errors is array of Error
Example (Custom) ¶
package main import ( sedoc "github.com/nsemikov/go-sedoc" ) var api = sedoc.New() func main() { // ... const ( ErrCommandNotImplemented = iota + sedoc.LastUsedErrorCode ErrAuthRequired ErrAuth ) var ErrorMap = sedoc.Errors{ {Code: ErrCommandNotImplemented, Description: "Command not implemented yet"}, {Code: ErrAuthRequired, Description: "No credentials set"}, {Code: ErrAuth, Description: "Auth error"}, } // ... api.Errors = append(sedoc.DefaultErrors, ErrorMap...) // ... api.AddCommand(sedoc.Command{ Name: "exec", Handler: func(c sedoc.Context) error { // ... return c.Error(ErrCommandNotImplemented, "exec") }, }) // ... }
Output:
func (Errors) MarshalXML ¶
MarshalXML for marshal into XML
type Example ¶
type Example struct { XMLName xml.Name `json:"-" xml:"example" yaml:"-"` Name string `json:"name,omitempty" xml:"name,attr,omitempty" yaml:"name,omitempty"` Description string `json:"description,omitempty" xml:"description,attr,omitempty" yaml:"description,omitempty"` Request ExampleRequest `json:"request,omitempty" xml:"request,omitempty" yaml:"request,omitempty"` Responses ExampleResponses `json:"responses,omitempty" xml:"responses,omitempty" yaml:"responses,omitempty"` }
Example using for show how to use command
Example (Usage) ¶
Use Examples to show how to use your API
package main import ( sedoc "github.com/nsemikov/go-sedoc" "github.com/nsemikov/go-sedoc/argument" ) var api = sedoc.New() func main() { // ... api.AddCommand(sedoc.Command{ Name: "signin", Description: "Sign in (login). Create new session.", Where: sedoc.Arguments{ argument.Required(argument.Login), argument.Required(argument.Password), }, Handler: func(c sedoc.Context) error { // ... return nil }, Examples: []sedoc.Example{ sedoc.Example{ Name: "valid", Description: "valid signin request", Request: sedoc.ExampleRequest{ Object: *sedoc.NewRequest(), // Your request example here }, Responses: []sedoc.ExampleResponse{ sedoc.ExampleResponse{ Object: *sedoc.NewResponse(), // Your response example here }, sedoc.ExampleResponse{ Description: "user not found or password incorrect", Object: *sedoc.NewResponse(), // Your response example here }, }, }, sedoc.Example{ Name: "invalid", Description: "invalid signin request (password is not present)", Request: sedoc.ExampleRequest{ Object: *sedoc.NewRequest(), // Your request example here }, Responses: []sedoc.ExampleResponse{ sedoc.ExampleResponse{ Object: *sedoc.NewResponse(), // Your response example here }, }, }, }, }) // ... }
Output:
type ExampleRequest ¶
type ExampleRequest struct { XMLName xml.Name `json:"-" xml:"request" yaml:"-"` Object Request `json:"request" yaml:"request"` JSON string `json:"json,omitempty" xml:"json,omitempty" yaml:"json,omitempty"` XML string `json:"xml,omitempty" xml:"xml,omitempty" yaml:"xml,omitempty"` YAML string `json:"yaml,omitempty" xml:"yaml,omitempty" yaml:"yaml,omitempty"` }
ExampleRequest using for show how to use command
func (ExampleRequest) JSONString ¶
func (er ExampleRequest) JSONString() (result string)
JSONString method
func (ExampleRequest) XMLString ¶
func (er ExampleRequest) XMLString() (result string)
XMLString method
func (ExampleRequest) YAMLString ¶
func (er ExampleRequest) YAMLString() (result string)
YAMLString method
type ExampleResponse ¶
type ExampleResponse struct { XMLName xml.Name `json:"-" xml:"response" yaml:"-"` Name string `json:"name,omitempty" xml:"name,attr,omitempty" yaml:"name,omitempty"` Description string `json:"description,omitempty" xml:"description,attr,omitempty" yaml:"description,omitempty"` Object Response `json:"response" yaml:"response"` JSON string `json:"json,omitempty" xml:"json,omitempty" yaml:"json,omitempty"` XML string `json:"xml,omitempty" xml:"xml,omitempty" yaml:"xml,omitempty"` YAML string `json:"yaml,omitempty" xml:"yaml,omitempty" yaml:"yaml,omitempty"` }
ExampleResponse using for show how to use command
func (ExampleResponse) JSONString ¶
func (er ExampleResponse) JSONString() (result string)
JSONString method
func (ExampleResponse) XMLString ¶
func (er ExampleResponse) XMLString() (result string)
XMLString method
func (ExampleResponse) YAMLString ¶
func (er ExampleResponse) YAMLString() (result string)
YAMLString method
type ExampleResponses ¶
type ExampleResponses []ExampleResponse
ExampleResponses is array of Command
func (ExampleResponses) MarshalXML ¶
func (arr ExampleResponses) MarshalXML(e *xml.Encoder, start xml.StartElement) error
MarshalXML for marshal into XML
type Examples ¶
type Examples []Example
Examples is array of Command
func (Examples) MarshalXML ¶
MarshalXML for marshal into XML
type InterfaceMap ¶
type InterfaceMap map[string]interface{}
InterfaceMap is a map[string]interface{}
func (InterfaceMap) MarshalXML ¶
func (s InterfaceMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error
MarshalXML marshals InterfaceMap into XML
func (*InterfaceMap) UnmarshalXML ¶
func (s *InterfaceMap) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
UnmarshalXML marshals InterfaceMap into XML
type MiddlewareFunc ¶
type MiddlewareFunc func(HandlerFunc) HandlerFunc
MiddlewareFunc func
Example (Usage) ¶
package main import ( sedoc "github.com/nsemikov/go-sedoc" ) var api = sedoc.New() func main() { // ... api.Use(func(next sedoc.HandlerFunc) sedoc.HandlerFunc { return func(c sedoc.Context) error { // your code here return next(c) } }) // ... }
Output:
type Request ¶
type Request struct { XMLName xml.Name `json:"-" xml:"request" yaml:"-"` ID string `json:"id,omitempty" xml:"id,attr,omitempty" yaml:"id,omitempty"` Datetime time.Time `json:"datetime,omitempty" xml:"datetime,attr,omitempty" yaml:"datetime,omitempty"` Session string `json:"session,omitempty" xml:"session,attr,omitempty" yaml:"session,omitempty"` Command string `json:"command,omitempty" xml:"command,attr" yaml:"command,omitempty"` Arguments InterfaceMap `json:"args,omitempty" xml:"args,omitempty" yaml:"args,omitempty"` Where []InterfaceMap `json:"where,omitempty" xml:"where,omitempty" yaml:"where,omitempty"` Set InterfaceMap `json:"set,omitempty" xml:"set,omitempty" yaml:"set,omitempty"` }
Request contains request fields
type Response ¶
type Response struct { XMLName xml.Name `json:"-" xml:"response" yaml:"-"` ID string `json:"id,omitempty" xml:"id,attr,omitempty" yaml:"id,omitempty"` Datetime time.Time `json:"datetime,omitempty" xml:"datetime,attr,omitempty" yaml:"datetime,omitempty"` Session string `json:"session,omitempty" xml:"session,attr,omitempty" yaml:"session,omitempty"` Command string `json:"command,omitempty" xml:"command,attr" yaml:"command,omitempty"` Arguments InterfaceMap `json:"args,omitempty" xml:"args,omitempty" yaml:"args,omitempty"` Result interface{} `json:"result,omitempty" xml:"result,omitempty" yaml:"result,omitempty"` Error *Error `json:"error,omitempty" xml:"error,omitempty" yaml:"error,omitempty"` }
Response contains response fields
type StringMap ¶
StringMap is a map[string]string
func (StringMap) MarshalXML ¶
MarshalXML marshals StringMap into XML
func (*StringMap) UnmarshalXML ¶
UnmarshalXML marshals StringMap into XML