dasel icon indicating copy to clipboard operation
dasel copied to clipboard

Preserve order of keys

Open meain opened this issue 4 years ago • 4 comments
trafficstars

Is your feature request related to a problem? Please describe. I was using dasel to bump the version in a package.json file. The actual edit in the diff that I have provided below is just the version changing from 0.0.1 to 0.0.2 but since dasel rearranges the keys, that info is lost in the diff. This might be a bit of a stretch, but would it be possible to preserve the order of the keys in json and toml files? Right now, I believe it reorders the keys in alphabetical order.

Describe the solution you'd like No reordering of the keys, the diff should just be the version line

Describe alternatives you've considered Blissful ignorance of the problem (not bad)

Additional context

diff --git a/package.json b/package.json
index b49b044..a027082 100644
--- a/package.json
+++ b/package.json
@@ -1,17 +1,17 @@
 {
-  "name": "marker",
-  "version": "0.0.1",
+  "author": "Abin Simon",
   "description": "Online tool to convert markdown to presentations",
-  "main": "index.js",
-  "author": "Abin Simon",
+  "devDependencies": {
+    "snowpack": "^3.1.2"
+  },
   "license": "Apache-2.0",
+  "main": "index.js",
+  "name": "marker",
   "scripts": {
-    "start": "snowpack dev",
-    "start-node": "snowpack dev --polyfill-node",
     "build": "snowpack build",
-    "snowpack": "snowpack"
+    "snowpack": "snowpack",
+    "start": "snowpack dev",
+    "start-node": "snowpack dev --polyfill-node"
   },
-  "devDependencies": {
-    "snowpack": "^3.1.2"
-  }
+  "version": "0.0.2"
 }

meain avatar Sep 12 '21 11:09 meain

This would be fantastic.

ypicard avatar Sep 27 '21 20:09 ypicard

I am planning on looking into this. I may need to implement custom JSON encoders/decoders to get this working

TomWright avatar Sep 27 '21 21:09 TomWright

Hey, just checking in if you have any news on this. Thanks!

ypicard avatar Nov 01 '21 19:11 ypicard

Nothing yet I'm afraid - I'm pretty busy right now

TomWright avatar Nov 01 '21 20:11 TomWright

Any update on this? I would love to use dasel, but comparing the changes it makes in git becomes crazy when all the keys reorder. Find myself needing to stick with find/replace for this reason. In my case it's Yaml being reordered.

joshuadmatthews avatar Dec 09 '22 19:12 joshuadmatthews

The difficulty around this is that maps in go aren't strictly sorted, meaning I'll have to largely rethink the storage layer. I've had some ideas in this area but haven't had the time to test any of them out yet.

TomWright avatar Dec 09 '22 20:12 TomWright

Chiming in here as I found dasel after looking for something to update a toml file. In my case I'm bumping a version in a Pipfile which gets reordered.

4x0v7 avatar Dec 22 '22 05:12 4x0v7

Would think you could use same storage mechanism and just increment a sortorder value on each key as you store them, then sort by it at each level while rebuilding the file.

joshuadmatthews avatar Dec 22 '22 12:12 joshuadmatthews

The idea there is correct, but dasel unmarshals into the actual data structure, i.e. a map[string]interface{}.

I need to create a value wrapper that allows me to store things like sort order on it first.

TomWright avatar Dec 22 '22 15:12 TomWright

See my draft PR here for information on where we're at with this request: https://github.com/TomWright/dasel/pull/289

TomWright avatar Jan 21 '23 14:01 TomWright

I guess this can now be closed :D

meain avatar Oct 27 '23 15:10 meain