Documentation
¶
Index ¶
- Constants
- Variables
- func BindFromRequest(request *http.Request, resource any, scope string) gomerr.Gomerr
- func BindToResponse(result reflect.Value, header http.Header, scope string, acceptLanguage string, ...) ([]byte, int)
- func Configure(options ...BindingOption)
- func SetBindFromRequestConfiguration(requestConfiguration BindFromRequestConfiguration) *structs.Tool
- func SetBindToResponseConfiguration(responseConfiguration BindToResponseConfiguration) *structs.Tool
- type BindDirectiveConfiguration
- type BindFromRequestConfiguration
- type BindToResponseConfiguration
- type BindingConfiguration
- type BindingOption
- type EmptyValueHandlingType
- type Marshal
- type Marshaler
- type Method
- type Op
- type StatusCoder
- type Unmarshal
- type UnroutableError
Constants ¶
const ( DefaultContentType = "application/json" DefaultPathBindingPrefix = "path." DefaultHeaderBindingPrefix = "header." DefaultQueryParamBindingPrefix = "query." DefaultPayloadBindingPrefix = "" DefaultSkipFieldDirective = "-" DefaultBindToFieldNameDirective = "+" DefaultBodyBindingDirective = "body" DefaultOmitEmptyDirective = "omitempty" DefaultIncludeEmptyDirective = "includeempty" DefaultEmptyValueHandlingDefault = OmitEmpty ContentTypeHeader = "Content-Type" AcceptsHeader = "Accepts" AcceptLanguageKey = "$_accept_language" )
const ( PutCollection = MethodPut + collection PostCollection = MethodPost + collection GetCollection = MethodGet + collection PatchCollection = MethodPatch + collection DeleteCollection = MethodDelete + collection HeadCollection = MethodHead + collection OptionsCollection = MethodOptions + collection PutInstance = MethodPut + instance PostInstance = MethodPost + instance GetInstance = MethodGet + instance PatchInstance = MethodPatch + instance DeleteInstance = MethodDelete + instance HeadInstance = MethodHead + instance OptionsInstance = MethodOptions + instance )
const InvalidHttpOp = 0b00000000
const StatusLimitExceeded = 402
Variables ¶
var (
DefaultBindFromRequestTool *structs.Tool
)
Functions ¶
func BindFromRequest ¶
BindFromRequest binds request data to the provided resource.
func BindToResponse ¶
func BindToResponse(result reflect.Value, header http.Header, scope string, acceptLanguage string, statusCode int) ([]byte, int)
BindToResponse TODO: add support for data format type
func Configure ¶ added in v0.2.0
func Configure(options ...BindingOption)
Configure applies options to the global default binding configuration for both request and response. This is the simplest way to configure HTTP binding.
Example:
http.Configure(http.CamelCaseFields) http.Configure(http.CamelCaseFields, http.OmitEmptyValues)
func SetBindFromRequestConfiguration ¶
func SetBindFromRequestConfiguration(requestConfiguration BindFromRequestConfiguration) *structs.Tool
func SetBindToResponseConfiguration ¶
func SetBindToResponseConfiguration(responseConfiguration BindToResponseConfiguration) *structs.Tool
Types ¶
type BindDirectiveConfiguration ¶
type BindDirectiveConfiguration struct {
// Default prefixes for qualified directives
PathBindingPrefix string
HeaderBindingPrefix string
QueryParamBindingPrefix string
PayloadBindingPrefix string
// Default values for unqualified directives
SkipField string
IncludeField string
BindBody string
OmitEmptyDirective string
IncludeEmptyDirective string
// Defines how an empty value is marshaled unless overridden by OmitEmptyDirective or IncludeEmptyDirective. Default
// is to omit.
EmptyValueHandlingDefault EmptyValueHandlingType
}
func NewBindDirectiveConfiguration ¶
func NewBindDirectiveConfiguration() BindDirectiveConfiguration
type BindFromRequestConfiguration ¶
type BindFromRequestConfiguration struct {
BindConfiguration bind.Configuration
BindDirectiveConfiguration
// contains filtered or unexported fields
}
BindFromRequestConfiguration TODO: add config option mechanism...
func NewBindFromRequestConfiguration ¶
func NewBindFromRequestConfiguration() BindFromRequestConfiguration
type BindToResponseConfiguration ¶
type BindToResponseConfiguration struct {
BindConfiguration bind.Configuration
BindDirectiveConfiguration
// contains filtered or unexported fields
}
BindToResponseConfiguration TODO: add config option mechanism...
func NewBindToResponseConfiguration ¶
func NewBindToResponseConfiguration() BindToResponseConfiguration
type BindingConfiguration ¶ added in v0.2.0
type BindingConfiguration struct {
// contains filtered or unexported fields
}
BindingConfiguration holds unified configuration for request/response binding.
func NewBindingConfiguration ¶ added in v0.2.0
func NewBindingConfiguration(options ...BindingOption) *BindingConfiguration
NewBindingConfiguration creates a new BindingConfiguration with the provided options.
func (*BindingConfiguration) SetAsDefault ¶ added in v0.2.0
func (bc *BindingConfiguration) SetAsDefault() *BindingConfiguration
SetAsDefault sets this configuration as the global default for both request and response binding. Returns the configuration for method chaining.
type BindingOption ¶ added in v0.2.0
type BindingOption func(*BindingConfiguration)
BindingOption configures HTTP binding for both request and response.
var ( // CamelCaseFields configures field names to use camelCase in JSON (e.g., "firstName"). CamelCaseFields BindingOption = wrapBindOption(bind.CamelCaseFields) // PascalCaseFields configures field names to use PascalCase in JSON (e.g., "FirstName"). // This is the default behavior. PascalCaseFields BindingOption = wrapBindOption(bind.PascalCaseFields) // OmitEmptyValues configures binding to omit empty/zero values from output. // This is the default behavior. OmitEmptyValues BindingOption = wrapBindOption(bind.OmitEmpty) // IncludeEmptyValues configures binding to include empty/zero values in output. IncludeEmptyValues BindingOption = wrapBindOption(bind.IncludeEmpty) )
Re-exported options from bind package for convenient access.
func RequestOption ¶ added in v0.2.0
func RequestOption(opts ...BindingOption) BindingOption
RequestOption returns a BindingOption that applies the given options only to request binding. Use this when you need different configuration for requests vs responses.
Example:
http.Configure(
http.CamelCaseFields, // applies to both
http.RequestOption(http.IncludeEmptyValues), // override for request only
)
func ResponseOption ¶ added in v0.2.0
func ResponseOption(opts ...BindingOption) BindingOption
ResponseOption returns a BindingOption that applies the given options only to response binding. Use this when you need different configuration for requests vs responses.
Example:
http.Configure(
http.CamelCaseFields, // applies to both
http.ResponseOption(http.OmitEmptyValues), // override for response only
)
type EmptyValueHandlingType ¶
type EmptyValueHandlingType int
const ( OmitEmpty EmptyValueHandlingType = iota IncludeEmpty )
type Marshal ¶
Marshal provides a function to convert the toMarshal to bytes suitable for returning in a response body.
type Method ¶
type Method = Op
const ( MethodPut Method // 0b00000001 MethodPost // 0b00000010 MethodGet // 0b00000011 MethodPatch // 0b00000100 MethodDelete // 0b00000101 MethodHead // 0b00000110 MethodOptions // 0b00000111 )
type Op ¶
type Op byte
Op has 8 bits where the bottom five bits correspond to the action type, the sixth and seventh bit specifies what resource type the action is against (instance or a collection; a singleton counts as an instance) and the highest bit indicates if the op is built-in or customer-defined. The seventh bit is reserved. Use NewOp rather than try to construct manually to allow for the introduction of new, built-in Method values.
func (Op) ResourceType ¶
type StatusCoder ¶
type StatusCoder interface {
StatusCode() int
}
type Unmarshal ¶
Unmarshal defines a function that processes the input and stores the result in the value pointed to by ptrToTarget. If ptrToTarget is nil, not a pointer, or otherwise unprocessable, Unmarshal returns a gomerr.Gomerr.
type UnroutableError ¶ added in v0.4.4
func Unroutable ¶ added in v0.4.4
func Unroutable() *UnroutableError