-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6c19737
Showing
12 changed files
with
842 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: Go | ||
|
||
on: | ||
push: | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.21 | ||
|
||
- name: Test | ||
run: make test | ||
|
||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v3 | ||
env: | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||
with: | ||
files: ./coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# If you prefer the allow list template instead of the deny list, see community template: | ||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore | ||
# | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work | ||
coverage.coverprofile | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.PHONY: test | ||
|
||
test: | ||
@echo "##### Running tests" | ||
go test -race -cover -coverprofile=coverage -covermode=atomic -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# go-urlvalues # | ||
|
||
![Test Status](https://github.com/vcraescu/go-urlvalues/actions/workflows/go.yml/badge.svg) | ||
[![codecov](https://codecov.io/github/vcraescu/go-urlvalues/branch/master/graph/badge.svg)](https://codecov.io/github/vcraescu/go-urlvalues) | ||
|
||
Go library that provides a simple and flexible way to encode Go values into URL query parameters. It | ||
offers a customizable interface to marshal various data types, including maps, slices, structs, and more. | ||
|
||
## Usage | ||
|
||
```bash | ||
go get -u github.com/vcraescu/go-urlvalues | ||
``` | ||
|
||
## Marshaler | ||
|
||
### Features | ||
|
||
- Encode Go values (structs and maps) into URL query parameters. | ||
- Support for encoding maps, slices, structs, and other data types. | ||
- Configurable options for customizing the marshaling process. | ||
|
||
### Marshaler Interface | ||
|
||
``` | ||
type Marshaler interface { | ||
EncodeValues(key string, values *url.Values) error | ||
} | ||
``` | ||
|
||
Implement the `EncodeValues` method in your custom types to provide specific encoding logic. | ||
|
||
### Custom Options | ||
|
||
#### MarshalOptions | ||
|
||
* `ArrayBrackets`: Add brackets to array/slice keys in the URL parameters. | ||
* `Delimiter`: Specify a custom delimiter for joining slice values in the URL parameters. | ||
|
||
Example: | ||
|
||
``` | ||
values, err := urlvalues.Marshal(data, func(opts *urlvalues.MarshalOptions) { | ||
opts.ArrayBrackets = true | ||
opts.Delimiter = "|" | ||
}) | ||
``` | ||
|
||
### Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/vcraescu/go-urlvalues" | ||
) | ||
|
||
func main() { | ||
type Object struct { | ||
Slice []int `url:"slice"` | ||
String string `url:"string"` | ||
} | ||
|
||
m := map[string]any{ | ||
"slice": []string{"100", "200"}, | ||
"int": 1, | ||
"map": map[string]any{ | ||
"slice": []string{"100", "200"}, | ||
"int": 1, | ||
}, | ||
"object": Object{ | ||
Slice: []int{1, 2}, | ||
String: "example", | ||
}, | ||
} | ||
|
||
values, err := urlvalues.Marshal(m) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(values.Encode()) | ||
// Output: int=1&map%5Bint%5D=1&map%5Bslice%5D=100&map%5Bslice%5D=200&object%5Bslice%5D=1&object%5Bslice%5D=2&object%5Bstring%5D=example&slice=100&slice=200 | ||
} | ||
``` | ||
|
||
## Credits | ||
|
||
This project utilizes the [google/go-querystring](https://github.com/google/go-querystring) library for encoding structs | ||
to URL. | ||
|
||
## License | ||
|
||
This library is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package urlvalues_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/vcraescu/go-urlvalues" | ||
) | ||
|
||
func ExampleMarshal() { | ||
type Object struct { | ||
Slice []int `url:"slice"` | ||
String string `url:"string"` | ||
} | ||
|
||
m := map[string]any{ | ||
"slice": []string{"100", "200"}, | ||
"int": 1, | ||
"map": map[string]any{ | ||
"slice": []string{"100", "200"}, | ||
"int": 1, | ||
}, | ||
"object": Object{ | ||
Slice: []int{1, 2}, | ||
String: "example", | ||
}, | ||
} | ||
|
||
values, err := urlvalues.Marshal(m) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(values.Encode()) | ||
// Output: int=1&map%5Bint%5D=1&map%5Bslice%5D=100&map%5Bslice%5D=200&object%5Bslice%5D=1&object%5Bslice%5D=2&object%5Bstring%5D=example&slice=100&slice=200 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module github.com/vcraescu/go-urlvalues | ||
|
||
go 1.21 | ||
|
||
require ( | ||
github.com/google/go-querystring v1.1.0 | ||
github.com/stretchr/testify v1.8.4 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= | ||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= | ||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
type zeroable interface { | ||
IsZero() bool | ||
} | ||
|
||
func Indirect(v reflect.Value) reflect.Value { | ||
for v.Kind() == reflect.Ptr { | ||
if v.IsNil() { | ||
return v | ||
} | ||
|
||
v = v.Elem() | ||
} | ||
|
||
return v | ||
} | ||
|
||
func IsZero(v reflect.Value) bool { | ||
switch v.Kind() { | ||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String: | ||
return v.Len() == 0 | ||
case reflect.Bool: | ||
return !v.Bool() | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return v.Int() == 0 | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
return v.Uint() == 0 | ||
case reflect.Float32, reflect.Float64: | ||
return v.Float() == 0 | ||
case reflect.Interface, reflect.Ptr: | ||
return v.IsNil() | ||
default: | ||
if !v.IsValid() { | ||
return true | ||
} | ||
|
||
if a, ok := v.Interface().(zeroable); ok { | ||
return a.IsZero() | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func StringValueMap(v reflect.Value) map[string]reflect.Value { | ||
iter := v.MapRange() | ||
out := make(map[string]reflect.Value) | ||
|
||
for iter.Next() { | ||
mv := reflect.ValueOf(iter.Value().Interface()) | ||
|
||
if !mv.IsValid() { | ||
continue | ||
} | ||
|
||
out[ValueString(iter.Key())] = mv | ||
} | ||
|
||
return out | ||
} | ||
|
||
func ValueString(v reflect.Value) string { | ||
if IsZero(v) { | ||
return "" | ||
} | ||
|
||
return fmt.Sprint(v.Interface()) | ||
} | ||
|
||
func SliceValues(v reflect.Value) []reflect.Value { | ||
out := make([]reflect.Value, 0, v.Len()) | ||
|
||
for i := 0; i < v.Len(); i++ { | ||
out = append(out, v.Index(i)) | ||
} | ||
|
||
return out | ||
} | ||
|
||
func IsKind(v reflect.Value, kinds ...reflect.Kind) error { | ||
for _, kind := range kinds { | ||
if v.Kind() == kind { | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("expected one of %q but got %q", kinds, v.Kind()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package internal | ||
|
||
import "net/url" | ||
|
||
func MergeURLValues(dst url.Values, src url.Values, scope string) { | ||
for k, v := range src { | ||
if scope != "" { | ||
k = scope + "[" + k + "]" | ||
} | ||
|
||
for _, s := range v { | ||
dst.Add(k, s) | ||
} | ||
} | ||
} |
Oops, something went wrong.