ginparam

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2021 License: MIT Imports: 5 Imported by: 0

README

ginparam

Query parameter binder for gin-gonic/gin

Installation

$ go get github.com/usagiga/ginparam

Example

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/usagiga/ginparam"
	"log"
)

type Request struct {
	// These fields will be loaded
	BoolVal   bool   `query:"bool_val"`
	StringVal string `query:"string_val"`
	IntVal    int    `query:"bool_val"`

	// These fields will be ignored
	IgnoredVal1 string `query:"-"`
	IgnoredVal2 string
}

func main() {
	ctx := gin.New()

	ctx.GET("/foo", func(ctx *gin.Context) {
		req := &Request{}
		err := ginparam.Read(ctx, req)
		if err != nil {
			log.Fatalf("Can't read params: %+v", err)
		}

		log.Println("StringVal: ", req.StringVal)
	})

	log.Println("Server started on :8080")
	err := ctx.Run(":8080")
	if err != nil {
		log.Fatalf("Something wrong in server: %+v", err)
    }
}

If there's no query struct tag, no value in specified environment keys or field applied query:"-", ginparam will ignore it.

See also example .

Features

  • Compatible with xerrors
  • Auto type detection
Supported types
  • int
  • string
  • bool
  • slice of them
  • struct contained them

Dependencies

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Read

func Read(ctx *gin.Context, out interface{}) (err error)

Read `out` members from (*gin.Context).Params. `query` tagged members(without `query:"-"`) will be read, and others will be ignored.

## Compatible types int, string, bool, their slice and struct contained them.

Slice is separated its own value with ",". Unfortunately, it has no way to escape ",", pls take care!

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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