Skip to content

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

Description

@jcote-tc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions