Skip to content

Commit

Permalink
feat: added transforms for slices
Browse files Browse the repository at this point in the history
  • Loading branch information
Oudwins committed Sep 15, 2024
1 parent a4e15e4 commit 2ed6cfd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
20 changes: 19 additions & 1 deletion slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (v *sliceProcessor) process(val any, dest any, path p.PathBuilder, ctx p.Pa
// 4. postTransforms
defer func() {
// only run posttransforms on success
if ctx.HasErrored() {
if !ctx.HasErrored() {
for _, fn := range v.postTransforms {
err := fn(dest, ctx)
if err != nil {
Expand Down Expand Up @@ -119,6 +119,24 @@ func (v *sliceProcessor) process(val any, dest any, path p.PathBuilder, ctx p.Pa
// 4. postTransforms -> defered see above
}

// Adds pretransform function to schema
func (v *sliceProcessor) PreTransform(transform p.PreTransform) *sliceProcessor {
if v.preTransforms == nil {
v.preTransforms = []p.PreTransform{}
}
v.preTransforms = append(v.preTransforms, transform)
return v
}

// Adds posttransform function to schema
func (v *sliceProcessor) PostTransform(transform p.PostTransform) *sliceProcessor {
if v.postTransforms == nil {
v.postTransforms = []p.PostTransform{}
}
v.postTransforms = append(v.postTransforms, transform)
return v
}

// !MODIFIERS

// marks field as required
Expand Down
49 changes: 47 additions & 2 deletions slices_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zog

import (
"strings"
"testing"

p "github.com/Oudwins/zog/primitives"
Expand Down Expand Up @@ -164,11 +165,55 @@ func TestSliceCustomTest(t *testing.T) {
assert.Empty(t, errs)
}

func TestSliceInvalidData(t *testing.T) {
func TestSliceWithStringInput(t *testing.T) {
input := "not a slice"
s := []string{}
schema := Slice(String())
schema := Slice(String()).Required()
errs := schema.Parse(input, &s)
assert.Nil(t, errs)
assert.Equal(t, s, []string{input})
}

func TestSliceOptionalSlice(t *testing.T) {
s := []string{}
schema := Slice(String())

errs := schema.Parse(nil, &s)
assert.Nil(t, errs)
assert.Len(t, s, 0)
}

func TestSlicePostTransform(t *testing.T) {
s := []string{}
schema := Slice(String()).PostTransform(func(dataPtr any, ctx ParseCtx) error {
s := dataPtr.(*[]string)
for i := 0; i < len(*s); i++ {
(*s)[i] = strings.ToUpper((*s)[i])
}
return nil
})

errs := schema.Parse([]string{"a", "b", "c"}, &s)

assert.Nil(t, errs)
assert.Len(t, s, 3)
assert.Equal(t, []string{"A", "B", "C"}, s)
}

func TestSliceCustomPreTransform(t *testing.T) {
s := []string{}
schema := Slice(String()).PreTransform(func(data any, ctx ParseCtx) (any, error) {
s := data.([]string)
for i := 0; i < len(s); i++ {
s[i] = strings.ToUpper(s[i])
}
return s, nil
})

errs := schema.Parse([]string{"a", "b", "c"}, &s)
assert.Nil(t, errs)
assert.Len(t, s, 3)
assert.Equal(t, s[0], "A")
assert.Equal(t, s[1], "B")
assert.Equal(t, s[2], "C")
}

0 comments on commit 2ed6cfd

Please sign in to comment.