Skip to content

Instantly share code, notes, and snippets.

@m0mus
Created Dec 9, 2021
Embed
What would you like to do?
Config tree vs flat
Config config = ConfigProvider.getConfig();

Tree structure

  • Config represented as a tree
  • Every tree node is a Config instance
  • Natural mapping for tree structured config sources such as yaml and json
  • Single and transparent SPI for converters

Flat structure

  • Config represented as a map
  • Natural mapping for tree structured config sources such as properties files
  • No single and transparent SPI for converters. Can only convert strings.
// Getting a node
// ---------------------------------------

// Tree
Config configNode = config.get("node.subnode");
Config configNode = config.get("node").get("subnode");

// Flat
// You cannot get a node in flat structure, you only can get a node value


// Getting a node value as string
// ---------------------------------------

// Tree
String str = config.get("node").asString();
String str = config.getValue("node", String.class); // Can be added for compatibility with MP Config

// Flat
String str = config.getValue("node", String.class);


// Getting a node value as an Object using converter
// ---------------------------------------

// Tree
DataSource dataSource = config.get("node").as(DataSource.class);

// Flat

// Not clear semantics
// - First parameter is a prefix or a key. 
// - The result can be produced using a converter or using some internal mechanism (another SPI, etc.)
DataSource dataSource = config.get("node", DataSource.class); 


// Get a list of subnodes
// ---------------------------------------

// Tree
List<Config> nodes = config.get("node").asList();

// Flat
// It's possible to do with some conplications related to the flat nature of configuration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment