Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Preserve key order via an OrderedMap produced by decoders

Open
#105 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
22/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
go, json, yaml
Domain
cli, data

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

enhancement

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 keys as a hint and m as the truth. On encode, emit keys that still exist in m, then any extra keys (added through Map()) in sorted order. Code that mutates n.Map() directly keeps working, at worst appending new keys at the end.
  • Implement EditorNode on *OrderedMap so edits keep order precisely.
  • JSON (dec.Token()) and YAML (yaml.Node.Content) decoders already see key order; produce *OrderedMap behind an option.
  • Fix Equal, which currently type-asserts n2.(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-keys like jq.
  • Optionally add tree.M("a", 1, "b", 2) returning an *OrderedMap, mirroring tree.A(...).
Dominant language
Go
Stars
7
Forks
2
Avg merge
11h 12m
Merged PRs (30d)
43

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mojatter/tree

All issues in mojatter/tree

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.