Skip to content

Add support for struct field name casing options #86

@vasudevm

Description

@vasudevm

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})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions