Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roundtripping inlined tables with '000..' fails.. #400

Open
goodboy opened this issue Jun 17, 2022 · 3 comments
Open

Roundtripping inlined tables with '000..' fails.. #400

goodboy opened this issue Jun 17, 2022 · 3 comments

Comments

@goodboy
Copy link

goodboy commented Jun 17, 2022

Probably relates to #396 and #383.

The following small script exemplifies this:

import toml

enc = toml.encoder.TomlEncoder(preserve=True)
inline_table = toml.TomlDecoder().get_empty_inline_table()

clears = {
    "0000e22a.63f7e33f.01.01": {'cost': 1},
    "0000e22a.63f7e33d.01.01": {'cost': 0},
}
for k, v in clears.items():
    inline_table[k] = v

txt = toml.dumps(
    {'section0': {'section1': clears}},
    encoder=enc,
)
print(txt)
table = toml.loads(txt)

print("INLINING")
txt = toml.dumps(
    {'section0': {'section1': inline_table}},
    encoder=enc,
)
print(txt)
table = toml.loads(txt)

The 2nd roundtrip will fail with:
executing.executing.NotOneValueFound: Expected one value, found 0

With more complex scenarios the errors are different (which i can also report if needed) but I'm pretty sure this is the root issue?

@goodboy
Copy link
Author

goodboy commented Jun 17, 2022

Further follow up.
The original error i was seeing in our system was from the following toml read:

tml = '''
['section0."section1"']
clears = { "0000e22a.63f7e33f.01.01" = { cost = 1 }
, "0000e22a.63f7e33d.01.01" = { cost = 0 }
, "0000e22a.63f7e346.01.01" = { cost = 0.355 }
, "0000e22a.63f7e347.01.01" = { cost = 0.5 }
, "0000e22a.63fc4d7a.01.01" = { cost = 1 }
, "0000e22a.64168864.01.01" = { cost = 1 }
 }
'''
table = toml.loads(tml) #, encoder=enc)
print("READING TABLE")

The original inline table style was generated using suggestions from #383, but according to the spec, this output is invalid?

Output error:

toml.decoder.TomlDecodeError: Found invalid character in key name: '"'. Try quoting the key name.

Interestingly changing the contents to the following avoids this parse error?

['section0."section1"']
clears = { "0000e22a.63f7e33f.01.01" = { cost = 1 } \
, "0000e22a.63f7e33d.01.01" = { cost = 0 } \
, "0000e22a.63f7e346.01.01" = { cost = 0.355 } \
, "0000e22a.63f7e347.01.01" = { cost = 0.5 } \
, "0000e22a.63fc4d7a.01.01" = { cost = 1 } \
, "0000e22a.64168864.01.01" = { cost = 1 } \
 }

So it would seem that writing inline tables isn't working in a way that they can be read back, but oddly only when some other strange logic is causing an unrelated, and incorrect, key parsing hangup.

@goodboy
Copy link
Author

goodboy commented Jun 17, 2022

If y'all want me to write some tests up for this stuff i think i can manage 😂

@goodboy
Copy link
Author

goodboy commented Jun 17, 2022

Ahh so looking at the spec: https://toml.io/en/v1.0.0#inline-table

Inline tables are intended to appear on a single line. A terminating comma (also called trailing comma) is not permitted after the last key/value pair in an inline table. No newlines are allowed between the curly braces unless they are valid within a value. Even so, it is strongly discouraged to break an inline table onto multiples lines. If you find yourself gripped with this desire, it means you should be using standard tables.

Thus i guess the motivation for #396 to work if you want to separate inline-table entries by newlines.

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

No branches or pull requests

1 participant