43 lines
907 B
Go
43 lines
907 B
Go
package playground
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/go-playground/assert/v2"
|
|
"github.com/go-playground/mold/v4"
|
|
"github.com/go-playground/mold/v4/modifiers"
|
|
"github.com/moznion/go-optional"
|
|
)
|
|
|
|
var tform *mold.Transformer
|
|
|
|
type UpdateCustomerDTO struct {
|
|
FirstName optional.Option[string] `mod:"trim"`
|
|
}
|
|
|
|
func TestMoldTrimOnOptional(t *testing.T) {
|
|
tform = modifiers.New()
|
|
|
|
tform.RegisterInterceptor(func(current reflect.Value) (inner reflect.Value) {
|
|
opt, ok := current.Interface().(optional.Option[string])
|
|
if !ok {
|
|
return current
|
|
} else if opt.IsNone() {
|
|
return reflect.ValueOf(nil)
|
|
}
|
|
return current.Index(0)
|
|
}, optional.Option[string]{})
|
|
|
|
dto := UpdateCustomerDTO{FirstName: optional.Some(" RaviAnand ")}
|
|
|
|
err := tform.Struct(context.Background(), &dto)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
assert.Equal(t, dto.FirstName.Unwrap(), "RaviAnand")
|
|
}
|