Giter VIP home page Giter VIP logo

Comments (9)

deankarn avatar deankarn commented on August 15, 2024 1

I will try to take a look at this tonight after work for you @frederikhors

from mold.

deankarn avatar deankarn commented on August 15, 2024 1

@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.

deankarn avatar deankarn commented on August 15, 2024 1

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.

frederikhors avatar frederikhors commented on August 15, 2024

I think this is the same, right? asaskevich/govalidator#329

from mold.

frederikhors avatar frederikhors commented on August 15, 2024

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
}
  1. Why ok in s, ok := v.Interface().(*string) is always false?

  2. How to set v to nil? Why is v.Set(nil) wrong?

Can you help me understand, please?

from mold.

frederikhors avatar frederikhors commented on August 15, 2024

@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.

frederikhors avatar frederikhors commented on August 15, 2024

I'll continue with other PRs and conflicts. But please do not forget about this: it's very important for me. 😅

from mold.

frederikhors avatar frederikhors commented on August 15, 2024

I think I like it.

from mold.

deankarn avatar deankarn commented on August 15, 2024

v4 release complete and this is now possible.

from mold.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.