Comments (9)
I will try to take a look at this tonight after work for you @frederikhors
from mold.
@frederikhors I looked into this and it is doable, BUT, I'm going to have to change the modifier function signature...a breaking change.
currently I extract the underlying value for manipulation, but to set the value of the pointer you need the parent value which I currently do not pass along to the function.
once I do though you'll be able to set the parent like so:
v.Set(reflect.Zero(parent.Type()))
Example, I pass current
to the function but not orig
which is needed to accomplish what you want.
func (t *Transformer) setByField(ctx context.Context, orig reflect.Value, ct *cTag) (err error) {
current, kind := extractType(orig)
if ct != nil && ct.hasTag {
for ct != nil {
switch ct.typeof {
case typeEndKeys:
return
case typeDive:
ct = ct.next
switch kind {
case reflect.Slice, reflect.Array:
err = t.setByIterable(ctx, current, ct)
case reflect.Map:
err = t.setByMap(ctx, current, ct)
default:
err = ErrInvalidDive
}
return
default:
if !current.CanAddr() {
newVal := reflect.New(current.Type()).Elem()
newVal.Set(current)
if err = ct.fn(ctx, t, newVal, ct.param); err != nil {
return
}
orig.Set(newVal)
} else {
if err = ct.fn(ctx, t, current, ct.param); err != nil {
return
}
}
ct = ct.next
}
}
}
Since more params are being passed and I don't want to make another breaking change after this I'm going to pass an interface to the functions like I do in validator
to future proof it a bit. This may take me a bit to implement as I want to get it right.
from mold.
FYI @frederikhors initial changes made https://github.com/go-playground/mold/tree/change-function-interface
will let them sit with me for a few days before merging, would also love your opinion on the new interface.
example niling:
package main
import (
"context"
"fmt"
"log"
"reflect"
"github.com/go-playground/mold/v4"
)
var tform *mold.Transformer
func main() {
tform = mold.New()
tform.Register("nilEmptyString", nilEmptyString)
type Test struct {
String *string `mold:"nilEmptyString"`
}
myEmptyString := ""
tt := Test{String: &myEmptyString}
err := tform.Struct(context.Background(), &tt)
if err != nil {
log.Fatal(err)
}
fmt.Printf("RESULT: %#v %t\n", tt, tt.String == nil)
}
func nilEmptyString(_ context.Context, fl mold.FieldLevel) error {
fl.Parent().Set(reflect.Zero(fl.Parent().Type()))
return nil
}
from mold.
I think this is the same, right? asaskevich/govalidator#329
from mold.
I'm stuck.
Reproduction: https://play.golang.org/p/_DlluqE2Y3k
package main
import (
"context"
"fmt"
"log"
"reflect"
"github.com/go-playground/mold/v3"
)
var tform *mold.Transformer
func main() {
tform = mold.New()
tform.Register("nilEmptyString", nilEmptyString)
type Test struct {
String *string `mold:"nilEmptyString"`
}
myEmptyString := ""
tt := Test{String: &myEmptyString}
err := tform.Struct(context.Background(), &tt)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", tt)
var myString string
err = tform.Field(context.Background(), &myString, "nilEmptyString")
if err != nil {
log.Fatal(err)
}
fmt.Println(myString)
}
func nilEmptyString(_ context.Context, _ *mold.Transformer, v reflect.Value, _ string) error {
s, ok := v.Interface().(*string)
if !ok { // ok is always false, why?
return nil
}
if s != nil && *s != "" {
return nil
}
if s != nil && *s == "" {
v.SetPointer(nil)
}
// v.Set(nil) // how to do this?
return nil
}
-
Why
ok
ins, ok := v.Interface().(*string)
is always false? -
How to set
v
tonil
? Why isv.Set(nil)
wrong?
Can you help me understand, please?
from mold.
@deankarn, I know that the new home could be a perfect enemy for our 20/24 a day PC habits 😄 but can I ask you to just unlock me on this problem/feature?
So I can use mold in my project right now as long as you calmly close the other PRs.
Thank you so much!
❤️
from mold.
I'll continue with other PRs and conflicts. But please do not forget about this: it's very important for me. 😅
from mold.
I think I like it.
from mold.
v4 release complete and this is now possible.
from mold.
Related Issues (15)
- Register() on modifiers.New() HOT 1
- Is this a misprint? HOT 1
- Probable bugs HOT 3
- question: plans to port validator on top of this? HOT 2
- Email, maybe a new one? HOT 3
- Add it to awesome-go HOT 2
- URGENT: Embedded structs HOT 2
- Gin example HOT 4
- Add support for "dive,keys,…,endkeys,dive" HOT 1
- `mod` tag vs `mold` tag? HOT 1
- Support for more transformers like "max length" on string etc HOT 3
- Maybe a bug, scrubs emails
- Struct func doesn't work on slice fields HOT 3
- Add docs for json tags list HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mold.