diff --git a/internal/edit/yaml.go b/internal/edit/yaml.go index b708b4539..9b4704901 100644 --- a/internal/edit/yaml.go +++ b/internal/edit/yaml.go @@ -23,11 +23,11 @@ func GetYAML(path, keyPath string) (string, bool) { if err != nil || root == nil { return "", false } - n := lookupYAML(root, strings.Split(keyPath, ".")) - if n == nil || n.Kind != yaml.ScalarNode { + n := root.lookup(strings.Split(keyPath, ".")) + if n == nil || n.kind != yaml.ScalarNode { return "", false } - return n.Value, true + return n.value, true } // GetYAMLMap reads the scalar entries of the mapping at a key path. @@ -36,14 +36,14 @@ func GetYAMLMap(path, keyPath string) map[string]string { if err != nil || root == nil { return nil } - n := lookupYAML(root, strings.Split(keyPath, ".")) - if n == nil || n.Kind != yaml.MappingNode { + n := root.lookup(strings.Split(keyPath, ".")) + if n == nil || n.kind != yaml.MappingNode { return nil } out := map[string]string{} - for i := 0; i+1 < len(n.Content); i += 2 { - if v := n.Content[i+1]; v.Kind == yaml.ScalarNode { - out[n.Content[i].Value] = v.Value + for i := 0; i+1 < len(n.content); i += 2 { + if v := &n.content[i+1]; v.kind == yaml.ScalarNode { + out[n.content[i].value] = v.value } } return out @@ -55,14 +55,14 @@ func GetYAMLList(path, keyPath string) []string { if err != nil || root == nil { return nil } - n := lookupYAML(root, strings.Split(keyPath, ".")) - if n == nil || n.Kind != yaml.SequenceNode { + n := root.lookup(strings.Split(keyPath, ".")) + if n == nil || n.kind != yaml.SequenceNode { return nil } var out []string - for _, c := range n.Content { - if c.Kind == yaml.ScalarNode { - out = append(out, c.Value) + for i := range n.content { + if c := &n.content[i]; c.kind == yaml.ScalarNode { + out = append(out, c.value) } } return out @@ -70,9 +70,10 @@ func GetYAMLList(path, keyPath string) []string { // GetYAMLText reads the value at a key path as the YAML it is written in — // a list flow ("[a, b]") or block as it is — for SetYAML to put back as a -// YAMLText. +// YAMLText. It reads yaml.v3's whole tree, styles and comments, which the +// other looks don't keep (loadYAML). func GetYAMLText(path, keyPath string) (string, bool) { - root, err := loadYAML(path) + root, err := filememo.Read("yaml text root", path, parseYAMLRoot) if err != nil || root == nil { return "", false } @@ -289,8 +290,8 @@ func JSONToYAML(src, dst string) error { // loadYAML reads the first document's mapping without decoding its values. // Unrelated duplicate keys, complex keys or invalid tags must not hide a // readable model setting. Writers use loadYAMLForEdit to validate the whole file. -func loadYAML(path string) (*yaml.Node, error) { - root, err := filememo.Read("yaml root", path, parseYAMLRoot) +func loadYAML(path string) (*yamlLook, error) { + root, err := filememo.Read("yaml root", path, parseYAMLLook) if errors.Is(err, fs.ErrNotExist) { return nil, nil } @@ -300,9 +301,71 @@ func loadYAML(path string) (*yaml.Node, error) { return root, nil } -// parseYAMLRoot is loadYAML's parse, made once each time the file is read -// and shared by every look (GetYAML, GetYAMLMap, GetYAMLList, -// GetYAMLText), so read only. Each look parsed the file again: dsh's +// yamlLook is what the looks (GetYAML, GetYAMLMap, GetYAMLList) read of a +// node: its kind, a scalar's value and the nodes under it, held inline. A +// yaml.Node is ~160 bytes behind a pointer, with a tag, styles, comments +// and places none of them reads: kept for dsh's 68 KB settings.yaml it +// was ~4.5 MB of an idle tray's 10 MB heap. +type yamlLook struct { + kind yaml.Kind + value string + content []yamlLook +} + +// parseYAMLLook is parseYAMLRoot's tree as a yamlLook. +func parseYAMLLook(b []byte) (*yamlLook, error) { + root, err := parseYAMLRoot(b) + if err != nil || root == nil { + return nil, err + } + l := lookOf(root) + return &l, nil +} + +func lookOf(n *yaml.Node) yamlLook { + l := yamlLook{kind: n.Kind} + if n.Kind == yaml.ScalarNode { + l.value = n.Value + } + if len(n.Content) > 0 { + l.content = make([]yamlLook, len(n.Content)) + for i, c := range n.Content { + if n.Kind == yaml.MappingNode && i%2 == 0 { + // a key is looked at by its value whatever its kind, as + // lookupYAML does + l.content[i] = yamlLook{kind: c.Kind, value: c.Value} + continue + } + l.content[i] = lookOf(c) + } + } + return l +} + +// lookup is lookupYAML on a yamlLook. +func (n *yamlLook) lookup(parts []string) *yamlLook { + for _, p := range parts { + if n.kind != yaml.MappingNode { + return nil + } + var next *yamlLook + for i := 0; i+1 < len(n.content); i += 2 { + if n.content[i].value == p { + next = &n.content[i+1] + break + } + } + if next == nil { + return nil + } + n = next + } + return n +} + +// parseYAMLRoot is the looks' parse, made once each time the file is read +// and shared by every look (GetYAML, GetYAMLMap, GetYAMLList through +// parseYAMLLook, GetYAMLText), so read only. Each look parsed the file again: dsh's // 80 KB settings.yaml, read per field by every /api/drift poll, was // 465 MB of what a live magpie allocated in 37 minutes. func parseYAMLRoot(b []byte) (*yaml.Node, error) { diff --git a/internal/edit/yaml_look_test.go b/internal/edit/yaml_look_test.go new file mode 100644 index 000000000..1fd2e4e5c --- /dev/null +++ b/internal/edit/yaml_look_test.go @@ -0,0 +1,106 @@ +package edit + +import ( + "os" + "path/filepath" + "reflect" + "strings" + "testing" + + "gopkg.in/yaml.v3" +) + +// TestYAMLLooksReadAsTheNodeTree holds GetYAML, GetYAMLMap and GetYAMLList, +// which read the slim tree loadYAML keeps, to what yaml.v3's own tree +// gives for every key path in a file with comments, anchors, aliases, +// flow and block lists, quoted and odd keys. +func TestYAMLLooksReadAsTheNodeTree(t *testing.T) { + src := `# head +base: &base + url: http://127.0.0.1:3425/v1 # line + key: "k" +providers: + magpie: + <<: *base + model: gpt-5.4 + other: *base + "quoted.key": {a: 1, b: [x, y]} + ? complex + : value +list: + - a + - {b: c} + - [d, e] +flow: [1, 2, three] +empty: {} +none: +num: 3 +? [k1, k2] +: v +` + path := filepath.Join(t.TempDir(), "c.yaml") + if err := os.WriteFile(path, []byte(src), 0o600); err != nil { + t.Fatal(err) + } + root, err := parseYAMLRoot([]byte(src)) + if err != nil { + t.Fatal(err) + } + var paths []string + var walk func(n *yaml.Node, pre string) + walk = func(n *yaml.Node, pre string) { + if n.Kind != yaml.MappingNode { + return + } + for i := 0; i+1 < len(n.Content); i += 2 { + p := n.Content[i].Value + if pre != "" { + p = pre + "." + p + } + paths = append(paths, p) + walk(n.Content[i+1], p) + } + } + walk(root, "") + paths = append(paths, "missing", "providers.missing", "num.deeper", "list.a") + if len(paths) < 20 { + t.Fatalf("walked only %d paths", len(paths)) + } + for _, p := range paths { + n := lookupYAML(root, strings.Split(p, ".")) + wantV, wantOK := "", false + var wantM map[string]string + var wantL []string + if n != nil { + switch n.Kind { + case yaml.ScalarNode: + wantV, wantOK = n.Value, true + case yaml.MappingNode: + wantM = map[string]string{} + for i := 0; i+1 < len(n.Content); i += 2 { + if n.Content[i+1].Kind == yaml.ScalarNode { + wantM[n.Content[i].Value] = n.Content[i+1].Value + } + } + case yaml.SequenceNode: + for _, c := range n.Content { + if c.Kind == yaml.ScalarNode { + wantL = append(wantL, c.Value) + } + } + } + } + if v, ok := GetYAML(path, p); v != wantV || ok != wantOK { + t.Errorf("GetYAML(%q) = %q, %v; the node tree reads %q, %v", p, v, ok, wantV, wantOK) + } + if m := GetYAMLMap(path, p); !reflect.DeepEqual(m, wantM) { + t.Errorf("GetYAMLMap(%q) = %v; the node tree reads %v", p, m, wantM) + } + if l := GetYAMLList(path, p); !reflect.DeepEqual(l, wantL) { + t.Errorf("GetYAMLList(%q) = %v; the node tree reads %v", p, l, wantL) + } + } + if v, ok := GetYAMLText(path, "flow"); !ok || v != "[1, 2, three]" { + t.Errorf("GetYAMLText(flow) = %q, %v; want the flow list as written", v, ok) + } +}