diff --git a/urlenc/build.go b/urlenc/build.go index f09c46b..cced940 100644 --- a/urlenc/build.go +++ b/urlenc/build.go @@ -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 +} diff --git a/urlenc/build_test.go b/urlenc/build_test.go index bb4d183..ddf93d2 100644 --- a/urlenc/build_test.go +++ b/urlenc/build_test.go @@ -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], + ), "" + }, + }, + ) +}