sedoc

package module
v0.1.1-0...-f686da7 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2020 License: MIT Imports: 12 Imported by: 0

README

License GoDoc Build Status Coverage Status Go Report Card

Why another API package?

  • There are many REST API autodocumented packages, but not for JSON-pure or TCP
  • You may want use JSON, XML or YAML format over any transport (WebSocket, HTTP, TCP, UDP, etc)
  • API will be automatically documented

Simple example

package main

import (
  "encoding/xml"
  "fmt"
  "time"

  sedoc "github.com/nsemikov/go-sedoc"
)

const (
  MyAppName    = "MyService"
  MyAppVersion = "0.1.0"
)

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", MyAppName, MyAppVersion)
      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)
  // ...
  if b, err := xml.MarshalIndent(response, "", "  "); err != nil {
    log.Fatal(err)
  } else {
    log.Println(string(b))
  }
}

This simple example will print to stdout something like this:

<response datetime="2018-11-10T16:51:30.41952529Z" command="help">
  <result description="My self documented API">
    <request_format required="1" count="7">
      <arg name="id" type="string" description="Request identifier (for debugging)"></arg>
      <arg name="datetime" type="string" description="Datetime string (ISO 8601)"></arg>
      <arg name="session" type="uuid" description="Session token uuid, formatted like &#34;01234567-89ab-cdef-0123-456789abcdef&#34;"></arg>
      <arg name="command" type="string" description="Command name string" required="true"></arg>
      <arg name="args" type="object" description="Extra request parameters, one-level object"></arg>
      <arg name="where" type="array" description="Search item(s) parameters, simple Array of one-level objects"></arg>
      <arg name="set" type="object" description="Item(s) data to set, one-level object"></arg>
    </request_format>
    <response_format required="1" count="7">
      <arg name="id" type="string" description="Request identifier (for debugging)"></arg>
      <arg name="datetime" type="string" description="Datetime string (ISO 8601)"></arg>
      <arg name="session" type="uuid" description="Session token uuid, formatted like &#34;01234567-89ab-cdef-0123-456789abcdef&#34;"></arg>
      <arg name="command" type="string" description="Command name string" required="true"></arg>
      <arg name="args" type="object" description="Extra request parameters, one-level object"></arg>
      <arg name="result" type="object" description="Result object. For XML maybe used another name"></arg>
      <arg name="error" type="object" description="Error object. Contains `code` and `desc` fields"></arg>
    </response_format>
    <commands count="2">
      <command name="help" description="Get list of commands">
        <examples count="1">
          <example name="simple help" description="simple help command usage example">
            <request>
              <request datetime="2018-10-16T09:58:03.487508407Z" command="help"></request>
            </request>
          </example>
        </examples>
      </command>
      <command name="info" description="">
        <examples count="1">
          <example name="simple">
            <request>
              <request datetime="2018-10-16T09:58:03.487508407Z" command="info"></request>
            </request>
            <responses count="1">
              <response name="simple">
                <response datetime="2018-10-16T09:58:03.487508407Z" command="info">
                  <result>MyService v0.1.0</result>
                </response>
              </response>
            </responses>
          </example>
        </examples>
      </command>
    </commands>
    <errors>
      <error code="1" desc="unknown error occurred"></error>
      <error code="2" desc="can&#39;t parse request"></error>
      <error code="3" desc="unknown command"></error>
      <error code="4" desc="invalid command argument parameter regexp"></error>
      <error code="5" desc="match command argument parameter regexp fails"></error>
      <error code="6" desc="require command argument parameter missing"></error>
      <error code="7" desc="unknown command argument parameter in request"></error>
      <error code="8" desc="invalid command argument parameter value"></error>
    </errors>
  </result>
</response>

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

Examples

Constants

View Source
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

View Source
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 New

func New() (api *API)

New api constructor

func (*API) AddCommand

func (api *API) AddCommand(cmd Command)

AddCommand append handler with help to API. AddCommand will rewrite command with the same name

func (*API) Execute

func (api *API) Execute(request *Request) *Response

Execute command from API

func (*API) GetCommand

func (api *API) GetCommand(name string) Command

GetCommand return Command

func (API) NewError

func (api API) NewError(code int, details ...interface{}) *Error

NewError is Error constructor

func (API) NewErrorInternal

func (api API) NewErrorInternal(code int, internal error, details ...interface{}) *Error

NewErrorInternal is Error constructor

func (*API) RemoveCommand

func (api *API) RemoveCommand(name string)

RemoveCommand remove command from API

func (API) RequestFromURL

func (api API) RequestFromURL(r *Request, u *url.URL) *Request

RequestFromURL func

func (*API) Use

func (api *API) Use(middleware ...MiddlewareFunc)

Use add middleware

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

func (Argument) Match

func (arg Argument) Match(v interface{}) (bool, error)

Match func is matching stringify representation of v for Argument RegExp

type ArgumentParser

type ArgumentParser func(v interface{}, list bool) (r interface{}, err error)

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) Add

func (arr *Arguments) Add(arg Argument) error

Add is add argument to array

func (*Arguments) Contains

func (arr *Arguments) Contains(name string) bool

Contains Argument

func (*Arguments) Get

func (arr *Arguments) Get(name string) (Argument, error)

Get is get argument from array

func (Arguments) MarshalXML

func (arr Arguments) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML for marshal into XML

func (*Arguments) Remove

func (arr *Arguments) Remove(name string) error

Remove is remove argument from array

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

func (*Command) Valid

func (cmd *Command) Valid() bool

Valid func

type Commands

type Commands []Command

Commands is array of Command

func (*Commands) Add

func (arr *Commands) Add(cmd Command) error

Add is add command to array

func (*Commands) Contains

func (arr *Commands) Contains(name string) bool

Contains Command

func (*Commands) Get

func (arr *Commands) Get(name string) (Command, error)

Get is get command from array

func (Commands) MarshalXML

func (arr Commands) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML for marshal into XML

func (*Commands) Remove

func (arr *Commands) Remove(name string) error

Remove is remove command from array

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

type CustomErrorHandlerFunc func(error, Context)

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

func (Error) Error

func (e Error) Error() string

Error convert to string

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) Add

func (arr *Errors) Add(err Error)

Add is add error to array

func (*Errors) Contains

func (arr *Errors) Contains(code int) bool

Contains Error

func (*Errors) Get

func (arr *Errors) Get(code int) Error

Get is get error from array

func (Errors) MarshalXML

func (arr Errors) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML for marshal into XML

func (*Errors) Remove

func (arr *Errors) Remove(code int)

Remove is remove error from array

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

func (arr Examples) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML for marshal into XML

type HandlerFunc

type HandlerFunc func(Context) error

HandlerFunc func

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

func NewRequest

func NewRequest() *Request

NewRequest func

func (*Request) String

func (r *Request) String() string

String format Request as string

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

func NewResponse

func NewResponse() *Response

NewResponse func

func (*Response) String

func (r *Response) String() string

String format Response as string

type StringMap

type StringMap map[string]string

StringMap is a map[string]string

func (StringMap) MarshalXML

func (s StringMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals StringMap into XML

func (*StringMap) UnmarshalXML

func (s *StringMap) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML marshals StringMap into XML

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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