go-querystring icon indicating copy to clipboard operation
go-querystring copied to clipboard

Add support for struct field name casing options

Open vasudevm opened this issue 3 years ago • 0 comments

TL;DR: Add support for camelCase and snake_case (commonly used formats for URL encoding) options in query.Values() function.

This package, by default, uses the struct's field names for URL query parameters. While the package supports defining custom field names using the url field tag, it is not possible add custom tags in certain scenarios, for example:

  1. When using external packages' struct definitions.
  2. When using structs that are tool-generated (protobuf, for example).

query/encode.go

package query
...

type Case int32
const (
    CASE_DEFAULT    Case = 0
    CASE_CAMEL      Case = 1
    CASE_SNAKE      Case = 2
    CASE_PASCAL     Case = 3
)

type Options struct {
    EncodingCase  Case
}

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

usage.go

import "github.com/google/go-querystring/query"
import "some/external/package"
...
v, _ := query.Values(package.SomeStruct{}, Options{EncodingCase: query.CASE_CAMEL})

vasudevm avatar Jan 12 '23 13:01 vasudevm