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

Comments from original TOML end up in the wrong spot #295

Open
jcote-tc opened this issue Jun 2, 2023 · 3 comments
Open

Comments from original TOML end up in the wrong spot #295

jcote-tc opened this issue Jun 2, 2023 · 3 comments

Comments

@jcote-tc
Copy link

jcote-tc commented Jun 2, 2023

Hello,

I am using the tomlkit Python library to modify an existing TOML file.

The original file looks like this:

## My POST route comment
[[routes]]
method = 'POST'
path = '/my/post/path'
topic = 'my-topic.0'

## My GET route comment
[[routes]]
method = 'GET'
path = '/my/get/path/{key}'
topic = 'my-topic.1'

I add two tables to that original array of tables "[[routes]]", with method = 'POST' and path = '/my/post/path'. When I do that, the resulting file becomes:

## My POST route comment
[[routes]]
method = 'POST'
path = '/my/post/path'
topic = 'my-topic.0'

## My GET route comment

[routes.rate_limit]
enabled = true

[routes.rate_limit.requests]
per_second = 25

[[routes]]
method = 'GET'
path = '/my/get/path/{key}'
topic = 'my-topic.1'

The comment ends up in the wrong spot. What can I do to make sure it stays on top of my second "[[routes]]" such as:

## My POST route comment
[[routes]]
method = 'POST'
path = '/my/post/path'
topic = 'my-topic.0'

[routes.rate_limit]
enabled = true

[routes.rate_limit.requests]
per_second = 25

## My GET route comment
[[routes]]
method = 'GET'
path = '/my/get/path/{key}'
topic = 'my-topic.1'

This is an extract of the code I use to add my 2 tables:

# result is <class 'tomlkit.items.Table'>
route_ratelimit = tomlkit.table()
route_ratelimit.add("enabled", True)
route_ratelimit_requests = tomlkit.table()
route_ratelimit_requests.add("per_second", 25)
result.append("rate_limit", route_ratelimit)
route_ratelimit.append("requests", route_ratelimit_requests)

Thank you for your assistance.

@frostming
Copy link
Contributor

It's hard for such level of style-preserving. The parser couldn't know what table the comments should adhere to.

@sallner
Copy link

sallner commented Sep 22, 2023

Would it be an option to add the comment inside the [[routes]] sections/entries?

@jcote-tc
Copy link
Author

Would it be an option to add the comment inside the [[routes]] sections/entries?

I am not in control of the original file - but all comments are on top of the sections so it makes it easier to handle that use case. I wrote a python function to handle that scenario. Basically I find the index/location where I need to insert my new Table, check if the line above is a Comment and if so I do -1 to the index, which preserves the location of the commented line:

def search_for_insertion_point_index(parsed_toml):
    key_counter = 0
    stored_keys = []
    for k in parsed_toml.body:
        stored_keys.append(k)
        key_counter += 1
        if isinstance(k[1], tomlkit.items.Table):
            # insert new table before the first found tomlkit.items.Table
            # check if the last line is a comment
            if isinstance(stored_keys[parsed_toml.body.index(k) - 1][1], tomlkit.items.Comment):
                #print("Comment found on the line before. Return index minus 1!")
                return parsed_toml.body.index(k) - 1
            #print(parsed_toml.body.index(k))
            return parsed_toml.body.index(k)
        if isinstance(k[1], tomlkit.items.AoT):
            # insert new table before the first found tomlkit.items.AoT
            # check if the line before is a comment
            if isinstance(stored_keys[parsed_toml.body.index(k) - 1][1], tomlkit.items.Comment):
                #print("Comment found on the line before. Return index minus 1!")
                return parsed_toml.body.index(k) - 1
            #print(parsed_toml.body.index(k))
            return parsed_toml.body.index(k)

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

3 participants