greb

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2021 License: MIT Imports: 10 Imported by: 0

README

Golang REquest Binder

An automated golang HTTP Request to struct binding. Why it exist? I don't know, i just think it will be better if we automate request binding and validation process doesn't it?

How to use

You need to create a new file with .greb extension. The greb extension is optional, but maybe we should stick with it shall we? To install the package please use go get github.com/firmanmm/greb/cmd/greb. Use greb -h to show the help. To generate the golang file use greb --in=example/simple/simple.greb --out=example/simple/simple.greb.go. Here is a simple greb file definition. You should not mix form and json since it is all stored in the body.

package simple

request Simple {
    ID              query:int           validate:"required"
    GroupID         param:int           alias:"group_id"
    Name            form:string         validate:"required"
    Weight          json:float          alias:"weight"
    IsAlive         form:bool
    Authorization   header:string       validate:"required" alias:"x-authorization"
    SessionID       cookie:string
}

Supported Binding

Binding Type

Binding type is the source data to get the value.

Type Description
query Taken from query param
form Taken from body
json Taken from body
header Taken from header
cookie Taken from cookie
Binding Data Type

Binding data type is the data type to bind to.

Type Description
int Integer value
float Decimal value
string Literally string
bool true or false
bytes Uses multipart when in form, and base64 in other scenario
Binding Tag

Binding Tag is to extend greb's functionality. Tags can be combined for fun.

Tag Description
validate Perform data validation based on golang validator library
alias Override key used to get data

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindBool

func BindBool(req *http.Request, key string, bindType BindType) (bool, error)

func BindBytes added in v1.0.1

func BindBytes(req *http.Request, key string, bindType BindType) ([]byte, error)

func BindFloat

func BindFloat(req *http.Request, key string, bindType BindType) (float64, error)

func BindInt

func BindInt(req *http.Request, key string, bindType BindType) (int, error)

func BindString

func BindString(req *http.Request, key string, bindType BindType) (string, error)

func Generate

func Generate(fileName string) (string, error)

func Validate

func Validate(data IBindable) error

Types

type BindDataType

type BindDataType uint
const (
	DATA_BIND_TYPE_INT    BindDataType = 1
	DATA_BIND_TYPE_FLOAT  BindDataType = 2
	DATA_BIND_TYPE_STRING BindDataType = 3
	DATA_BIND_TYPE_BOOL   BindDataType = 4
)

type BindType

type BindType uint
const (
	BIND_TYPE_FORM   BindType = 1
	BIND_TYPE_QUERY  BindType = 2
	BIND_TYPE_JSON   BindType = 3
	BIND_TYPE_HEADER BindType = 4
	BIND_TYPE_COOKIE BindType = 5
	BIND_TYPE_PARAM  BindType = 6
)

type Field

type Field struct {
	Identifier string  `@Ident`
	Type       string  `@Ident`
	DataType   string  `":"@Ident`
	Validation *string `("validate"":"@String)?`
	Alias      *string `("alias"":"@String)?`
}

type Generator

type Generator struct{}

func (*Generator) Generate

func (g *Generator) Generate(ast *Greb) (string, error)

type Greb

type Greb struct {
	Package  string     `"package" @Ident`
	Requests []*Request `@@*`
}

type IBindable

type IBindable interface {
	BindRequest(req *http.Request) error
}

type Parser

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

func (*Parser) Parse

func (p *Parser) Parse(rawText string) (*Greb, error)

type Request

type Request struct {
	Name   string   `"request" @Ident`
	Fields []*Field `"{" @@* "}"`
}

type URLParamBind

type URLParamBind func(req *http.Request, key string) string
var URLParamBindFunc URLParamBind

Directories

Path Synopsis
cmd
example

Jump to

Keyboard shortcuts

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