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

How to 'merge' toml files #27

Closed
akkumar opened this issue May 17, 2020 · 2 comments
Closed

How to 'merge' toml files #27

akkumar opened this issue May 17, 2020 · 2 comments

Comments

@akkumar
Copy link

akkumar commented May 17, 2020

I have an application with a default set of options and another set of options to override the default (either the subset or its entirety ) . How do pass 2 .toml in a specific order so I get a final object that represents the merged / overridden values.

Eg:

Eg: default.toml

[db]
readURI = "mysql://user:pass@127.0.0.1/mydb?charset=utf8"
writeURI = "mysql://user:pass@127.0.0.1/mydb?charset=utf8"

[db.options]
ConnMaxLifetime = 1
MaxOpenConns = 10

Eg: prod.toml

[db]
readURI = "mysql://secureuser:securepass@127.0.0.1/mydb?charset=utf8"
writeURI = "mysql://secureuser:securepass@127.0.0.1/mydb?charset=utf8"

So hypothetically an api like -

parser.parse(['default.toml',' prod.toml']

would yield a final toml object as below:

[db]
readURI = "mysql://secureuser:securepass@127.0.0.1/mydb?charset=utf8"
writeURI = "mysql://secureuser:securepass@127.0.0.1/mydb?charset=utf8"

[db.options]
ConnMaxLifetime = 1
MaxOpenConns = 10

Eg: the go implementation takes in an object and 'decodes' a given toml on that object. This helps us to have a simpler override functionality. More details at https://godoc.org/github.com/BurntSushi/toml#Decode .

Is there a similar api / hack available to repeatedly parse and process a bunch of .toml files so we can override default options ?

@iarna
Copy link
Owner

iarna commented Jun 3, 2020

@akkumar So the result of TOML.parse is just a JS object, so the usual approach would be to combine them with JS tools. Exactly what that looks like depends on your needs:

So you might go with the most simple option, with object composition:

const default = TOML.parse(default)
const prod = TOML.parse(prod)
const config = {...default, ...prod}

Or use a module for deep merging: (not an endorsement of this particular module, it's just what came up when searching)

const objectMerge = require('object-merge')
const default = TOML.parse(default)
const prod = TOML.parse(prod)
const config = objectMerge(default , prod)

Both of these methods can merge as many objects together as you like.

@iarna iarna closed this as completed Jun 3, 2020
@iarna
Copy link
Owner

iarna commented Jun 3, 2020

Something that might be cool would be a config library built on top of this library that provides composition of objects like this. Maybe something like rc but built on TOML instead of ini files.

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