gestalt icon indicating copy to clipboard operation
gestalt copied to clipboard

Reference other branches for defaults?

Open credmond-git opened this issue 2 years ago • 5 comments

I have A TOML-File like this

[main] bla = 1

[parts]

[parts.partA] hello = "World" foo = "Bar"

[parts.partB] hello = "Gestalt" extends = "partA"

Now I'd like to parse this file into a hierarchy of java records.

Problem for my specific usecase: As shown in partB above, some parts may depend on other parts oder a default-section. Is there a way to create some kind of hook, which would jump intop the decoding process and make PartB have all the other values from PartB? (so that partB has after that hello=Gestalt, foo=Bar, extends=partA) my initial hope after reading the mentioning of sub configurations in the documentation, it would be possible to get a branch from the config, check for any extends/defaults, get the extended branch and set it as default/fallback for the first branch (like in gestalt-builder for fallback-files, but maybe even recursive).

Any thoughts? Thanks.

credmond-git avatar Feb 15 '24 17:02 credmond-git

@brainbytes42 i have moved the second part of your bug here.

credmond-git avatar Feb 16 '24 03:02 credmond-git

It isnt exactly as described , but have you looked into https://github.com/lightbend/config?tab=readme-ov-file#uses-of-substitutions.

credmond-git avatar Feb 16 '24 03:02 credmond-git

This is the goal of the post processors. They allow you to modify the config tree in any way. The only current post processor TransformerPostProcessor only deals with leaf values and allows string substitution. But in theory the post processor should allow any modification of the tree including inserting one branch into another. But it hasn't been tested with anything other than changing leaf values.

I can investigate adding a new post processor to include this functionality.

I was thinking of something more like

[main]
bla = 1

[parts]

[parts.partA]
hello = "World"
foo = "Bar"

[parts.partB]
hello = "Gestalt"
${insert} = "parts.partA"

So all the values from parts.partA would be inserted into parts.partB. But parts.partB would take priority, so if there was duplicates the values in parts.partB would take precedence.

credmond-git avatar Feb 16 '24 06:02 credmond-git

It isnt exactly as described , but have you looked into https://github.com/lightbend/config?tab=readme-ov-file#uses-of-substitutions.

AFAIK this isn't possible in TOML, isn't it? The substitutions are for HOCON-Format... Maybe I'll have to have a look into that again, but I Like the TOML-Syntax with it's tables...

This is the goal of the post processors. They allow you to modify the config tree in any way. The only current post processor TransformerPostProcessor only deals with leaf values and allows string substitution. But in theory the post processor should allow any modification of the tree including inserting one branch into another. But it hasn't been tested with anything other than changing leaf values.

I'll have a look into that, thanks for the hint.

I can investigate adding a new post processor to include this functionality.

This would be incredible. :-)

I was thinking of something more like

[main]
bla = 1

[parts]

[parts.partA]
hello = "World"
foo = "Bar"

[parts.partB]
hello = "Gestalt"
${insert} = "parts.partA"

So all the values from parts.partA would be inserted into parts.partB. But parts.partB would take priority, so if there was duplicates the values in parts.partB would take precedence.

For the HOCON-Syntax, the insert would be an object inheritance, given by this example in the docs:

data-center-generic = { cluster-size = 6 }
data-center-east = ${data-center-generic}
data-center-east = { name = "east" }
data-center-west = ${data-center-generic}
data-center-west = { name = "west", cluster-size = 8 }

brainbytes42 avatar Feb 19 '24 10:02 brainbytes42

Hi, I've chosen to migrate my config-format to HOCON, which provides the substitution/extension-mechanism. Thanks for the hint - I thought about that before, but had started with TOML... ;-)

So now I have something like this

parts = {
    partA = {
        hello = "World"
        foo = "Bar"
    }
    partB = ${parts.partA} {
        hello = "Gestalt"
    }
}

and with your library, I can parse this to records - nice. :-)

brainbytes42 avatar Feb 20 '24 08:02 brainbytes42

It is probably too late to help. But I have added a feature to allow node substitutions. https://github.com/gestalt-config/gestalt?tab=readme-ov-file#node-substitution-include-nodes If you are able to give it a try let me know if it works for you. This should work across all file types, not just HOCON.

credmond-git avatar Aug 06 '24 18:08 credmond-git