| 1 |
From: Tom Wright <[email protected]> |
| 2 |
Date: Sat, 1 Aug 2026 01:44:34 +0100 |
| 3 |
Subject: Support go-toml v2.4.3 |
| 4 |
|
| 5 |
go-toml v2.4 rejects encoding a bare value as a document root, which the |
| 6 |
TOML writer relied on when the top level value isn't a table - the result |
| 7 |
of selecting a scalar or list out of a document. Every *_to_toml case in |
| 8 |
the cross format tests failed with "cannot encode a X as a document |
| 9 |
root", as reported in the Debian build. |
| 10 |
|
| 11 |
The writer now encodes those values against a placeholder key and strips |
| 12 |
the "<key> = " prefix back off, so go-toml still owns value formatting |
| 13 |
and the output is unchanged. Using the inline tag option on the |
| 14 |
placeholder also fixes a top level list of tables, which previously |
| 15 |
emitted table headers with an empty key. |
| 16 |
|
| 17 |
Fixes #550 |
| 18 |
|
| 19 |
Bug: https://github.com/TomWright/dasel/issues/550 |
| 20 |
Origin: upstream, https://github.com/TomWright/dasel/commit/70fd28f38c0e7eb35ef1a034813daf1976e90485 |
| 21 |
--- |
| 22 |
go.mod | 2 +- |
| 23 |
go.sum | 4 +- |
| 24 |
parsing/toml/toml_writer.go | 88 ++++++++++++++++++++++++++++------------ |
| 25 |
parsing/toml/toml_writer_test.go | 74 +++++++++++++++++++++++++++++++++ |
| 26 |
4 files changed, 140 insertions(+), 28 deletions(-) |
| 27 |
|
| 28 |
diff --git a/go.mod b/go.mod |
| 29 |
index 71227d8..5cfb3d5 100644 |
| 30 |
--- a/go.mod |
| 31 |
+++ b/go.mod |
| 32 |
@@ -10,7 +10,7 @@ require ( |
| 33 |
github.com/goccy/go-json v0.10.6 |
| 34 |
github.com/google/go-cmp v0.7.0 |
| 35 |
github.com/hashicorp/hcl/v2 v2.24.0 |
| 36 |
- github.com/pelletier/go-toml/v2 v2.2.5-0.20250826075308-a0e846496753 |
| 37 |
+ github.com/pelletier/go-toml/v2 v2.4.3 |
| 38 |
github.com/zclconf/go-cty v1.17.0 |
| 39 |
go.yaml.in/yaml/v4 v4.0.0-rc.3 |
| 40 |
gopkg.in/ini.v1 v1.67.0 |
| 41 |
diff --git a/go.sum b/go.sum |
| 42 |
index ab1f40e..ce5ca18 100644 |
| 43 |
--- a/go.sum |
| 44 |
+++ b/go.sum |
| 45 |
@@ -66,8 +66,8 @@ github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELU |
| 46 |
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= |
| 47 |
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc= |
| 48 |
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= |
| 49 |
-github.com/pelletier/go-toml/v2 v2.2.5-0.20250826075308-a0e846496753 h1:aTpyfgn3dz2npHl011BHQehdSavqjzhZdE6fJuJlO3A= |
| 50 |
-github.com/pelletier/go-toml/v2 v2.2.5-0.20250826075308-a0e846496753/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= |
| 51 |
+github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY= |
| 52 |
+github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= |
| 53 |
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= |
| 54 |
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
| 55 |
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= |
| 56 |
diff --git a/parsing/toml/toml_writer.go b/parsing/toml/toml_writer.go |
| 57 |
index 9108274..fd115ba 100644 |
| 58 |
--- a/parsing/toml/toml_writer.go |
| 59 |
+++ b/parsing/toml/toml_writer.go |
| 60 |
@@ -33,28 +33,6 @@ func (j *tomlWriter) Write(value *model.Value) ([]byte, error) { |
| 61 |
return nil, fmt.Errorf("nil value") |
| 62 |
} |
| 63 |
|
| 64 |
- var goValue interface{} |
| 65 |
- var err error |
| 66 |
- |
| 67 |
- if value.IsMap() { |
| 68 |
- goValue, err = buildGoValueForMap(value) |
| 69 |
- if err != nil { |
| 70 |
- return nil, fmt.Errorf("failed to construct go value: %w", err) |
| 71 |
- } |
| 72 |
- } else { |
| 73 |
- // handle scalars, slices, etc. Use goTypeAndValue to get a concrete reflect.Value |
| 74 |
- typ, rv, err := goTypeAndValue(value) |
| 75 |
- if err != nil { |
| 76 |
- return nil, fmt.Errorf("failed to convert non-map top-level value: %w", err) |
| 77 |
- } |
| 78 |
- // For nil/zero interface, ensure we pass nil interface rather than a typed zero. |
| 79 |
- if typ.Kind() == reflect.Interface && rv.IsZero() { |
| 80 |
- goValue = nil |
| 81 |
- } else { |
| 82 |
- goValue = rv.Interface() |
| 83 |
- } |
| 84 |
- } |
| 85 |
- |
| 86 |
// We currently encode the top level value directly. We have little control over nested values. |
| 87 |
// Perhaps it would be better to implement a custom Encoder that respects metadata on nested values? |
| 88 |
var buf bytes.Buffer |
| 89 |
@@ -65,10 +43,25 @@ func (j *tomlWriter) Write(value *model.Value) ([]byte, error) { |
| 90 |
encoder.SetArraysMultiline(false) |
| 91 |
} |
| 92 |
|
| 93 |
- if err := encoder.Encode(goValue); err != nil { |
| 94 |
- return nil, fmt.Errorf("toml encode failed: %w", err) |
| 95 |
+ var outBytes []byte |
| 96 |
+ |
| 97 |
+ if value.IsMap() { |
| 98 |
+ goValue, err := buildGoValueForMap(value) |
| 99 |
+ if err != nil { |
| 100 |
+ return nil, fmt.Errorf("failed to construct go value: %w", err) |
| 101 |
+ } |
| 102 |
+ if err := encoder.Encode(goValue); err != nil { |
| 103 |
+ return nil, fmt.Errorf("toml encode failed: %w", err) |
| 104 |
+ } |
| 105 |
+ outBytes = buf.Bytes() |
| 106 |
+ } else { |
| 107 |
+ // handle scalars, slices, etc. |
| 108 |
+ var err error |
| 109 |
+ outBytes, err = encodeRootValue(encoder, &buf, value) |
| 110 |
+ if err != nil { |
| 111 |
+ return nil, err |
| 112 |
+ } |
| 113 |
} |
| 114 |
- outBytes := buf.Bytes() |
| 115 |
|
| 116 |
// Ensure trailing newline for consistency with other format writers/tests. |
| 117 |
if len(outBytes) == 0 || outBytes[len(outBytes)-1] != '\n' { |
| 118 |
@@ -78,6 +71,51 @@ func (j *tomlWriter) Write(value *model.Value) ([]byte, error) { |
| 119 |
return outBytes, nil |
| 120 |
} |
| 121 |
|
| 122 |
+// rootValueKey is the placeholder key used when encoding a non-table document root. |
| 123 |
+const rootValueKey = "v" |
| 124 |
+ |
| 125 |
+// encodeRootValue encodes a top level value that isn't a map, e.g. the result of |
| 126 |
+// selecting a single scalar or list out of a document. |
| 127 |
+// A TOML document root must be a table, so go-toml will only encode these values |
| 128 |
+// as part of a key/value pair. We encode against a placeholder key and strip the |
| 129 |
+// `<key> = ` prefix back off, leaving go-toml in charge of formatting the value. |
| 130 |
+func encodeRootValue(encoder *pkg.Encoder, buf *bytes.Buffer, value *model.Value) ([]byte, error) { |
| 131 |
+ typ, rv, err := goTypeAndValue(value) |
| 132 |
+ if err != nil { |
| 133 |
+ return nil, fmt.Errorf("failed to convert non-map top-level value: %w", err) |
| 134 |
+ } |
| 135 |
+ |
| 136 |
+ // A null root has no TOML representation at all. Pass the nil interface |
| 137 |
+ // through so go-toml reports it rather than silently writing nothing. |
| 138 |
+ if typ.Kind() == reflect.Interface && rv.IsZero() { |
| 139 |
+ if err := encoder.Encode(nil); err != nil { |
| 140 |
+ return nil, fmt.Errorf("toml encode failed: %w", err) |
| 141 |
+ } |
| 142 |
+ return buf.Bytes(), nil |
| 143 |
+ } |
| 144 |
+ |
| 145 |
+ // The inline option keeps maps and lists of maps as inline tables, rather than |
| 146 |
+ // go-toml emitting table headers built from the placeholder key. |
| 147 |
+ wrapperType := reflect.StructOf([]reflect.StructField{{ |
| 148 |
+ Name: "F0", |
| 149 |
+ Type: typ, |
| 150 |
+ Tag: reflect.StructTag(fmt.Sprintf(`toml:"%s,inline"`, rootValueKey)), |
| 151 |
+ }}) |
| 152 |
+ wrapper := reflect.New(wrapperType).Elem() |
| 153 |
+ wrapper.Field(0).Set(rv) |
| 154 |
+ |
| 155 |
+ if err := encoder.Encode(wrapper.Interface()); err != nil { |
| 156 |
+ return nil, fmt.Errorf("toml encode failed: %w", err) |
| 157 |
+ } |
| 158 |
+ |
| 159 |
+ prefix := []byte(rootValueKey + " = ") |
| 160 |
+ out := buf.Bytes() |
| 161 |
+ if !bytes.HasPrefix(out, prefix) { |
| 162 |
+ return nil, fmt.Errorf("toml encode failed: unexpected encoding of top-level value: %s", string(out)) |
| 163 |
+ } |
| 164 |
+ return out[len(prefix):], nil |
| 165 |
+} |
| 166 |
+ |
| 167 |
// buildGoValueForMap constructs a reflect.Value that is a struct with fields in |
| 168 |
// the same order as keys in the provided map Value. The struct fields are |
| 169 |
// tagged with `toml:"<original key>"` so go-toml will use the original key |
| 170 |
diff --git a/parsing/toml/toml_writer_test.go b/parsing/toml/toml_writer_test.go |
| 171 |
index 0678c05..b8be964 100644 |
| 172 |
--- a/parsing/toml/toml_writer_test.go |
| 173 |
+++ b/parsing/toml/toml_writer_test.go |
| 174 |
@@ -5,6 +5,7 @@ import ( |
| 175 |
"strings" |
| 176 |
"testing" |
| 177 |
|
| 178 |
+ "github.com/tomwright/dasel/v3/model" |
| 179 |
"github.com/tomwright/dasel/v3/parsing" |
| 180 |
"github.com/tomwright/dasel/v3/parsing/toml" |
| 181 |
) |
| 182 |
@@ -241,6 +242,79 @@ func TestTomlWriter_MoreCases(t *testing.T) { |
| 183 |
}) |
| 184 |
} |
| 185 |
|
| 186 |
+// TestTomlWriter_RootValue covers top level values that aren't tables, e.g. the |
| 187 |
+// result of selecting a single scalar or list out of a document. A TOML document |
| 188 |
+// root must be a table, so these are written as bare values. |
| 189 |
+func TestTomlWriter_RootValue(t *testing.T) { |
| 190 |
+ slice := func(vals ...*model.Value) *model.Value { |
| 191 |
+ s := model.NewSliceValue() |
| 192 |
+ for _, v := range vals { |
| 193 |
+ if err := s.Append(v); err != nil { |
| 194 |
+ t.Fatalf("failed to append to slice: %v", err) |
| 195 |
+ } |
| 196 |
+ } |
| 197 |
+ return s |
| 198 |
+ } |
| 199 |
+ kvMap := func(k string, v *model.Value) *model.Value { |
| 200 |
+ m := model.NewMapValue() |
| 201 |
+ if err := m.SetMapKey(k, v); err != nil { |
| 202 |
+ t.Fatalf("failed to set map key: %v", err) |
| 203 |
+ } |
| 204 |
+ return m |
| 205 |
+ } |
| 206 |
+ |
| 207 |
+ tests := map[string]struct { |
| 208 |
+ in *model.Value |
| 209 |
+ exp string |
| 210 |
+ }{ |
| 211 |
+ "string": {in: model.NewStringValue("world"), exp: "'world'\n"}, |
| 212 |
+ "string quoting": {in: model.NewStringValue(`it's "here"`), exp: "\"it's \\\"here\\\"\"\n"}, |
| 213 |
+ "int": {in: model.NewIntValue(123), exp: "123\n"}, |
| 214 |
+ "negative int": {in: model.NewIntValue(-5), exp: "-5\n"}, |
| 215 |
+ "float": {in: model.NewFloatValue(12.3), exp: "12.3\n"}, |
| 216 |
+ "bool true": {in: model.NewBoolValue(true), exp: "true\n"}, |
| 217 |
+ "bool false": {in: model.NewBoolValue(false), exp: "false\n"}, |
| 218 |
+ "empty slice": {in: slice(), exp: "[]\n"}, |
| 219 |
+ "int slice": {in: slice(model.NewIntValue(1), model.NewIntValue(2)), exp: "[1, 2]\n"}, |
| 220 |
+ "string slice": {in: slice(model.NewStringValue("a")), exp: "['a']\n"}, |
| 221 |
+ "nested slice": {in: slice(slice(model.NewIntValue(1)), slice()), exp: "[[1], []]\n"}, |
| 222 |
+ "slice of tables": {in: slice(kvMap("a", model.NewIntValue(1)), kvMap("a", model.NewIntValue(2))), exp: "[{a = 1}, {a = 2}]\n"}, |
| 223 |
+ } |
| 224 |
+ |
| 225 |
+ for name, tc := range tests { |
| 226 |
+ name := name |
| 227 |
+ tc := tc |
| 228 |
+ t.Run(name, func(t *testing.T) { |
| 229 |
+ for _, compact := range []bool{false, true} { |
| 230 |
+ opts := parsing.DefaultWriterOptions() |
| 231 |
+ opts.Compact = compact |
| 232 |
+ writer, err := toml.TOML.NewWriter(opts) |
| 233 |
+ if err != nil { |
| 234 |
+ t.Fatalf("unexpected error creating writer: %v", err) |
| 235 |
+ } |
| 236 |
+ |
| 237 |
+ out, err := writer.Write(tc.in) |
| 238 |
+ if err != nil { |
| 239 |
+ t.Fatalf("writer error (compact=%v): %v", compact, err) |
| 240 |
+ } |
| 241 |
+ if got := string(out); got != tc.exp { |
| 242 |
+ t.Fatalf("root value output mismatch (compact=%v)\nexpected:\n%s\ngot:\n%s", compact, tc.exp, got) |
| 243 |
+ } |
| 244 |
+ } |
| 245 |
+ }) |
| 246 |
+ } |
| 247 |
+ |
| 248 |
+ t.Run("null", func(t *testing.T) { |
| 249 |
+ writer, err := toml.TOML.NewWriter(parsing.DefaultWriterOptions()) |
| 250 |
+ if err != nil { |
| 251 |
+ t.Fatalf("unexpected error creating writer: %v", err) |
| 252 |
+ } |
| 253 |
+ if _, err := writer.Write(model.NewNullValue()); err == nil { |
| 254 |
+ t.Fatal("expected an error writing a null root value, got none") |
| 255 |
+ } |
| 256 |
+ }) |
| 257 |
+} |
| 258 |
+ |
| 259 |
func TestTomlWriter_Compact(t *testing.T) { |
| 260 |
doc := []byte(`title = "TOML Example" |
| 261 |
[owner] |