urlquery

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2020 License: MIT Imports: 5 Imported by: 38

README

Introduce

A URL Query string Encoder and Parser by go.

  • Parse from URL Query string to go structure
  • Encode from go structure to URL Query string

Keywords

x-www-form-urlencoded HTTP-Query URLEncode URL-Query go

Feature

  • Support full go structure Translation
    • Basic Structure: Int[8,16,32,64] Uint[8,16,32,64] String Bool Float[32,64] Byte Rune
    • Complex Structure: Array Slice Map Struct
    • Nested Struct with above Basic or Complex Structure
  • Support top-level structure: Map, Slice or Array, not only Struct
  • Support self-defined URL-Encode rule
  • Support self-defined key name relation rule
  • Support to control whether ignoring Zero-value of struct member. It will cut down the length of encoded URL-Query string

Quick Start

More to see example

package main

import (
	"github.com/hetiansu5/urlquery"
	"fmt"
)

type SimpleData struct {
	Id     int
	Name   string          `query:"name"`
}

func main() {
	data := SimpleData{
		Id:   2,
		Name: "test",
	}

	//Marshal: from go structure to http-query string
	bytes, err := urlquery.Marshal(data)
	fmt.Println(string(bytes), err)

	//Unmarshal: from http-query  string to go structure
	v := &SimpleData{}
	err = urlquery.Unmarshal(bytes, v)
	fmt.Println(*v, err)
}

Attention

  • For Map structure, Marshal supports map[Basic]Basic|Complex, but Unmarshal just supports map[Basic]Basic
  • Default: ignoring Zero-value of struct member. You can enable it with Option
  • Remember that: Byte is actually uint8, Rune is actually int32

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(data interface{}) ([]byte, error)

encode go structure to string

func NewEncoder

func NewEncoder(opts ...Option) *encoder

func NewParser

func NewParser(opts ...Option) *parser

func SetGlobalUrlEncoder

func SetGlobalUrlEncoder(u UrlEncoder)

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

decode string to go structure

Types

type ErrInvalidMapKeyType

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

func (ErrInvalidMapKeyType) Error

func (e ErrInvalidMapKeyType) Error() string

type ErrInvalidMapValueType

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

func (ErrInvalidMapValueType) Error

func (e ErrInvalidMapValueType) Error() string

type ErrInvalidUnmarshalError

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

func (ErrInvalidUnmarshalError) Error

func (e ErrInvalidUnmarshalError) Error() string

type ErrTranslated

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

func (ErrTranslated) Error

func (e ErrTranslated) Error() string

type ErrUnhandledType

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

func (ErrUnhandledType) Error

func (e ErrUnhandledType) Error() string

type ErrUnsupportedBitSize

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

func (ErrUnsupportedBitSize) Error

func (e ErrUnsupportedBitSize) Error() string

type NeedEmptyValueOption

type NeedEmptyValueOption bool

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithNeedEmptyValue

func WithNeedEmptyValue(c bool) Option

support to control whether to ignore empty value. It just happen to the element directly in strcut, not include map slice array

func WithUrlEncoder

func WithUrlEncoder(u UrlEncoder) Option

support customized urlEncoder option

type UrlEncoder

type UrlEncoder interface {
	Escape(s string) string
	UnEscape(s string) (string, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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