Documentation
¶
Index ¶
- Constants
- Variables
- func ContextErrorValue(ctx context.Context) (error, bool)
- func ContextObjectValue[T any](ctx context.Context) (T, bool)
- func Must(err ...error)
- func ReplaceGlobal(j Jayson)
- type Extension
- func ExtChain(extensions ...Extension) Extension
- func ExtConditional(condition Extension, ext ...Extension) Extension
- func ExtFirst(ext ...Extension) Extension
- func ExtFunc(extResponseWriter ExtensionApplyResponseWriterFunc, ...) Extension
- func ExtHeader(h http.Header) Extension
- func ExtHeaderValue(k string, v string) Extension
- func ExtNoop() Extension
- func ExtObjectKeyValue(key string, value any) Extension
- func ExtObjectKeyValuef(key string, format string, args ...any) Extension
- func ExtObjectUnwrap(obj any) Extension
- func ExtOmitObjectKey(keys ...string) Extension
- func ExtOmitSettingsKey(fn func(settings Settings) []string) Extension
- func ExtStatus(status int) Extension
- type ExtensionApplyResponseObjectFunc
- type ExtensionApplyResponseWriterFunc
- type Jayson
- type Settings
Constants ¶
const (
// DebugMaxCallerDepth is the maximum depth of debug caller
DebugMaxCallerDepth = 255
)
Variables ¶
var ( Warning = errors.New("jayson: warning") // ErrImproperlyConfigured is error returned when Jayson is improperly configured. ErrImproperlyConfigured = errors.New("jayson: improperly configured") ErrAlreadyRegistered = fmt.Errorf("%w: already registered", ErrImproperlyConfigured) WarnAlreadyRegistered = fmt.Errorf("%w: already registered", Warning) )
var ( // Any is used to RegisterResponse and RegisterError for any response/error object, // It is used as base type for all response/error objects // regardless if it's registered or not. Any = anyTarget(0) )
Functions ¶
func ContextErrorValue ¶
ContextErrorValue returns the error value stored in the context.
func ContextObjectValue ¶
ContextObjectValue returns the object value stored in the context.
func ReplaceGlobal ¶ added in v0.2.0
func ReplaceGlobal(j Jayson)
ReplaceGlobal replaces global instance of Jayson with given instance
Types ¶
type Extension ¶
type Extension interface {
// ExtendResponseWriter extends the response
ExtendResponseWriter(context.Context, http.ResponseWriter) bool
// ExtendResponseObject extends the object
ExtendResponseObject(context.Context, map[string]any) bool
}
Extension is the interface for Jayson ext.
It is used to alter the response or the object before it is sent to the client.
func ExtConditional ¶
ExtConditional calls first extFunc, and if it returns true, it calls all the ext. This is useful for conditional ext based on context (such as debug mode or any context values).
func ExtFirst ¶
ExtFirst returns an extFunc that returns the first extFunc that extends the response.
func ExtFunc ¶
func ExtFunc( extResponseWriter ExtensionApplyResponseWriterFunc, extResponseObject ExtensionApplyResponseObjectFunc, ) Extension
ExtFunc is an extFunc that calls the given function to extend the response or the response object.
func ExtHeaderValue ¶
ExtHeaderValue is an extFunc that adds a single HTTP header to the response.
func ExtNoop ¶
func ExtNoop() Extension
ExtNoop does not extend the response or the response object.
func ExtObjectKeyValue ¶
ExtObjectKeyValue is an extFunc that adds a single key-value pair to the response object.
func ExtObjectKeyValuef ¶
ExtObjectKeyValuef is an extFunc that adds a single key-value pair to the response object based on a format string.
func ExtObjectUnwrap ¶ added in v0.2.0
ExtObjectUnwrap is an Extension that converts the given object to the response object. It is useful if you want to add key/values to the response object (by altering it via Extensions). It uses json package to marshal/unmarshal given object. Warning if passed object does not marshal to json object, it will not be touched.
func ExtOmitObjectKey ¶
ExtOmitObjectKey is an extFunc that removes the given keys from the response object. This extFunc needs to be called at the end, so it removes the keys after all other ext have added their keys.
func ExtOmitSettingsKey ¶ added in v0.2.0
ExtOmitSettingsKey is an extFunc that removes the given keys from the response object based on the settings.
type ExtensionApplyResponseObjectFunc ¶
ExtensionApplyResponseObjectFunc is a function that extends the response object.
type ExtensionApplyResponseWriterFunc ¶
type ExtensionApplyResponseWriterFunc func(context.Context, http.ResponseWriter) bool
ExtensionApplyResponseWriterFunc is a function that extends the response writer.
type Jayson ¶
type Jayson interface {
// Debug enables debug mode via zap logger.
Debug(*zap.Logger)
// Error writes error to the client.
Error(context.Context, http.ResponseWriter, error, ...Extension)
// RegisterError registers extFunc for given error.
RegisterError(error, ...Extension) error
// RegisterResponse registers extFunc for given response object.
RegisterResponse(any, ...Extension) error
// Response writes given object/error to the client.
Response(context.Context, http.ResponseWriter, any, ...Extension)
}
Jayson is the interface that provides methods for writing responses to the client.
By default, there is a global instance to be used, but for some special purposes you can create your own (multiple servers, different settings, etc.)
type Settings ¶
type Settings struct {
DefaultErrorStatus int
DefaultErrorMessageKey string
DefaultErrorStatusCodeKey string
DefaultErrorStatusTextKey string
DefaultResponseStatus int
DefaultUnwrapObjectKey string // if unwrap fails, object will be placed under this key
}
Settings for jayson instance
func ContextSettingsValue ¶
ContextSettingsValue returns the settings value stored in the context.
func DefaultSettings ¶
func DefaultSettings() Settings
DefaultSettings returns default settings for jayson instance