helper

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2023 License: MIT Imports: 28 Imported by: 0

README

helper

gin

  1. 提供了更多的 handler 选择。

    • func(*gin.Context)
    • func(*gin.Context) error
    • func(*gin.Context, *reqType) error
    • func(*gin.Context) (*respType, error)
    • func(*gin.Context, *reqType) (*respType, error)
  2. 自动参数绑定。如何添加更多支持的 tag ?

    • header
    • uri
    • form
    • json
  3. 默认使用中文的 validator。

  4. 通过 tag defaultreqType 提供默认值,默认支持:

    • string: default:"foo"
    • []byte: default:"bar"
    • int: default:"10"
    • float64: default:"10.0"
    • bool: default:"true"
    • time.Duration: default:"10s", 使用 time.ParseDuration 进行解析。
    • time.Time: 使用 time.RFC3339 或者 time.RFC3339Nano,支持 now+{time.Duration}, now-{time.Duration}
    • 使用 mapstructure 支持自定义类型。
  5. 支持使用 zerolog 覆盖以下 gin 的配置。

    • gin.DefaultWriter
    • gin.DefaultErrorWriter
    • gin.DebugPrintRouteFunc
  6. reqType 支持下列钩子。

    • BeforeBind(*gin.Context)
    • AfterBind(*gin.Context)
    • BeforeValidate(*gin.Context)
    • AfterValidate(*gin.Context)
Usage
package main

import (
	"github.com/fioepq9/helper"
	"github.com/gin-gonic/gin"
)

type EchoRequest struct {
	Message string `form:"message"`
}

type EchoResponse struct {
	Message string `json:"message"`
}

// curl -X GET "http://localhost:8080/echo?message=hello"
// {"message": "hello"}
func main() {
	e := gin.New()
	
	r := helper.Gin().Router(e)
	
	r.GET("/echo", func(c *gin.Context, req *EchoRequest) (resp *EchoResponse, err error) {
		return &EchoResponse{Message: req.Message}, nil
	})
	
	if err := e.Run(":8080"); err != nil {
		panic(err)
	}
}
Examples
  1. 默认值的使用

Contributing

Alt

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StringToBoolHookFunc

func StringToBoolHookFunc() mapstructure.DecodeHookFuncType

func StringToBytesHookFunc

func StringToBytesHookFunc() mapstructure.DecodeHookFuncType

func StringToFloat64HookFunc

func StringToFloat64HookFunc() mapstructure.DecodeHookFuncType

func StringToIntHookFunc

func StringToIntHookFunc() mapstructure.DecodeHookFuncType

func StringToSliceHookFunc

func StringToSliceHookFunc(sep string) mapstructure.DecodeHookFuncType

func StringToTimeHookFunc

func StringToTimeHookFunc() mapstructure.DecodeHookFuncType

func UnmarshalToMapHookFunc

func UnmarshalToMapHookFunc(unmarshal func(in []byte, out any) error) mapstructure.DecodeHookFuncType

func UnmarshalToSliceHookFunc

func UnmarshalToSliceHookFunc(unmarshal func(in []byte, out any) error) mapstructure.DecodeHookFuncType

func UnmarshalToStructHookFunc

func UnmarshalToStructHookFunc(unmarshal func(in []byte, out any) error) mapstructure.DecodeHookFuncType

Types

type AfterBinding

type AfterBinding interface {
	AfterBind(c *gin.Context) error
}

type AfterValidation

type AfterValidation interface {
	AfterValidate(c *gin.Context) error
}

type BeforeBinding

type BeforeBinding interface {
	BeforeBind(c *gin.Context) error
}

type BeforeValidation

type BeforeValidation interface {
	BeforeValidate(c *gin.Context) error
}

type GinBinding

type GinBinding interface {
	Name() string
	Bind(c *gin.Context, obj any) error
}

type GinBindingWrapper

type GinBindingWrapper struct {
	Binding binding.Binding
}

func NewGinBinding

func NewGinBinding(b binding.Binding) *GinBindingWrapper

func (*GinBindingWrapper) Bind

func (b *GinBindingWrapper) Bind(c *gin.Context, obj any) error

func (*GinBindingWrapper) Name

func (b *GinBindingWrapper) Name() string

type GinDefaultBinding

type GinDefaultBinding struct {
	TagName     string
	DecodeHooks []mapstructure.DecodeHookFunc
}

func NewGinDefaultBinding

func NewGinDefaultBinding(options ...func(*GinDefaultBinding)) *GinDefaultBinding

func (*GinDefaultBinding) Bind

func (b *GinDefaultBinding) Bind(c *gin.Context, obj any) error

func (*GinDefaultBinding) Name

func (b *GinDefaultBinding) Name() string

type GinHelper

type GinHelper struct {
	Bindings            []GinBinding
	BindingValidator    binding.StructValidator
	BindingErrorHandler func(*gin.Context, error)
	SuccessHandler      func(*gin.Context, any)
	ErrorHandler        func(*gin.Context, error)
}

func Gin

func Gin(options ...func(*GinHelper)) *GinHelper

Gin

  • Notes: This function first call will disable the gin default validator

