Preserve key order via an OrderedMap produced by decoders
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 22/100
Research direction
Start with node.go, where Map and the Equal method live; the issue says Equal type-asserts n2.(Map) and would panic on an OrderedMap. Then look at the JSON and YAML decoders and the encoders, which currently sort keys. The design is settled on paper but the scope spans node types, encoders, decoders and the tq flags, so confirm the approach with maintainers before writing code. Done means order survives a decode-encode round trip and Equal no longer panics.
Written by the indexing model from the issue text.
Description
Problem
Map is map[string]Node and encoders always sort keys, so {"z":1,"a":2} comes out as {"a":2,"z":1}. For tq this means --inplace edits reorder the whole file. Changing Map into a struct would break the tree.Map{"a": tree.V(1)} literal syntax, which we want to keep.
Proposed solution
Add a separate node type used only by decoders (opt-in), and keep Map as is:
type OrderedMap struct {
keys []string // order hint
m Map // source of truth
}
func (o *OrderedMap) Type() Type { return TypeMap }
func (o *OrderedMap) Map() Map { return o.m }
- Treat
keysas a hint andmas the truth. On encode, emitkeysthat still exist inm, then any extra keys (added throughMap()) in sorted order. Code that mutatesn.Map()directly keeps working, at worst appending new keys at the end. - Implement
EditorNodeon*OrderedMapso edits keep order precisely. - JSON (
dec.Token()) and YAML (yaml.Node.Content) decoders already see key order; produce*OrderedMapbehind an option. - Fix
Equal, which currently type-assertsn2.(Map)and would panic (https://github.com/mojatter/tree/blob/2d4fd3d11f3e28c7c95f51d091b94ef0a6ed18f1/node.go#L491-L492). - tq: preserve input order by default and add
-S/--sort-keyslike jq. - Optionally add
tree.M("a", 1, "b", 2)returning an*OrderedMap, mirroringtree.A(...).
- Dominant language
- Go
- Stars
- 7
- Forks
- 2
- Avg merge
- 11h 12m
- Merged PRs (30d)
- 43
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from mojatter/tree
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
query: Read null like jq in key steps, comparisons and contains()Possibly taken @mojatter claimed this today. Openbug
Difficulty 4/5 3-5 days Newbie friendliness 55/100
mojatter/tree#152 · 3 comments · 1 assignee ·
Maintainers usually reply within 1 day
Similar issues
-
fix(handlers): explicitly set Content-Type header in static Index handlerPossibly taken @kage-zero claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
eugenioenko/ttt#1206 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
open-policy-agent/opa#9426 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
rorkai/App-Store-Connect-CLI#3192 ·
Maintainers usually reply within 1 day
-
t:urlDecodeUni does not map U+3002 and U+FF61 to a dot, unlike ModSecurityPossibly taken @fzipi claimed this today. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day