urlquery

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2020 License: MIT Imports: 6 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

Quick Start

More to see example

package main

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

type SimpleChild struct {
	Status bool `query:"status"`
	Name   string
}

type SimpleData struct {
	Id         int
	Name       string          `query:"name"`
	Child      SimpleChild
	Params     map[string]int8 `query:"p"`
	Array      [3]uint16
}

func main() {
	data := SimpleData{
		Id:   2,
		Name: "http://localhost/test.php?id=2",
		Child: SimpleChild{
			Status: true,
		},
		Params: map[string]int8{
			"one": 1,
		},
		Array: [3]uint16{2, 3, 300},
	}

	//Marshal: from go structure to url query string
	bytes, _ := urlquery.Marshal(data)
	fmt.Println(string(bytes))

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

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

View Source
const (
	SymbolEqual = "="
	SymbolAnd   = "&"
)

Variables

This section is empty.

Functions

func Marshal

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

*

  • encode go structure to string
  • Thread safety

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
  • Thread safety

Types

type DefaultUrlEncoder added in v1.2.2

type DefaultUrlEncoder struct{}

default url encoder

func (DefaultUrlEncoder) Escape added in v1.2.2

func (u DefaultUrlEncoder) Escape(s string) string

func (DefaultUrlEncoder) UnEscape added in v1.2.2

func (u DefaultUrlEncoder) UnEscape(s string) (string, error)

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 zero-value. It just happen to the element directly in strcut, not including 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