src

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultCodeInternal       = 1
	DefaultCodeUnknown        = 2
	DefaultCodeUnknownMessage = "unknown error"
)

Variables

This section is empty.

Functions

func ToJSON

func ToJSON(v Result, pretties ...bool) string

ToJSON convert v to json string.

Types

type Configuration

type Configuration struct {
	Type ResultType `json:"type" yaml:"type"`
	// contains filtered or unexported fields
}

Configuration is a component for library.

var Config *Configuration

Config is a singleton instance for library.

func (*Configuration) After

func (o *Configuration) After() error

type DataType

type DataType string

DataType is a type name for response result.

const (
	DataTypeError  DataType = "error"
	DataTypeList   DataType = "list"
	DataTypeObject DataType = "object"
	DataTypePaging DataType = "paging"
)

type Error

type Error struct {
	Code    int
	Message string
}

Error is a component used to define an error struct.

func NewError

func NewError(code int, format string, args ...any) Error

NewError creates a new error instance.

func (Error) Error

func (o Error) Error() string

func (Error) GetCode

func (o Error) GetCode() int

func (Error) GetMessage

func (o Error) GetMessage() string

type ErrorGetter

type ErrorGetter interface {
	Code() int
	Message() string
}

Error is a component used to define an error struct.

type Paginator

type Paginator struct {
	Body   any     `json:"body" xml:"body"`
	Paging *Paging `json:"paging" xml:"paging"`
}

Paginator is a paginator struct for response.

{
    "body": [
         {
             ...
         },
         {
             ...
         }
    ],
    "paging": {
        ...
    }
}

func NewPaginator

func NewPaginator(body any, total int64, page, size int) *Paginator

NewPaginator creates a paginator response struct.

type Paging

type Paging struct {
	// Total
	// is total amount of items.
	Total int64 `json:"total" xml:"total"`

	// Size
	// show items amount on each page.
	Size int `json:"size" xml:"size"`

	// First
	// shows the start page number of paginator.
	First int `json:"first" xml:"first"`

	// Prev
	// shows previous page number of paginator.
	Prev int `json:"prev" xml:"prev"`

	// Curr
	// shows the current page number of paginator.
	Curr int `json:"curr" xml:"curr"`

	// Next
	// shows the next page number of paginator.
	Next int `json:"next" xml:"next"`

	// Last
	// shows the last page number of paginator.
	Last int `json:"last" xml:"last"`
}

Paging is a component used to store paginator.

func NewPaging

func NewPaging(total int64, page int, size int) *Paging

NewPaging create a new paginator component.

type Result

type Result interface {
	IsHtml() bool
	IsString() bool

	// String
	// convert result as JSON string.
	String() string

	// With
	// generate response as normal with any on data field.
	With(data any) Result

	// WithCodeError
	// generate response as error with specified code and message.
	WithCodeError(code int, message string) Result

	// WithEmpty
	// generate response a simple struct without data field.
	WithEmpty() Result

	// WithError
	// generate response as error struct.
	WithError(err error) Result

	// WithHtml
	// generate response as error struct.
	WithHtml(body []byte) Result

	// WithList
	// generate response as listing on data field.
	WithList(items any) Result

	// WithPaging
	// generate response as paging with a paginator fields on data field.
	WithPaging(items any, total int64, page, size int) Result

	// WithString
	// generate response as string result.
	WithString(str string) Result
}

Result is a standard response operations.

type ResultConstructor

type ResultConstructor func() Result

ResultConstructor is a function used to creates a Result constructor.

type ResultType

type ResultType string

ResultType is a type name for response struct.

const (
	ResultTypeCode  ResultType = "code"
	ResultTypeErrno ResultType = "errno"

	DefaultResultType = ResultTypeErrno
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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