Skip to content

Web UI saves keep router.yaml's comments and layout (#11) - #54

Merged
Cd1s merged 1 commit into
mainfrom
fix/ui-save-keeps-comments
Sep 25, 2026
Merged

Cd1s merged 1 commit into
mainfrom
fix/ui-save-keeps-comments

Conversation

@Cd1s

@Cd1s Cd1s commented Sep 25, 2026

Copy link
Copy Markdown
Owner

What

mr/yamledit.go: a web UI save no longer re-encodes router.yaml. The live file and the submitted config are both decoded into Config and encoded canonically (struct order, omitempty); where the two canonical trees differ, the live file's text is edited:

  • a changed scalar is replaced where it stands, keeping its quoting (username: "…" stays double-quoted);
  • map entries are added after the section's last entry or cut out together with their comment lines (blank lines that separate groups stay);
  • lists are matched by name (else by position); reordered named items move with their lines; unnamed lists that change length are rewritten;
  • flow collections ({name: …, proto: […]}, [a, b]) are edited inside — alignment padding and line breaks stay; plain values that would read as syntax inside a flow (::10) are quoted; [] / {} that receive content become block form;
  • values that defaults derive from a setting the page changed (a DHCP pool's lease, RA details when RA goes off) are spelled out as the page had them, so the file means exactly what was previewed.

The result must decode (with defaults) to exactly the submitted config; otherwise the canonical encoding is written, as before (comments lost), and the reason is logged. The # written by the web UI header is no longer prepended on every save.

Why

configFromJSON went JSON → map → yaml.Marshal: saving once from the web UI dropped every comment of a hand-written router.yaml and sorted all keys, and turned a one-field change into a whole-file git diff.

Verification

  • tools/ci.sh passes (private and public trees).
  • New tests (mr/yamledit_test.go): a save without changes is byte-identical for the example config and the lab config (all features); each kind of edit changes only its own line(s) — scalar, list-item scalar with a trailing comment, quoted string, sysctl map, inline list, a value inside an aligned flow item, appended flow item, removed named item, new top-level section, optional key off / on, reorder.
  • Property test: every one of the 608 scalar values of the lab config, changed one at a time through the page's JSON, is edited in place (no fallback); all but 2 change a single line (those 2 also spell out a derived default). It found and fixed quoting of ::10 in flows, multi-line flow collections, blank-line handling and the derived-default case.
  • Anchors, several documents and non-mapping files are refused (canonical fallback).

Closes #11

https://claude.ai/code/session_01HWeUjo1dmS7gBkCYHrVjCr

…ged values are edited

Every save from the web UI went JSON -> map -> yaml.Marshal: all comments dropped, keys sorted.

- yamledit.go: the live file and the page's config are both encoded canonically; where they differ
  the live file's text is edited — scalars in place (quoting kept), entries added or cut out (their
  comments too, blank separators kept), lists matched by `name`, flow collections edited inside
  (alignment and line breaks kept), `[]` / `{}` that get content become blocks. Values the defaults
  derive from a changed setting are spelled out as the page had them.
- The result must decode to exactly the submitted config, else the canonical form is written
  (comments lost, logged).
- Tests: unchanged save byte-identical (home, lab); every edit kind touches only its own lines; all
  608 scalars of the lab config edited in place one by one; anchors / multi-document refused.

Closes #11
@Cd1s
Cd1s merged commit e572177 into main Sep 25, 2026
2 checks passed
@Cd1s
Cd1s deleted the fix/ui-save-keeps-comments branch September 25, 2026 16:39
@Cd1s Cd1s mentioned this pull request Sep 25, 2026
52 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

网页保存会丢失 router.yaml 的注释并重排键

1 participant