common

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: MIT Imports: 22 Imported by: 6

README

go-common

a common module for OpenTreeHole Go projects.

Documentation

Overview

Package common provides common functions and types for OpenTreeHole Go projects.

Index

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 ErrorHandler(ctx *fiber.Ctx, err error) error

func GetJWTToken

func GetJWTToken(c *fiber.Ctx) string

GetJWTToken extracts token from header or cookie return empty string if not found

func GetUserID

func GetUserID(c *fiber.Ctx) (int, error)

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

func ParseJWTToken(token string, user any) error

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 StripContent(content string, contentMaxSize int) string

func ValidateBody

func ValidateBody(c *fiber.Ctx, model any) error

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

func ValidateQuery(c *fiber.Ctx, model any) error

ValidateQuery parse, set default and validate query into model

func ValidateStruct added in v0.0.6

func ValidateStruct(model any) error

func Values added in v0.0.3

func Values[T comparable, S any](m map[T]S) (s []S)

Types

type CustomTime

type CustomTime struct {
	time.Time
}

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 BadRequest(messages ...string) *HttpError

func Forbidden

func Forbidden(messages ...string) *HttpError

func InternalServerError added in v0.0.5

func InternalServerError(messages ...string) *HttpError

func NotFound added in v0.0.5

func NotFound(messages ...string) *HttpError

func Unauthorized

func Unauthorized(messages ...string) *HttpError

func (*HttpError) Error

func (e *HttpError) Error() string

type Map

type Map = map[string]any

type MessageResponse added in v0.0.7

type MessageResponse struct {
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

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

type Tester

type Tester struct {
	ID    int    `json:"id"`
	Token string `json:"token"`
}

Tester is a struct that mocks a request user

func (*Tester) Delete

func (tester *Tester) Delete(t assert.TestingT, config RequestConfig)

func (*Tester) Get

func (tester *Tester) Get(t assert.TestingT, config RequestConfig)

func (*Tester) Patch

func (tester *Tester) Patch(t assert.TestingT, config RequestConfig)

func (*Tester) Post

func (tester *Tester) Post(t assert.TestingT, config RequestConfig)

func (*Tester) Put

func (tester *Tester) Put(t assert.TestingT, config RequestConfig)

func (*Tester) Request

func (tester *Tester) Request(t assert.TestingT, config RequestConfig)

Jump to

Keyboard shortcuts

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