google / google/go-querystring

Add support for struct field name casing options

Open
#86 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Go
Stars
2.1k
Forks
188
Avg merge
10h 23m
Merged PRs (30d)
1

Description

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.