descriptor

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2022 License: Apache-2.0 Imports: 11 Imported by: 4

Documentation

Overview

Package descriptor the idl descriptor for describe the idls with golang

Index

Constants

This section is empty.

Variables

View Source
var (
	// APIQueryAnnotation api.path annotation
	APIQueryAnnotation = NewBAMAnnotation("api.query", NewAPIQuery)
	// APIPathAnnotation api.path annotation
	APIPathAnnotation = NewBAMAnnotation("api.path", NewAPIPath)
	// APIHeaderAnnotation api.header annotation
	APIHeaderAnnotation = NewBAMAnnotation("api.header", NewAPIHeader)
	// APICookieAnnotation api.cookie annotation
	APICookieAnnotation = NewBAMAnnotation("api.cookie", NewAPICookie)
	// APIBodyAnnotation api.body annotation
	APIBodyAnnotation = NewBAMAnnotation("api.body", NewAPIBody)
	// APIHttpCodeAnnotation api.http_code annotation
	APIHttpCodeAnnotation = NewBAMAnnotation("api.http_code", NewAPIHTTPCode)
	// APINoneAnnotation api.none annotation
	APINoneAnnotation = NewBAMAnnotation("api.none", NewAPINone)
	// APIRawBodyAnnotation api.raw_body annotation
	APIRawBodyAnnotation = NewBAMAnnotation("api.raw_body", NewAPIRawBody)
)
View Source
var (
	// APIGetAnnotation api.get annotation
	APIGetAnnotation = NewBAMAnnotation("api.get", NewAPIGet)
	// APIPostAnnotation api.post annotation
	APIPostAnnotation = NewBAMAnnotation("api.post", NewAPIPost)
	// APIPutAnnotation api.put annotation
	APIPutAnnotation = NewBAMAnnotation("api.put", NewAPIPut)
	// APIDeleteAnnotation api.delete annotation
	APIDeleteAnnotation = NewBAMAnnotation("api.delete", NewAPIDelete)
)
View Source
var APIJSConvAnnotation = NewBAMAnnotation("api.js_conv", NewAPIJSConv)

APIJSConvAnnotation api.js_conv annotation

View Source
var DefaultNewMapping = NewAPIBody

DefaultNewMapping the default mapping creator

View Source
var GoTagAnnatition = NewBAMAnnotation("go.tag", NewGoTag)

GoTagAnnatition go.tag annatation define

Functions

func FindAnnotation

func FindAnnotation(key, value string) (interface{}, bool)

FindAnnotation search an annotation by given key/value

func RegisterAnnotation

func RegisterAnnotation(an Annotation)

RegisterAnnotation register an annotation for parser

Types

type Annotation

type Annotation interface {
	// Equal assert the given key/value is this Annotation
	Equal(key, value string) bool // for search
	// Handle the handle function of the Annotation
	Handle() interface{} // one of NewHttpMapping/NewKeyMapping/NewValueMapping/NewRoute
}

Annotation idl annotation interface

func NewBAMAnnotation

func NewBAMAnnotation(key string, handle interface{}) Annotation

NewBAMAnnotation create a bam annotation

func NewNoneAnnotation

func NewNoneAnnotation(key string) Annotation

NewNoneAnnotation create do nothing annotation

func NewNoneWithValueAnnotation

func NewNoneWithValueAnnotation(key, value string) Annotation

NewNoneWithValueAnnotation create do nothing annotation

type Cookies

type Cookies map[string]string

Cookies ...

type FieldDescriptor

type FieldDescriptor struct {
	Name         string // field name
	Alias        string // alias name
	ID           int32
	Required     bool
	IsException  bool
	Type         *TypeDescriptor
	HTTPMapping  HTTPMapping
	ValueMapping ValueMapping
}

FieldDescriptor idl field descriptor

func (*FieldDescriptor) FieldName

func (d *FieldDescriptor) FieldName() string

FieldName return field name maybe with an alias

type FiledMapping

type FiledMapping interface {
	Handle(field *FieldDescriptor)
}

FiledMapping mapping handle for filed descriptor

type FunctionDescriptor

type FunctionDescriptor struct {
	Name           string
	Oneway         bool
	Request        *TypeDescriptor
	Response       *TypeDescriptor
	HasRequestBase bool
}

FunctionDescriptor idl function descriptor

type HTTPMapping

type HTTPMapping interface {
	// get value from request
	Request(ctx context.Context, req *HTTPRequest, field *FieldDescriptor) (interface{}, bool, error)
	// set value to response
	Response(ctx context.Context, resp *HTTPResponse, field *FieldDescriptor, val interface{}) error
}

HTTPMapping http mapping annotation

type HTTPRequest

type HTTPRequest struct {
	Header  http.Header
	Query   url.Values
	Cookies Cookies
	Method  string
	Host    string
	Path    string
	Params  *Params // path params
	RawBody []byte
	Body    map[string]interface{}
}

HTTPRequest ...

type HTTPResponse

type HTTPResponse struct {
	Header     http.Header
	StatusCode int32
	Body       map[string]interface{}
}

HTTPResponse ...

func NewHTTPResponse

func NewHTTPResponse() *HTTPResponse

NewHTTPResponse ...

func (*HTTPResponse) Write

func (resp *HTTPResponse) Write(w http.ResponseWriter) error

Write to ResponseWriter

type NewFieldMapping

type NewFieldMapping func(value string) FiledMapping

NewFieldMapping FiledMapping creator

var NewGoTag NewFieldMapping = func(value string) FiledMapping {
	return &goTag{reflect.StructTag(value)}
}

NewGoTag go.tag annotation creator

type NewHTTPMapping

