wk8 / go-ordered-map

Optimal implementation of ordered maps for Golang - ie maps that remember the order in which keys were inserted.
Apache License 2.0
523 stars 40 forks source link

Q: Is it possible to use nested json with OrderedMap? #27

Closed dtseiler closed 1 year ago

dtseiler commented 1 year ago

Good afternoon,

Trying to use orderedmap to read in json, set and/or delete a handful of values, then write the whole thing out again. We'd like to preserve the order if possible so I stumbled upon your library. If I have a json file like this:

{
  "postgres": {
    "version": "12",
    "replication": {
      "slots": {
        "foo": true,
        "bar": true
      }
    },
    "archive_mode": "on",
    "cluster_name": "dts",
    "databases": [
      "dtsdb"
    ],
    "extensions": {
      "dtsdb": {
        "cube": true,
        "earthdistance": true,
        "pgcrypto": true,
        "uuid-ossp": false
      }
    }
  }
  "linux": {
    "sysctl": {
      "vm.nr_hugepages": 2200
    }
  },
}

So we can read it into an OrderedMap, add, update or delete a few of the values and then write everything out to the file. I'm not the greatest go wizard in the land so I'm struggling to think of how to do this with your lib. I'm not able to get to any of the nested values. Is this possible? For example if I want to set postgres.archive_mode to off or postgres.replication.slots.foo to false.

wk8 commented 1 year ago

Hey @dtseiler .

Do you know the shape of the JSON you're trying to parse ahead of time? For example with your example do you know that you expect something like

type MyJson struct {
  Postgres Postgres `json:"postgres"`
  // ...
}

type Postgres struct {
  Version string `json:"version"`
  Replication // ...
}

or you'd like to be able to parse any arbitrary nested JSON and keep it ordered all the way down?

wk8 commented 1 year ago

Closing due to inactivity.