Documentation ¶
Overview ¶
Package common provides common functions and types for OpenTreeHole Go projects.
Index ¶
- Variables
- func ErrorHandler(ctx *fiber.Ctx, err error) error
- func GetJWTToken(c *fiber.Ctx) string
- func GetUserID(c *fiber.Ctx) (int, error)
- func Keys[T comparable, S any](m map[T]S) (s []T)
- func Max[T constraints.Ordered](a, b T) T
- func MiddlewareCustomLogger(c *fiber.Ctx) error
- func MiddlewareGetUserID(c *fiber.Ctx) error
- func Min[T constraints.Ordered](a, b T) T
- func ParseJWTToken(token string, user any) error
- func RegisterApp(app *fiber.App)
- func StackTraceHandler(_ *fiber.Ctx, e any)
- func StripContent(content string, contentMaxSize int) string
- func ValidateBody(c *fiber.Ctx, model any) error
- func ValidateQuery(c *fiber.Ctx, model any) error
- func ValidateStruct(model any) error
- func Values[T comparable, S any](m map[T]S) (s []S)
- type CustomTime
- type ErrorDetail
- type ErrorDetailElement
- type HttpError
- type Map
- type MessageResponse
- type RequestConfig
- type Tester
- func (tester *Tester) Delete(t assert.TestingT, config RequestConfig)
- func (tester *Tester) Get(t assert.TestingT, config RequestConfig)
- func (tester *Tester) Patch(t assert.TestingT, config RequestConfig)
- func (tester *Tester) Post(t assert.TestingT, config RequestConfig)
- func (tester *Tester) Put(t assert.TestingT, config RequestConfig)
- func (tester *Tester) Request(t assert.TestingT, config RequestConfig)
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrJWTTokenRequired = errors.New("jwt token required") ErrInvalidJWTToken = errors.New("invalid jwt token") )
View Source
var ( DefaultTester = &Tester{} UserTester = &Tester{ID: 1} AdminTester = &Tester{ID: 2} OtherTester = map[int]*Tester{ 0: DefaultTester, 1: UserTester, 2: AdminTester, } // map[userID]Tester )
View Source
var App *fiber.App
View Source
var Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
View Source
var Validate = validator.New()
Functions ¶
func ErrorHandler ¶ added in v0.0.2
func GetJWTToken ¶
func GetJWTToken(c *fiber.Ctx) string
GetJWTToken extracts token from header or cookie return empty string if not found
func Keys ¶
func Keys[T comparable, S any](m map[T]S) (s []T)
func Max ¶
func Max[T constraints.Ordered](a, b T) T
func MiddlewareCustomLogger ¶
func MiddlewareCustomLogger(c *fiber.Ctx) error
func MiddlewareGetUserID ¶
func MiddlewareGetUserID(c *fiber.Ctx) error
func Min ¶
func Min[T constraints.Ordered](a, b T) T
func ParseJWTToken ¶
ParseJWTToken extracts and parse token, whatever start with "Bearer " or not
func RegisterApp ¶
func RegisterApp(app *fiber.App)
RegisterApp registers the fiber app to the common package It should be called before any test
func StackTraceHandler ¶ added in v0.0.2
func StackTraceHandler(_ *fiber.Ctx, e any)
func StripContent ¶ added in v0.0.3
func ValidateBody ¶
ValidateBody parse, set default and validate body based on Content-Type. It supports json, xml and form only when struct tag exists; if empty, using defaults.
func ValidateQuery ¶
ValidateQuery parse, set default and validate query into model
func ValidateStruct ¶ added in v0.0.6
func Values ¶ added in v0.0.3
func Values[T comparable, S any](m map[T]S) (s []S)
Types ¶
type CustomTime ¶
func (*CustomTime) UnmarshalJSON ¶
func (ct *CustomTime) UnmarshalJSON(data []byte) error
func (*CustomTime) UnmarshalText ¶
func (ct *CustomTime) UnmarshalText(data []byte) error
type ErrorDetail ¶
type ErrorDetail []*ErrorDetailElement
func (ErrorDetail) Error ¶
func (e ErrorDetail) Error() string
type ErrorDetailElement ¶
type ErrorDetailElement struct { Tag string `json:"tag"` Field string `json:"field"` Kind reflect.Kind `json:"-"` Value any `json:"value"` Param string `json:"param"` StructField string `json:"struct_field"` Message string `json:"message"` }
func (*ErrorDetailElement) Error ¶ added in v0.0.9
func (e *ErrorDetailElement) Error() string
type HttpError ¶
type HttpError struct { Code int `json:"code,omitempty"` Message string `json:"message,omitempty"` Detail *ErrorDetail `json:"detail,omitempty"` }
func BadRequest ¶
func InternalServerError ¶ added in v0.0.5
func Unauthorized ¶
type MessageResponse ¶ added in v0.0.7
func Message ¶ added in v0.0.7
func Message(msg string) *MessageResponse
Message return MessageResponse
type RequestConfig ¶
type RequestConfig struct { Method string `default:"GET"` Route string `default:"/"` ExpectedStatus int `default:"200"` RequestHeaders map[string]string `default:"-"` RequestQuery any `default:"-"` RequestBody any `default:"-"` ResponseModel any `default:"-"` ExpectedBody string `default:"-"` ContentType string `default:"application/json"` }
RequestConfig is a struct that contains the config of a request
Click to show internal directories.
Click to hide internal directories.