func (*GinHelper) Router

func (h *GinHelper) Router(routes gin.IRoutes) *GinRouter

func (*GinHelper) SetZerologWriter

func (h *GinHelper) SetZerologWriter(log zerolog.Logger, lvl zerolog.Level) *GinHelper

SetZerologWriter set zerolog writer

  • gin.DefaultWriter
  • gin.DefaultErrorWriter
  • gin.DebugPrintRouteFunc

type GinRouter

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

func (*GinRouter) GET

func (r *GinRouter) GET(path string, handler any) *GinRouter

func (*GinRouter) Handle

func (r *GinRouter) Handle(method string, path string, handler any) *GinRouter

func (*GinRouter) POST

func (r *GinRouter) POST(path string, handler any) *GinRouter

type GinURIBinding

type GinURIBinding struct {
	Params     func(*gin.Context) map[string][]string
	BindingURI binding.BindingUri
}

func NewGinURIBinding

func NewGinURIBinding() *GinURIBinding

func (*GinURIBinding) Bind

func (b *GinURIBinding) Bind(c *gin.Context, obj any) error

func (*GinURIBinding) Name

func (b *GinURIBinding) Name() string

type GinValidator

type GinValidator struct {
	Validate           *validator.Validate
	Translator         locales.Translator
	TranslatorRegister func(v *validator.Validate, trans ut.Translator) error
	Verbose            bool
	// contains filtered or unexported fields
}

func NewGinValidator

func NewGinValidator(options ...func(*GinValidator)) *GinValidator

func (*GinValidator) Engine

func (v *GinValidator) Engine() any

func (*GinValidator) ValidateStruct

func (v *GinValidator) ValidateStruct(obj any) error

type GormHelper

type GormHelper struct {
	Config *gorm.Config
}

func Gorm

func Gorm(options ...func(*GormHelper)) *GormHelper

func (*GormHelper) MySQL

func (h *GormHelper) MySQL(
	host string,
	port int,
	user string,
	passwd string,
	dbname string,
) (*gorm.DB, error)

type GormZerologLogger

type GormZerologLogger struct {
	BackupWriter              io.Writer
	Level                     zerolog.Level
	SlowThreshold             time.Duration
	IgnoreRecordNotFoundError bool
}

func NewGormZerologLogger

func NewGormZerologLogger(options ...func(logger *GormZerologLogger)) *GormZerologLogger

func (*GormZerologLogger) Error

func (l *GormZerologLogger) Error(ctx context.Context, msg string, data ...any)

func (*GormZerologLogger) Info

func (l *GormZerologLogger) Info(ctx context.Context, msg string, data ...any)

func (*GormZerologLogger) LogMode

func (l *GormZerologLogger) LogMode(level logger.LogLevel) logger.Interface

func (*GormZerologLogger) Trace

func (l *GormZerologLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

func (*GormZerologLogger) Warn

func (l *GormZerologLogger) Warn(ctx context.Context, msg string, data ...any)

type StackInfo

type StackInfo struct {
	Func string `json:"func"`
	File string `json:"file"`
}

type ViperHelper

type ViperHelper struct {
	V             *viper.Viper
	ConfigFile    string
	TagName       string
	EnableEnv     bool
	IgnoreEnvFunc func(key string, val string) bool
	DecodeHooks   []mapstructure.DecodeHookFunc
}

func Viper

func Viper(options ...func(*ViperHelper)) *ViperHelper

func (*ViperHelper) Unmarshal

func (h *ViperHelper) Unmarshal(conf any) error

type ZerologHelper

type ZerologHelper struct{}

func Zerolog

func Zerolog() *ZerologHelper

func (*ZerologHelper) ConsoleWriter

func (h *ZerologHelper) ConsoleWriter() zerolog.ConsoleWriter

func (*ZerologHelper) NewConsoleLogger

func (h *ZerologHelper) NewConsoleLogger() *zerolog.Logger

func (*ZerologHelper) NewLogger

func (h *ZerologHelper) NewLogger(out io.Writer) *zerolog.Logger

func (*ZerologHelper) SetDefaultGlobalErrorStackMarshaller

func (h *ZerologHelper) SetDefaultGlobalErrorStackMarshaller() *ZerologHelper

func (*ZerologHelper) SetDefaultGlobalInterfaceMarshalFunc

func (h *ZerologHelper) SetDefaultGlobalInterfaceMarshalFunc() *ZerologHelper

func (*ZerologHelper) SetDefaultGlobalLevel

func (h *ZerologHelper) SetDefaultGlobalLevel() *ZerologHelper

func (*ZerologHelper) SetErrorStackMarshaller

func (h *ZerologHelper) SetErrorStackMarshaller(fn func(error) any) *ZerologHelper

func (*ZerologHelper) SetGlobalLevel

func (h *ZerologHelper) SetGlobalLevel(level zerolog.Level) *ZerologHelper

func (*ZerologHelper) SetInterfaceMarshalFunc

func (h *ZerologHelper) SetInterfaceMarshalFunc(fn func(any) ([]byte, error)) *ZerologHelper

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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