Comments (2)
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 a
is a number in the first map, bur changes to a object in the second which is a different type. mergo
is strong when it comes to merging struct
s o the same type.
mergo
provides a Map
function, but mapstructure
fits better because it is the main goal of the project to handle map
s.
from mergo.
@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.
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
- 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 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 mergo.