Skip to content

Simplify converting from any type to any of the common built-in types in Go (e.g. convert from a string to another type in one command and just panic if there's an error).

License

Notifications You must be signed in to change notification settings

dsoprea/go-multiparse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status

Introduction

go-multiparse allows you to easily convert one type of value into a built-in type (e.g. strings from query values in web requests to integers or floats). The original purpose of this project is to reduce the repetition in parsing from string values in web-requests though it is not limited to just parsing strings. Parse errors will simply panic.

Example

// import "github.com/dsoprea/go-multiparse"

value := parse.Parse("123.456", "float64").(float64)

Implementation

This is a general parsing framework. Parser implementations must satisfy the Parser interface and be registered for specific input types. You will have to register multiple times if you are trying to parse both a base type and one or more aliases of it. The StringParser is included and parses over string values. It is automatically registered.

Parser Interface

This is the interface:

type Parser interface {
    String(value interface{}) string

    Int8(value interface{}) int8
    Int16(value interface{}) int16
    Int32(value interface{}) int32
    Int64(value interface{}) int64

    Uint8(value interface{}) uint8
    Uint16(value interface{}) uint16
    Uint32(value interface{}) uint32
    Uint64(value interface{}) uint64

    Hex8(value interface{}) uint8
    Hex16(value interface{}) uint16
    Hex32(value interface{}) uint32
    Hex64(value interface{}) uint64

    Float32(value interface{}) float32
    Float64(value interface{}) float64

    Bool(value interface{}) bool

    Rfc3339(value interface{}) time.Time
}

To register a new implementation, use AddParser(). An example based on how we automatically register StringParser:

type := reflect.TypeOf("")
parse.AddParser(type, parserInstance)

Of course, it would not make any sense to register another string parser. However, if you have a more exotic type that you often need to convert, implement the interface methods that you require, panic on the others, and use AddParser() to register it.

Alternate Usage

The Parse() function is a convenience wrapper (which uses a dictionary to find the method name). You may acquire and use the Parser instance directly:

// import "reflect"

vRaw := "123.456"
t := reflect.TypeOf(vRaw)
p := parse.GetParser(t)
v := p.Float64(vRaw)

Note that using a parser this way returns the desired type directly (not as a interface{}).

Utilities

We also include a convenience function to parse values from an HTTP request (query arguments or body data):

// func FromRequestBody(r *http.Request, name string, kindName string, required bool) (value interface{})
v = parse.FromRequestBody(r, "varname", "float64", true).(float64)

// func FromRequestQuery(r *http.Request, name string, kindName string, required bool) (value interface{})
v = parse.FromRequestQuery(r, "argname", "float64", true).(float64)

// func FromRequestHeader(r *http.Request, name string, kindName string, required bool) (value interface{})
v = parse.FromRequestHeader(r, "X-HEADER-NAME", "float64", true).(float64)

// func (jrp *parse.JsonRequestParser) Get(name string, kindName string, required bool) (value interface{})
jrp := parse.NewJsonRequestParser(r)
v = jrp.Get("account_id", "uint64", true).(uint64)

// func FromMap(dict map[string]string, name string, kindName string, required bool) (value interface{}) {
v = parse.FromMap(d, "account_id", "uint64", true).(uint64)

// func FromInterfaceMap(dict map[string]interface{}, name string, kindName string, required bool) (value interface{}) {
v = parse.FromInterfaceMap(d, "account_id", "uint64", true).(uint64)

// func FromEnviron(name string, kindName string, required bool) (value interface{})
v = parse.FromEnviron("varname", "float64", true).(float64)

About

Simplify converting from any type to any of the common built-in types in Go (e.g. convert from a string to another type in one command and just panic if there's an error).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages