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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve original formatting when serializing #26

Open
henrikje opened this issue Feb 28, 2023 · 2 comments
Open

Preserve original formatting when serializing #26

henrikje opened this issue Feb 28, 2023 · 2 comments

Comments

@henrikje
Copy link

henrikje commented Feb 28, 2023

Hi Anton! 馃憢馃檪

In my current use case, I have a user-edited toml file where I sometimes want to update a specific value programmatically. In this scenario, I would want to update the file without losing any comments or custom white space added by the user (except when directly affected by the change).

In the the most general form, TomlValue.from(path).write(path) would be a "no-op". If the TomlValue is modified between reading and writing as little formatting should change as possible. As a more special case, I could imagine a special operation which writes a new value to a single specified key without decoding the full document. But that may not be in line with the current API.

I understand that this may be a fundamental change and simply out of scope, but it doesn't hurt to ask, right? 馃榿

@valderman
Copy link
Owner

I think this would be doable for 90% of cases without too much work. The last 10% would be significantly harder. Consider the following examples:

[foo]
bar = 123
foo = {bar = 123}
foo.bar = 123

All of these map to {"foo": {"bar": 123}} in JSON notation. While the two first cases are easy enough to handle - just annotate foo with the type of dictionary it corresponds to - the third one is a problem. Handling cases like this would require replacing the current simple pretty-printing algorithm with some slow and convoluted hackery, and the annotations to support it would be pretty invasive to boot.

Would a best effort solution, which does the right thing as long as you only use the first two forms but converts the third form into the first during a roundtrip, still be useful?

@henrikje
Copy link
Author

henrikje commented Mar 9, 2023

In my case, I have an end-user editable configuration file which may include custom formatting and comments. The file is read in full and decoded to data classes on startup. In some cases I want to persist changes to the configuration. Typicaly, I have one or a few specific value(s) that I want to change programmatically. The rest of the file should not be changed (i.e. preserving custom white space and comments).

The value (X) I want to change may be at different levels, such as:

a = "X"
[a]
b.c = "X"
[a]
b = [ "X", "y", "z" ]
[a]
b = [ { c = "X", d = "y" }, { e = "z" } ]

As hinted at earlier, if it is simpler, for my use case it would be fine to say something like:

tomlMapper.encodeInline(tomlFilePath, "a.b.c", newValue)

In this case, the full file would not be decoded. Instead, it would be parsed only enough to identify the "bounds" of the existing value for that key in the file, and then replace that part of the file with the new value.

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