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

feat: add support for setting array element #34

Merged
merged 4 commits into from
Jun 26, 2024

Conversation

limaa
Copy link
Contributor

@limaa limaa commented Jun 21, 2024

Currently, the 'set' command lacks the access to an array element by index. This patch adds this feature.

Example: toml set --toml-path pyproject.toml tool.poetry.authors[0] "Marc Rijken <[email protected]>"

Currently, the 'set' command lacks the access to an array element by
index. This patch adds this feature.

Example: `toml set --toml-path pyproject.toml tool.poetry.authors[0] "Marc Rijken <[email protected]>"`
if match:
table = match.group("table")
index = int(match.group("index"))
toml_part[table][index] = value
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you a descriptive message to the user when the table or index does not exists and exit with exit code 1

README.md Show resolved Hide resolved
Whenever accessing a non-existent array, the 'set' command should error
out and print a message instead of a python exception with traceback
information.
Currently the 'set' command fails to add an array element when the index
does not already exists, it fires an'IndexError' exception.
The underlying 'tomlkit' library offers a handy
'tomlkit.items.Array.insert()' method which inserts a value before an
index. This is the proposed behavior:

  $ cat config.toml
  cities = ["Rotterdam"]

  $ toml set 'cities[1]' = 'Amsterdam' && cat config.toml
  cities = ["Rotterdam", "Amsterdam"]

  $ toml set 'cities[999]' = 'Eindhoven' && cat config.toml
  cities = ["Rotterdam", "Amsterdam", "Eindhoven"]
@limaa
Copy link
Contributor Author

limaa commented Jun 24, 2024

I've added both features:

  • error message when the accessed array is non-existent. Example: error: non-existent array 'authorRS'
  • adding element to array by using the following indices. See following example
    $ cat config.toml
    cities = ["Rotterdam"]
    
    $ toml set 'cities[1]' = 'Amsterdam' && cat config.toml
    cities = ["Rotterdam", "Amsterdam"]
    
    $ toml set 'cities[999]' = 'Eindhoven' && cat config.toml
    cities = ["Rotterdam", "Amsterdam", "Eindhoven"]

Co-authored-by: Marc Rijken <[email protected]>
@mrijken mrijken merged commit 8435a80 into mrijken:master Jun 26, 2024
1 check passed
@limaa limaa deleted the feat/set-array-element branch June 27, 2024 18:38
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.

2 participants