Code Monkey home page Code Monkey logo

Comments (9)

hectorj2f avatar hectorj2f commented on August 22, 2024 1

Is there any progress on this ? I am facing the same issue

from mergo.

luisdavim avatar luisdavim commented on August 22, 2024

I think this is the same issue reported in Praqma/helmsman#137

from mergo.

svyotov avatar svyotov commented on August 22, 2024

As far as I can see this is because of https://github.com/imdario/mergo/blob/9316a62528ac99aaecb4e47eadd6dc8aa6533d58/mergo.go#L36-L59
Merging Boolean and Numeric values that are false or zero are considered empty thus replaced.

from mergo.

github4jiawen avatar github4jiawen commented on August 22, 2024

This can be accomplished with custom bool and transformer feature

package main

import (
	"encoding/json"
	"log"
	"reflect"

	"github.com/imdario/mergo"
)

type BoolInSetting struct {
	Set   bool
	Value bool
}

func (b *BoolInSetting) UnmarshalJSON(data []byte) error {
	b.Set = true

	err := json.Unmarshal(data, &b.Value)
	return err
}

type BoolInSettingTransformer struct {
}

func (t BoolInSettingTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
	if (typ == reflect.TypeOf(BoolInSetting{})) {
		return func(dst, src reflect.Value) error {
			if dst.CanSet() && src.FieldByName("Set").Bool() {
				dst.Set(src)
			}
			return nil
		}
	}
	return nil
}

type ConfigWithBool struct {
	BooleanField BoolInSetting `json:"BooleanField"`
}

func main() {
	var trusy ConfigWithBool
	var falsy ConfigWithBool
	var err error
	err = json.Unmarshal([]byte(`{"BooleanField": true}`), &trusy)
	if err != nil {
		log.Fatalln(err)
	}
	err = json.Unmarshal([]byte(`{"BooleanField": false}`), &falsy)
	if err != nil {
		log.Fatalln(err)
	}
	err = mergo.Merge(&trusy, falsy, mergo.WithTransformers(BoolInSettingTransformer{}))
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("%+v", trusy)
}

from mergo.

vilipwong avatar vilipwong commented on August 22, 2024

Hello, is mergo.WithOverride or mergo.WithOverwriteWithEmptyValue suppose to solve this ?

I faced the same issue with string and boolean value, using mergo.WithOverwriteWithEmptyValue e.g:

p1 := map[string]interface{}{
	"A": 3, "B": "note", "C": true,
}
p2 := map[string]interface{}{
	"B": "", "C": false,
}
mergo.Merge(&p1, &p2, mergo.WithOverwriteWithEmptyValue)

Expected result:
{A:3 B:"" C:false}

Actual result:
{A:3 B:"note" C:true}

from mergo.

darccio avatar darccio commented on August 22, 2024

Ok, let's see. The OP's example expects f2.Bar to be false, and it is the destination variable, so it's always truthy.

I rewrote the example with more meaningful variable names, as it was misleading me to the opposite direction:

import (
  "testing"
  "github.com/imdario/mergo"
  "github.com/stretchr/testify/assert"
)
func TestBoolean(t *testing.T) {
  type Foo struct {
    Bar bool `json:"bar"`
  }

  f1 := Foo{Bar: true}
  f2 := Foo{Bar: false}

  mergo.Merge(&f2, f1)
  assert.False(t, f2.Bar)
}

Your intention, from what I understood, is to keep the false value as is. Not sure why as I'm missing context here. I'm going to tag along and restate what I said in #24:

This is the expected behavior. As stated in README:

Mergo is intended to assign only zero value fields on destination with source value.

Provided this, your example won't work with the current implementation. If you need to protect false values, you must follow @github4jiawen example. Transformers are a simple way to filter the main behavior.

About your case, @vilipwong, I pushed another test to check it out, and I confirm you that mergo.WithOverwriteWithEmptyValue will help you. Take into account that you have the opposite problem, as you want to override a truthy value with false.

from mergo.

vilipwong avatar vilipwong commented on August 22, 2024

I see. i was using release v0.3.9, it works when i go get @master. Any reason why its not released ?
@imdario

from mergo.

darccio avatar darccio commented on August 22, 2024

@vilipwong I want to do the release with some more fixes from v0.3.9 related bugs.

from mergo.

TomOperator avatar TomOperator commented on August 22, 2024

For those of you who need it, @Pothulapati built a fork that solves this here.

from mergo.

Related Issues (20)

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.