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

Write table with keys containing a dot #299

Closed
corrantho opened this issue Jul 14, 2023 · 3 comments
Closed

Write table with keys containing a dot #299

corrantho opened this issue Jul 14, 2023 · 3 comments

Comments

@corrantho
Copy link

Hello,

I have the below code

    doc = tomlkit.document()
    a = tomlkit.aot()
    t = tomlkit.table()

    t.add("name", "test")
    t.add("a.prop1", "value1")
    t.add("a.prop2", "value2")
    a.append(t)
    doc.add("test", a)
    
    print(tomlkit.dumps(doc))

I'm trying to add to a table some elements containing some dots.

I'm expecting the below result:

[[test]]
name = "test"
a.prop1 = "value1"
a.prop2 = "value2"

but I'm getting

[[test]]
name = "test"
"a.prop1" = "value1"
"a.prop2" = "value2"

There are extra double quotes I don't want.

Any idea how am I using wrongly the tomlkit library?

Thanks in advance.

@frostming
Copy link
Contributor

frostming commented Jul 14, 2023

use tomlkit.key([key1, key2]) to get dotted key, or it will be regarded as a single key. The example would be:

t.add(tomlkit.key(["a", "prop1"]), "value2")

@corrantho
Copy link
Author

Thank you for your response.
It works fine when I add one property as below

import tomlkit

doc = tomlkit.document()
a = tomlkit.aot()
t = tomlkit.table()

t.add("name", "test")
t.add(tomlkit.key(["a", "prop1"]), "value1")

a.append(t)
doc.add("test", a)

print(tomlkit.dumps(doc))

But as soon as I add a second property, it fails.

import tomlkit

doc = tomlkit.document()
a = tomlkit.aot()
t = tomlkit.table()

t.add("name", "test")
t.add(tomlkit.key(["a", "prop1"]), "value1")
t.add(tomlkit.key(["a", "prop2"]), "value2")

a.append(t)
doc.add("test", a)

print(tomlkit.dumps(doc))

With the error

Traceback (most recent call last):
  File "test-tomlkit.py", line 9, in <module>
    t.add(tomlkit.key(["a", "prop2"]), "value2")
  File "C:\Users\user1\Anaconda3\lib\site-packages\tomlkit\items.py", line 1385, in add
    return self.append(key, value)
  File "C:\Users\user1\Anaconda3\lib\site-packages\tomlkit\items.py", line 1491, in append
    self._value.append(key, _item)
  File "C:\Users\user1\Anaconda3\lib\site-packages\tomlkit\container.py", line 188, in append
    self._handle_dotted_key(key, item)
  File "C:\Users\user1\Anaconda3\lib\site-packages\tomlkit\container.py", line 150, in _handle_dotted_key
    self.append(name, value)
  File "C:\Users\user1\Anaconda3\lib\site-packages\tomlkit\container.py", line 243, in append
    self._table_keys[-1] != current_body_element[0]
IndexError: list index out of range

@corrantho
Copy link
Author

I think it's fixed by #284
It appears I'm using an outdated version of tomlkit.

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

2 participants