feat: more structured way to accesn urlencs builder.

This commit is contained in:
Andrey Parhomenko 2024-05-30 11:30:25 +05:00
parent fabcb6fd49
commit 2b31a6a970
2 changed files with 61 additions and 22 deletions

View file

@ -1,14 +1,35 @@
package urlenc
import "strings"
import "fmt"
import "net/url"
type Pair[V any] struct {
type Values = url.Values
type Builder interface {
BuildQueryValues() Values
}
type Value[V any] struct {
Name string
Value V
}
func (a Pair[V]) String() string {
return fmt.Sprintf("%s=%v", a.Name, a.Value)
func (value Value[V]) BuildQueryValues() Values {
values := Values{}
values.Add(
value.Name,
fmt.Sprintf("%v", value.Value),
)
return values
}
func (value Value[V]) String() string {
unesc, err := url.QueryUnescape(
value.BuildQueryValues().Encode(),
)
if err != nil {
panic(err)
}
return unesc
}
type Array[V any] struct {
@ -16,21 +37,35 @@ type Array[V any] struct {
Values []V
}
func (a Array[V]) String() string {
var b strings.Builder
func (a Array[V]) BuildQueryValues() Values {
values := Values{}
for i, v := range a.Values {
fmt.Fprintf(&b, "%s[%d]=%v", a.Name, i, v)
if i < len(a.Values) - 1 {
fmt.Fprint(&b, "&")
}
values.Add(
fmt.Sprintf("%s[%d]", a.Name, i),
fmt.Sprintf("%v", v),
)
}
return b.String()
return values
}
func Join(values ...any) string {
vs := make([]string, len(values))
for i, v := range values {
vs[i] = fmt.Sprintf("%s", v)
func (a Array[V]) String() string {
unesc, err := url.QueryUnescape(
a.BuildQueryValues().Encode(),
)
if err != nil {
panic(err)
}
return strings.Join(vs, "&")
return unesc
}
func Join(builders ...Builder) Values {
ret := Values{}
for _, builder := range builders {
for k, v := range builder.BuildQueryValues() {
for _, jv := range v {
ret.Add(k, jv)
}
}
}
return ret
}

View file

@ -4,20 +4,24 @@ import "testing"
import "log"
func TestArray(t *testing.T) {
log.Printf("Array{...}: %s\n", Array[int]{
Name: "id",
Values: []int{123, 456, 789},
})
log.Printf(
"Array{...}: %s\n",
Array[int]{
Name: "id",
Values: []int{123, 456, 789},
},
)
}
func TestJoin(t *testing.T) {
log.Printf(
"Join(...): %s",
Join(
Array[int]{
"id",
[]int{123, 456, 789},
},
Pair[int]{"somename", 25},
),
Value[string]{"somename", "value with '='"},
).Encode(),
)
}