ginutil

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2025 License: MIT Imports: 5 Imported by: 0

README

ginutil

Standardized Gin parameter binding.

package main

import (
  "context"
  "fmt"

  "github.com/gin-gonic/gin"
  "github.com/mengdu/ginutil"
)

type Foo struct{}

type HelloReq struct {
  Name string `query:"name" binding:"required,min=1,max=10"` // e.g: ?name=Tom
}

func (f Foo) Hello(ctx context.Context, req *HelloReq) (string, error) {
  return fmt.Sprintf("Hello, %s!", req.Name), nil
}

func apiReply(c *gin.Context, err error, v any) {
  if err != nil {
    c.JSON(500, gin.H{"ret": -1, "msg": err.Error()})
    return
  }
  c.JSON(200, gin.H{"data": v})
}

func main() {
  srv := gin.New()
  foo := Foo{}

  srv.GET("/hello", ginutil.Handle(apiReply, foo.Hello))

  if err := srv.Run("localhost:8080"); err != nil {
    panic(err)
  }
}

Binding request parameters

// binding from uri string
type UriReq = ginutil.Uri[struct {
  A string `uri:"a" binding:"required"` // use `uri` tag for field binding
  B int    `uri:"b" binding:"min=2,max=10"`
}]

// binding from query string
type QueryReq = ginutil.Query[struct {
  A string `query:"a" binding:"required"` // use `query` tag for field binding
  B int    `query:"b" binding:"min=2,max=10"`
}]

// binding from header
type HeaderReq = ginutil.Header[struct {
  A string `header:"a" binding:"required"` // use `header` tag for field binding
  B int    `header:"b" binding:"min=2,max=10"`
}]

// binding from `application/x-www-form-urlencoded` body
type FormReq = ginutil.Form[struct {
  A string `form:"a" binding:"required"` // use `form` tag for field binding
  B int    `form:"b" binding:"min=2,max=10"`
}]

// binding from `application/form-data` body
type UploadFormReq = ginutil.FormData[struct {
  Key  string                `form:"key" binding:"required"` // use `form` tag for field binding
  File *multipart.FileHeader `form:"file" binding:"required"` // file
}]

// binding from `application/json` body
type JsonReq = ginutil.JSON[struct {
  A string `json:"a" binding:"required"` // use `json` tag for field binding
  B int    `json:"b" binding:"min=2,max=10"`
}]

// binding from `application/xml` body
type XmlReq = ginutil.XML[struct {
  A string `xml:"a" binding:"required"` // use `xml` tag for field binding
  B int    `xml:"b" binding:"min=2,max=10"`
}]

Documentation

Index

Constants

View Source
const (
	ContextKey = key("context")
)

Variables

This section is empty.

Functions

func Context

func Context(ctx context.Context) *gin.Context

func Handle

func Handle[A, B any](reply Reply, handler Handler[*A, B]) gin.HandlerFunc

func ShouldBind

func ShouldBind(c *gin.Context, v any) error

Types

type CustomReplyer

type CustomReplyer interface {
	Handle(c *gin.Context)
}

type Form

type Form[T any] struct {
	Value T `from:"form-urlencoded"`
}

func (*Form[T]) Bind

func (f *Form[T]) Bind(c *gin.Context) error

type FormData

type FormData[T any] struct {
	Value T `from:"form-data"`
}

func (*FormData[T]) Bind

func (f *FormData[T]) Bind(c *gin.Context) error

type GinBinder

type GinBinder interface {
	Bind(c *gin.Context) error
}

type Handler

type Handler[Req, Reply any] func(ctx context.Context, req Req) (Reply, error)
type Header[T any] struct {
	Value T `from:"header"`
}

func (*Header[T]) Bind

func (h *Header[T]) Bind(c *gin.Context) error

type JSON

type JSON[T any] struct {
	Value T `from:"json"`
}

func (*JSON[T]) Bind

func (j *JSON[T]) Bind(c *gin.Context) error

type MsgPack

type MsgPack[T any] struct {
	Value T `from:"msgpack"`
}

func (*MsgPack[T]) Bind

func (j *MsgPack[T]) Bind(c *gin.Context) error

type ProtoBuf

type ProtoBuf[T any] struct {
	Value T `from:"protobuf"`
}

func (*ProtoBuf[T]) Bind

func (j *ProtoBuf[T]) Bind(c *gin.Context) error

type Query

type Query[T any] struct {
	Value T `from:"query"`
}

func (*Query[T]) Bind

func (q *Query[T]) Bind(c *gin.Context) error

type QueryBinding

type QueryBinding struct{}

QueryBinding implements `binding.Binding` is used to bind query strings and define fields using the tag 'query'

func (QueryBinding) Bind

func (q QueryBinding) Bind(req *http.Request, v any) error

func (QueryBinding) Name

func (QueryBinding) Name() string

type Reply

type Reply func(c *gin.Context, err error, v any)

type Uri

type Uri[T any] struct {
	Value T `from:"uri"`
}

func (*Uri[T]) Bind

func (u *Uri[T]) Bind(c *gin.Context) error

type Void

type Void struct{}

Ignore the type of binding.

func (Void) Bind

func (Void) Bind(c *gin.Context, v any) error

type XML

type XML[T any] struct {
	Value T `from:"xml"`
}

func (*XML[T]) Bind

func (j *XML[T]) Bind(c *gin.Context) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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