query

package module
Version: v0.0.0-...-249d266 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2018 License: MIT Imports: 7 Imported by: 1

README

Build Status

query

query is Go library for encoding structs into URL query string.

Installation

To install query, use go get

go get -u github.com/gotoxu/query

Example usages

Encoder
package main

import (
	"fmt"
	"net/url"

	"github.com/gotoxu/query"
)

type example struct {
	Name     string  `url:"name"`
	Password string  `url:"password"`
	Age      int     `url:"age"`
	Salary   float64 `url:"salary"`
	ID       int64   `url:"id"`
}

func main() {
	e := &example{
		Name:     "XuQiaolun",
		Password: "123456",
		Age:      33,
		Salary:   20000.00,
		ID:       1523798459,
	}

	var err error
	var m url.Values
	m, err = query.NewEncoder().Encode(e)
	if err != nil {
		panic(err)
	}

	fmt.Println(m.Encode())
}

默认的Struct tag name是url,Result:

age=33&id=1523798459&name=XuQiaolun&password=123456&salary=20000.000000

你也可以使用函数SetAliasTag来自定义tag,如:

encoder := query.NewEncoder().SetAliasTag("schema")
Decoder
package main

import (
	"net/url"

	"github.com/gotoxu/query"
)

type example struct {
	Name     string  `url:"name"`
	Password string  `url:"password"`
	Age      int     `url:"age"`
	Salary   float64 `url:"salary"`
	ID       int64   `url:"id"`
}

func main() {
	m, err := url.ParseQuery("age=33&id=1523798459&name=XuQiaolun&password=123456&salary=20000.000000")
	if err != nil {
		panic(err)
	}

	decoder := query.NewDecoder()
	var e example
	err = decoder.Decode(m, &e)
	if err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConversionError

type ConversionError struct {
	Key   string
	Type  reflect.Type
	Index int
	Err   error
}

ConversionError 存储了转换错误的详细信息

func (ConversionError) Error

func (e ConversionError) Error() string

type Converter

type Converter func(string) reflect.Value

Converter 转换函数

type Decoder

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

Decoder 将map[string][]string反编码为一个struct

func NewDecoder

func NewDecoder() *Decoder

NewDecoder 返回一个新的解码器

func (*Decoder) Decode

func (d *Decoder) Decode(src map[string][]string, dst interface{}) error

Decode 将一个map[string][]string解码为一个struct

func (*Decoder) IgnoreUnknownKeys

func (d *Decoder) IgnoreUnknownKeys(i bool)

IgnoreUnknownKeys 控制解码器遇到未知key时的行为

func (*Decoder) RegisterConverter

func (d *Decoder) RegisterConverter(value interface{}, converterFunc Converter)

RegisterConverter 为自定义类型注册一个转换函数

func (*Decoder) SetAliasTag

func (d *Decoder) SetAliasTag(tag string)

SetAliasTag 修改用来定位自定义字段别名的tag

func (*Decoder) ZeroEmpty

func (d *Decoder) ZeroEmpty(z bool)

ZeroEmpty 控制解码器遇到空值时的行为

type Encoder

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

Encoder 将一个struct编码为url.Values

func NewEncoder

func NewEncoder() *Encoder

NewEncoder 返回一个新的编码器

func (*Encoder) Encode

func (e *Encoder) Encode(src interface{}) (map[string][]string, error)

Encode 将一个struct编码为map[string][]string

func (*Encoder) RegisterEncoder

func (e *Encoder) RegisterEncoder(value interface{}, encoder func(reflect.Value) string)

RegisterEncoder 为自定义类型注册编码函数

func (*Encoder) SetAliasTag

func (e *Encoder) SetAliasTag(tag string)

SetAliasTag 用于修改用来定位自定义字段别名的tag

type MultiError

type MultiError map[string]error

MultiError 存储了多个编码或解码错误

func (MultiError) Error

func (e MultiError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL