-
-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mergo not overriding destination with source #228
Comments
It looks like you're looking for something like github.com/mitchellh/mapstructure instead which is exactly tailored for use cases “where you don't quite know the structure of the underlying data until you read a part of it“. In your case |
@RomilShah This code works as you expected: package main
import (
"encoding/json"
"fmt"
"dario.cat/mergo"
)
var (
a = "{\"a\": \"60\"}"
b = "{\"a\": { \"name\":\"asprin\", \"dose\":0.12,\"stt\":true} }"
)
func main() {
var source, destination map[string]interface{}
err := json.Unmarshal([]byte(a), &destination)
if err != nil {
panic(err)
}
err = json.Unmarshal([]byte(b), &source)
if err != nil {
panic(err)
}
err = mergo.Map(&destination, source, mergo.WithOverride)
if err != nil {
panic(err)
}
fmt.Println(destination, " <-- result")
} It even works if you use Merge instead of Map, making mergo equivalent to mapstructure. Merging structs or maps is the same logic, although in Mergo Map is a specialized case of Merge. |
I am trying to merge values of 2 JSON into 1, where value of source should override value of destination. It is working fine for most the cases except for the case where value of 1 hashmap is string and other value is another hashmap. Below is the code snipped
playground: https://go.dev/play/p/BTmRc7wf6Js
Current output is
Excepted output is
or at-least give error that it is not possible to merge hashmap to string`
The text was updated successfully, but these errors were encountered: