Comments (9)
Is there any progress on this ? I am facing the same issue
from mergo.
I think this is the same issue reported in Praqma/helmsman#137
from mergo.
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.
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.
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.
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.
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.
@vilipwong I want to do the release with some more fixes from v0.3.9 related bugs.
from mergo.
For those of you who need it, @Pothulapati built a fork that solves this here.
from mergo.
Related Issues (20)
- Determine whether to execute a custom transformer according to the types of SRC and DST HOT 2
- Transformers: How to access underlying datatype HOT 1
- mergo WithSliceDeepCopy into differen types will causes painc
- Missing merge only empty dst and check empty value not work for struct HOT 1
- WithSliceDeepCopy cannot merge nums in slice. Suggestion: Recomplete it as WithRecursiveOverride HOT 1
- Mergo not overriding destination with source HOT 2
- Docs: “Mergo in the wild” inaccurate HOT 2
- Mergo is not overriding destination map key with empty value with WithOverwriteWithEmptyValue
- Add support OSS-Fuzz
- Merge Slices HOT 6
- bool values are not merged properly HOT 7
- New module path breaks go install of package with indirect dependency on mergo HOT 13
- How to overwrite specified field with empty value in struct HOT 1
- Choosing fields of struct to override
- Vanity name makes live hard for developers HOT 9
- Vanity url doesn't redirect to github repo HOT 5
- module declares its path as: dario.cat/mergo but was required as: github.com/imdario/mergo HOT 7
- bool issue HOT 2
- unable to go get package
- dario.cat has expired HOT 3
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 mergo.