type NewHTTPMapping func(value string) HTTPMapping

NewHTTPMapping HTTPMapping creator api.query = value

var NewAPIBody NewHTTPMapping = func(value string) HTTPMapping {
	return &apiBody{value}
}

NewAPIBody ...

var NewAPICookie NewHTTPMapping = func(value string) HTTPMapping {
	return &apiCookie{value}
}

NewAPICookie ...

var NewAPIHTTPCode NewHTTPMapping = func(value string) HTTPMapping {
	return &apiHTTPCode{}
}

NewAPIHTTPCode ...

var NewAPIHeader NewHTTPMapping = func(value string) HTTPMapping {
	return &apiHeader{value}
}

NewAPIHeader ...

var NewAPINone NewHTTPMapping = func(value string) HTTPMapping {
	return &apiNone{}
}

NewAPINone ...

var NewAPIPath NewHTTPMapping = func(value string) HTTPMapping {
	return &apiPath{value}
}

NewAPIPath ...

var NewAPIQuery NewHTTPMapping = func(value string) HTTPMapping {
	return &apiQuery{value}
}

NewAPIQuery ...

var NewAPIRawBody NewHTTPMapping = func(value string) HTTPMapping {
	return &apiRawBody{}
}

NewAPIRawBody ...

type NewRoute

type NewRoute func(value string, function *FunctionDescriptor) Route

NewRoute route creator

var NewAPIDelete NewRoute = func(value string, function *FunctionDescriptor) Route {
	return &apiRoute{http.MethodDelete, value, function}
}

NewAPIDelete ...

var NewAPIGet NewRoute = func(value string, function *FunctionDescriptor) Route {
	return &apiRoute{http.MethodGet, value, function}
}

NewAPIGet ...

var NewAPIPost NewRoute = func(value string, function *FunctionDescriptor) Route {
	return &apiRoute{http.MethodPost, value, function}
}

NewAPIPost ...

var NewAPIPut NewRoute = func(value string, function *FunctionDescriptor) Route {
	return &apiRoute{http.MethodPut, value, function}
}

NewAPIPut ...

type NewValueMapping

type NewValueMapping func(value string) ValueMapping

NewValueMapping ValueMapping creator

var NewAPIJSConv NewValueMapping = func(value string) ValueMapping {

	return &apiJSConv{}
}

NewAPIJSConv ...

type Param

type Param struct {
	Key   string
	Value string
}

Param in request path

type Params

type Params struct {
	// contains filtered or unexported fields
}

Params and recyclable

func (*Params) ByName

func (ps *Params) ByName(name string) string

ByName search Param by given name

func (*Params) Recycle

func (ps *Params) Recycle()

Recycle the Params

type Route

type Route interface {
	Method() string
	Path() string
	Function() *FunctionDescriptor
}

Route the route annotation

type Router

type Router interface {
	// Handle register Route to Router
	Handle(rt Route)
	// Lookup FunctionDescriptor from HTTPRequest
	Lookup(req *HTTPRequest) (*FunctionDescriptor, error)
}

Router http router for bam annotations

func NewRouter

func NewRouter() Router

NewRouter ...

type ServiceDescriptor

type ServiceDescriptor struct {
	Name      string
	Functions map[string]*FunctionDescriptor
	Router    Router
}

ServiceDescriptor idl service descriptor

func (*ServiceDescriptor) LookupFunctionByMethod

func (s *ServiceDescriptor) LookupFunctionByMethod(method string) (*FunctionDescriptor, error)

LookupFunctionByMethod lookup function by method

type StructDescriptor

type StructDescriptor struct {
	Name           string
	FieldsByID     map[int32]*FieldDescriptor
	FieldsByName   map[string]*FieldDescriptor
	RequiredFields map[int32]*FieldDescriptor
}

StructDescriptor idl struct descriptor

func (*StructDescriptor) CheckRequired

func (d *StructDescriptor) CheckRequired(rw map[int32]struct{}) error

CheckRequired check all required fields at the end of read or write

type Type

type Type byte

Type constants in the Thrift protocol

const (
	STOP   Type = 0
	VOID   Type = 1
	BOOL   Type = 2
	BYTE   Type = 3
	I08    Type = 3
	DOUBLE Type = 4
	I16    Type = 6
	I32    Type = 8
	I64    Type = 10
	STRING Type = 11
	UTF7   Type = 11
	STRUCT Type = 12
	MAP    Type = 13
	SET    Type = 14
	LIST   Type = 15
	UTF8   Type = 16
	UTF16  Type = 17
	// BINARY Type = 18   wrong and unusued
	JSON Type = 19
)

Types

func FromThriftTType

func FromThriftTType(t thrift.TType) Type

FromThriftTType ...

func (Type) String

func (p Type) String() string

String for format and print

func (Type) ToThriftTType

func (p Type) ToThriftTType() thrift.TType

ToThriftTType convert to thrift.TType

type TypeDescriptor

type TypeDescriptor struct {
	Name          string
	Type          Type
	Key           *TypeDescriptor   // for map key
	Elem          *TypeDescriptor   // for slice or map element
	Struct        *StructDescriptor // for struct
	IsRequestBase bool
}

TypeDescriptor idl type descriptor

type ValueMapping

type ValueMapping interface {
	Request(ctx context.Context, val interface{}, field *FieldDescriptor) (interface{}, error)
	Response(ctx context.Context, val interface{}, field *FieldDescriptor) (interface{}, error)
}

ValueMapping value mapping annotation

type Void

type Void struct{}

Void use empty struct as void instead of `nil`, because sometimes `nil` was used as optional none

Jump to

Keyboard shortcuts

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