querystring

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

QueryString

QueryString is a Go library for converting a struct or a map into URL values, with support for custom encoders, time formatting, and handling of different data types.

Installation

Use the package manager go get to install QueryString.

go get github.com/FPbear/querystring

Usage

Example
import "github.com/FPbear/querystring"

type Input struct {
    Hello string     `url:"hello"`
    Foo   string     `url:"foo,omitempty"`
    Empty string     `url:"empty"`
}

func main() {
    input := Input{
        Hello: "world",
        Foo:   "",
        Empty: "",
    }

    values, err := querystring.Values(input)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(values.Encode())
    // Output: hello=world&empty=
}

You can also customize the encoder, for example:

import "github.com/FPbear/querystring"

type subEncode struct {
	Hello string `form:"hello"`
	World string `form:"world"`
}

func (s *subEncode) Encode() ([]string, error) {
	return []string{s.Hello + "-" + s.World}, nil
}

type Input struct {
    Hello string     `form:"hello"`
    Foo   string     `form:"foo,omitempty"`
    Empty string     `form:"empty"`
    Sub   *subEncode `form:"sub,omitempty"`
}

func main() {
    input := Input{
        Hello: "world",
        Foo:   "",
        Empty: "",
        Sub: &subEncode{
            Hello: "hello",
            World: "world",
        },
    }

    con := NewConverter(NewTag(WithTag("form")))
    values, err := con.Values(input)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(values.Encode())
    // Output: hello=world&empty=&sub=hello-world
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToCamelCase

func ToCamelCase(s string) string

ToCamelCase : Converts the string s to camel case. The string s must start with a letter and contain only letters, numbers, and the characters '_', '-', and '.'. The first letter of the string s is converted to lowercase. If the string s is empty or does not start with a letter, the function returns an empty string. If the string s contains characters other than letters, numbers, '_', '-', and '.', the function returns an empty string. The function returns the converted string. For example: ToCamelCase("foo-bar") returns "fooBar". ToCamelCase("foo_bar") returns "fooBar". ToCamelCase("foo.bar") returns "fooBar". ToCamelCase("foo-bar-") returns "". ToCamelCase("foo-bar-1") returns "". ToCamelCase("1foo-bar") returns "". ToCamelCase("foobar") returns "foobar". ToCamelCase("fooBar") returns "fooBar".

func ToPascalCase

func ToPascalCase(s string) string

ToPascalCase : Converts the string s to pascal case. The string s must start with a letter and contain only letters, numbers, and the characters '_', '-', and '.'. The first letter of the string s is converted to uppercase. If the string s is empty or does not start with a letter, the function returns an empty string. If the string s contains characters other than letters, numbers, '_', '-', and '.', the function returns an empty string. The function returns the converted string. For example: ToPascalCase("foo-bar") returns "FooBar". ToPascalCase("foo_bar") returns "FooBar". ToPascalCase("foo.bar") returns "FooBar". ToPascalCase("foo-bar-") returns "". ToPascalCase("foo-bar-1") returns "". ToPascalCase("1foo-bar") returns "". ToPascalCase("foobar") returns "Foobar". ToPascalCase("fooBar") returns "Foobar". ToPascalCase("fooBar1") returns "Foobar1". ToPascalCase("FooBar") returns "FooBar". ToPascalCase("foo_2-bar") returns "Foo2Bar".

func ToSnakeCase

func ToSnakeCase(input string) string

ToSnakeCase : Converts the string s to snake case. The string s must start with a letter and contain only letters, numbers, and the characters '_', '-', and '.'. The first letter of the string s is converted to lowercase. If the string s is empty or does not start with a letter, the function returns an empty string. If the string s contains characters other than letters, numbers, '_', '-', and '.', the function returns an empty string. The function returns the converted string. For example: ToSnakeCase("foo-bar") returns "foo_bar". ToSnakeCase("foo_bar") returns "foo_bar". ToSnakeCase("foo.bar") returns "foo_bar". ToSnakeCase("foo-bar-") returns "". ToSnakeCase("foo-bar-1") returns "foo_bar_1". ToSnakeCase("1foo-bar") returns "".

func Values

func Values(v interface{}) (url.Values, error)

Values converts a value to url.Values. The value can be a map, a struct, a pointer to a struct, or a pointer to a map. The value can also implement the Encoder interface. If the value is a map, the key must be a string and the value must be a string. If the value is a struct, the field must have a tag with the key "url".

Types

type Converter

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

func NewConverter

func NewConverter(tag Tag) *Converter

func (*Converter) Values

func (c *Converter) Values(v interface{}) (url.Values, error)

Values converts a value to url.Values. The value can be a map, a struct, a pointer to a struct, or a pointer to a map. The value can also implement the Encoder interface. If the value is a map, the key must be a string and the value must be a string. If the value is a struct, the field must have a tag with the key "url" or a custom tag type. The tag value is the name of the field in the url.Values.

type Encoder

type Encoder interface {
	Encode() ([]string, error)
}

Encoder is an interface implemented by any type that wishes to encode itself into URL values in a non-standard way. The Encode method returns the encoded values.

type Name

type Name string
const (
	CamelCase  Name = "camel"
	PascalCase Name = "pascal"
	SnakeCase  Name = "snake"
)

func (Name) Convert

func (n Name) Convert(name string) string

Convert : Converts the string name to the specified case. The function returns the converted string. For example: Name("camel").Convert("hello_world") returns "helloWorld". Name("pascal").Convert("hello_world") returns "HelloWorld". Name("snake").Convert("HelloWorld") returns "hello_world". Name("").Convert("hello_world") returns "hello_world".

func (Name) IsEmpty

func (n Name) IsEmpty() bool

type Option

type Option func(*option)

func WithSkipField

func WithSkipField(skipField string) Option

func WithTag

func WithTag(tag string) Option

func WithUseName

func WithUseName(useName Name) Option

type Tag

type Tag interface {
	// Get returns the tag value of the struct field.
	Get(field reflect.StructField) (string, bool)
	// ParseTag parses the tag value and returns the tag name and tag options.
	ParseTag(tag string) (string, TagOptions)
	// Skip returns the value of the tag to skip the field.
	Skip() string
}

Tag represents the tag interface. The tag interface provides the methods to get the tag value, parse the tag, and skip the field. The tag value is the value of the tag in the struct field. The parse tag method parses the tag value and returns the tag name and tag options. The skip method returns the value of the tag to skip the field.

func NewTag

func NewTag(opts ...Option) Tag

type TagOptions

type TagOptions []string

TagOptions represents the options of a tag. The options are separated by commas.

func (TagOptions) Contains

func (o TagOptions) Contains(option string) bool

Contains checks whether the tagOptions contains the specified option.

Jump to

Keyboard shortcuts

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