feat: ✨ create mold example with go-optional
This commit is contained in:
commit
bdf491a4b1
17
go.mod
Normal file
17
go.mod
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
module gitea.dikurium.ch/InnoPeak/mold-optional-playground
|
||||||
|
|
||||||
|
go 1.22.3
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/go-playground/mold/v4 v4.5.0
|
||||||
|
github.com/moznion/go-optional v0.12.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0 // indirect
|
||||||
|
github.com/gosimple/slug v1.13.1 // indirect
|
||||||
|
github.com/gosimple/unidecode v1.0.1 // indirect
|
||||||
|
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 // indirect
|
||||||
|
github.com/segmentio/go-snakecase v1.2.0 // indirect
|
||||||
|
golang.org/x/text v0.6.0 // indirect
|
||||||
|
)
|
28
go.sum
Normal file
28
go.sum
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
||||||
|
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
|
github.com/go-playground/mold/v4 v4.5.0 h1:ZXwf0uZWWxIahglRigOeuFpIuPZxvEGQJ4FxS5xxL6M=
|
||||||
|
github.com/go-playground/mold/v4 v4.5.0/go.mod h1:qUluiWEozHr7EVk1vJzJgW/kWsPwMdpTjx8LV0NEulA=
|
||||||
|
github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
|
||||||
|
github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
|
||||||
|
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
|
||||||
|
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||||
|
github.com/moznion/go-optional v0.12.0 h1:gM9YSR7kusSQHiaq2IDHU7WoJNGETT1NbuB15XU4ebA=
|
||||||
|
github.com/moznion/go-optional v0.12.0/go.mod h1:UP85Bc+uliSDFDzN7Zw8D6gBO1bdPChKFpNu1DJfCqE=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 h1:Cpx2WLIv6fuPvaJAHNhYOgYzk/8RcJXu/8+mOrxf2KM=
|
||||||
|
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734/go.mod h1:hqVOMAwu+ekffC3Tvq5N1ljnXRrFKcaSjbCmQ8JgYaI=
|
||||||
|
github.com/segmentio/go-snakecase v1.2.0 h1:4cTmEjPGi03WmyAHWBjX53viTpBkn/z+4DO++fqYvpw=
|
||||||
|
github.com/segmentio/go-snakecase v1.2.0/go.mod h1:jk1miR5MS7Na32PZUykG89Arm+1BUSYhuGR6b7+hJto=
|
||||||
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
|
||||||
|
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
42
optional_test.go
Normal file
42
optional_test.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
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")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user