feat: added urlenc.FnArray.

This commit is contained in:
Andrey Parhomenko 2024-07-30 02:43:03 +05:00
parent 2b31a6a970
commit 22a222d945
2 changed files with 44 additions and 0 deletions

View file

@ -69,3 +69,27 @@ func Join(builders ...Builder) Values {
}
return ret
}
type FnArray[V any] struct {
Values []V
Fn func([]V, int) (string, string)
}
func (a FnArray[V]) BuildQueryValues() Values {
values := Values{}
for i := range a.Values {
k, v := a.Fn(a.Values, i)
values.Add(k, v)
}
return values
}
func (a FnArray[V]) String() string {
unesc, err := url.QueryUnescape(
a.BuildQueryValues().Encode(),
)
if err != nil {
panic(err)
}
return unesc
}

View file

@ -2,6 +2,7 @@ package urlenc
import "testing"
import "log"
import "fmt"
func TestArray(t *testing.T) {
log.Printf(
@ -25,3 +26,22 @@ func TestJoin(t *testing.T) {
).Encode(),
)
}
func TestFnArray(t *testing.T) {
log.Printf(
"FnArray{...}: %s",
FnArray[int64]{
[]int64{
1, 2, 3, 11, 12, 13,
},
func(
a []int64, i int,
) (string, string) {
return fmt.Sprintf(
"custom_field_%d_value_changed",
a[i],
), ""
},
},
)
}