Comments (4)
Line 386 in 0c96289
May be we should add the values to the root in case of no paths specified
from dyno.
I thought about this in the past, and I decided to not allow it for consistency reasons.
For example Append()
and Delete()
when modifies a slice, the new slice value must be "set back". If the value you append to or delete from is the "root", the implementation would not be able to set it back (unless a pointer to it would be passed), it would have to be returned.
I agree that it could be useful in case of Set
. So I'm not completely "shutting" down this idea.
On the other hand this would not be a significant gain. For example when you unmarshal your source YAML, you know it's a map, so you may unmarshal it right into a map. And if you already have your root as a map, you can simply set top-level elements using simple index expressions (e.g. src["field3"] = 123
), without the need of dyno.
from dyno.
Ok, stop the press!
Your task does not require any addition or change to dyno
, it is supported with how everything is now.
You say you want to add a new field3
field, so that is the path you want to set.
For example, let's say you want to add the new "field3" = 999
entry. This is all it takes:
if err := dyno.Set(m, 999, "field3"); err != nil {
panic(err)
}
Here's a complete runnable app doing that:
package main
import (
"encoding/json"
"os"
"github.com/icza/dyno"
)
func main() {
m := map[string]interface{}{
"field": map[string]interface{}{
"test": 123,
"test2": 123443,
},
"field2": "sdfsdf",
}
if err := dyno.Set(m, 999, "field3"); err != nil {
panic(err)
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(m); err != nil {
panic(err)
}
}
Output of the above app:
{
"field": {
"test": 123,
"test2": 123443
},
"field2": "sdfsdf",
"field3": 999
}
from dyno.
With the above solution, I consider this closed. If you want something else, or I misunderstood, I may reopen the issue or create a new one.
from dyno.
Related Issues (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 dyno.