Skip to content

Produce valid TOML for non-map root values - #551

Open
guilherme-puida wants to merge 2 commits into
TomWright:masterfrom
guilherme-puida:push-syyrwopnwutv
Open

Produce valid TOML for non-map root values#551
guilherme-puida wants to merge 2 commits into
TomWright:masterfrom
guilherme-puida:push-syyrwopnwutv

Conversation

@guilherme-puida

Copy link
Copy Markdown
Contributor

A document like the one below (or anything that is not a key-value pair):

123

is not valid TOML. This PR fixes this by making it a key-value pair instead.

value = 123

Do note that this is a breaking change, but I guess this is fine since the previously emitted documents were just invalid.

I chose value here arbitrarily, it could be changed to another key. This fixes #550, and also bumps to-toml to the latest version.

Bare scalars and arrays are not valid TOML roots. Wrap them into a
single-key map to emit `value = ...` instead.

Fixes TomWright#550
@TomWright

Copy link
Copy Markdown
Owner

Hey @guilherme-puida , I missed this and opened this PR.

Happy to take your contribution if you could update the root value handling here to exclude value = so the test assertions don't need to change to this newStringWithFormat(toml.TOML, value = true),?

@guilherme-puida

guilherme-puida commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

By excluding "value = ", dasel would produce invalid TOML documents. For example, when running on current master:

% echo 1 | ./dasel -i json -o toml --root | tee >(python -c 'import tomllib, sys; print(tomllib.load(sys.stdin.buffer))')
1
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import tomllib, sys; print(tomllib.load(sys.stdin.buffer))
                               ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.14/tomllib/_parser.py", line 133, in load
    return loads(s, parse_float=parse_float)
  File "/usr/lib/python3.14/tomllib/_parser.py", line 174, in loads
    pos = key_value_rule(src, pos, out, header, parse_float)
  File "/usr/lib/python3.14/tomllib/_parser.py", line 403, in key_value_rule
    pos, key, value = parse_key_value_pair(src, pos, parse_float)
                      ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.14/tomllib/_parser.py", line 443, in parse_key_value_pair
    raise TOMLDecodeError("Expected '=' after a key in a key/value pair", src, pos)
tomllib.TOMLDecodeError: Expected '=' after a key in a key/value pair (at line 1, column 2)

And on this branch:

% echo 1 | ./dasel -i json -o toml --root | tee >(python -c 'import tomllib, sys; print(tomllib.load(sys.stdin.buffer))')
value = 1
{'value': 1}

Is this really what is expected? I would assume that dasel always emits valid TOML documents.

@TomWright

Copy link
Copy Markdown
Owner

It's a fair question, and you are right about the output not being directly readable by a TOML parser, but I'm cautious of anyone already using a toml output with a single value and this change breaking their pipelines.

Quite often people look to extract the value itself without any TOML document for use with other scripts, or even for use in another dasel executable.

I am torn though since ini, hcl and csv writers already have requirements around the output data.

@guilherme-puida

guilherme-puida commented Aug 1, 2026 via email

Copy link
Copy Markdown
Contributor Author

@TomWright

Copy link
Copy Markdown
Owner

I think we can take the middle road. If you could keep the current (incorrect) behaviour in-place, then open up a new PR with the change. We can let the people raise any issue they have with it, otherwise let it go in.

I do need to create a "plain" output writer which could substitute the problematic use-case I mentioned above in the future.

@guilherme-puida

Copy link
Copy Markdown
Contributor Author

I think we can take the middle road. If you could keep the current (incorrect) behaviour in-place, then open up a new PR with the change. We can let the people raise any issue they have with it, otherwise let it go in.

Might be easier to merge #553 first, then I'll adapt this PR to just introduce the value = ... behavior (essentially reverting the changes from that PR).

I do need to create a "plain" output writer which could substitute the problematic use-case I mentioned above in the future.

+1

@TomWright

Copy link
Copy Markdown
Owner

OK will do, thanks.

I've just merged the plain output writer to set this PR up for success

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.

Support for github.com/pelletier/go-toml v2.4.3+?

2 participants