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 TOML support for config files #741

Merged
merged 12 commits into from
May 28, 2024
Merged

feat: Add TOML support for config files #741

merged 12 commits into from
May 28, 2024

Conversation

fmartingr
Copy link
Contributor

Summary

This pull request adds TOML support for config files.

The ReadFromJSON function has been refactored into a unified ReadFrom with the same signature that determines the filetype by the extension the file has, if the extension is .toml a TOML decoder is used, following with JSON by default.

Code chagues

  • Created a Decoder interface that we can use to simplify the unmarshalling logic in the code (try to decode, search for fallback, etc). Now we just pass the Decoder (or DecoderFactory) around and the rest of the logic is the same for all decoders.
  • Implemented JSONDecoder and TOMLDecoder (from json.Decoder and toml.Decoder) since their signatures are different and also to allow TOML to show up better errors on decoding failures.
  • Main point is the "new" ReadFrom method that should take care of everything.
  • It defaults to JSON so all regular usage should keep working as usual

Potential improvements

I focused on the Decoding part only, but if we plan to use it also as a encoder (to generate config files) we need to improve the data structure, interfaces and names around. It shouldn't be much but something to take into consideration.

I like to have "Codec" interfaces that take care of encoding/decoding around and create implementations for whatever thing I want to support, to I would go in that direction. Maybe in another PR if this one gets in.

Why

TOML files are way more readable for the end user, and we can put comments in the file for explanation of fields, better sort keys, etc.

Even better could be using the TOML's library capabilites to add comments on TOML files. I potentially see this in a way that:

  • The data structure is defined with all necessary fields and comments
  • Sample files are rendered directly from the structs
    • Creating the file automatically means we would lose manual organization, like in the deployer.sample.toml file I commited to this branch where I grouped settings. But that can be solved by nesting structs by topic.
  • A CI step (lint) can create sample files from the configuration structs and check if the ones match with the code, ensuring that they are always up to date. This would lead to the data structures being the source of truth for the configuration while providing documentation right in the TOML files.

Test run

Performed a trial deployment with config and deployer TOML files. Didn't test all commands manually.

Ticket Link

--

@fmartingr fmartingr changed the title Add TOML support for config files feat: Add TOML support for config files May 10, 2024
Copy link
Member

@agnivade agnivade left a comment

Choose a reason for hiding this comment

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

Neat work. Left some comments.

.gitignore Show resolved Hide resolved
defaults/json.go Outdated Show resolved Hide resolved
coordinator/config.go Outdated Show resolved Hide resolved
defaults/main.go Outdated Show resolved Hide resolved
Copy link
Member

@agarciamontoro agarciamontoro left a comment

Choose a reason for hiding this comment

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

This looks great! Thank you so much for putting the effort into implementing this <3 Just a couple of nits before approving.

config/deployer.sample.toml Show resolved Hide resolved
defaults/main.go Outdated Show resolved Hide resolved
defaults/main.go Outdated Show resolved Hide resolved
fmartingr and others added 2 commits May 27, 2024 13:45
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
Copy link
Member

@agarciamontoro agarciamontoro left a comment

Choose a reason for hiding this comment

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

Looks great, thank you!

@agarciamontoro agarciamontoro added the 4: Reviews Complete All reviewers have approved the pull request label May 27, 2024
@fmartingr fmartingr merged commit f16bc39 into master May 28, 2024
1 check passed
@fmartingr fmartingr deleted the toml-support branch May 28, 2024 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4: Reviews Complete All reviewers have approved the pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants