From 2ed6cfd10a058ad5c3a41cab1822d5ca0d8b8fea Mon Sep 17 00:00:00 2001 From: oudwins Date: Sun, 15 Sep 2024 21:34:22 +0200 Subject: [PATCH] feat: added transforms for slices --- slices.go | 20 +++++++++++++++++++- slices_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/slices.go b/slices.go index cc415c0..ddb90d3 100644 --- a/slices.go +++ b/slices.go @@ -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 { @@ -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 diff --git a/slices_test.go b/slices_test.go index d714208..32f0f33 100644 --- a/slices_test.go +++ b/slices_test.go @@ -1,6 +1,7 @@ package zog import ( + "strings" "testing" p "github.com/Oudwins/zog/primitives" @@ -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") +}