Code Monkey home page Code Monkey logo

typesafe-config's Introduction

Build Status

typesafe-config

The main goal of this project is to explore basic features of com.typesafe:config.

Reference: https://github.com/lightbend/config#overview

project description

com.typesafe:config is a type-safe configuration library for JVM languages.
All we need to do to start using com.typesafe:config is:

  • add dependency
<dependency>
    <groupId>com.typesafe</groupId>
    <artifactId>config</artifactId>
    <version>1.3.3</version>
</dependency>
  • add application.conf to resources
  • get Config class
Config conf = ConfigFactory.load();

project content

  • test/resources/application.conf - file with most popular use-cases. It's written in HOCON.
  • test/java/ConfigTest - we test if all lines from application.conf file was loaded successfully.
  • test/java/Author - util class for mapping specific entry in application.conf directly to the JavaBean.
  • test/java/UserStatus - util class for mapping specific entry in application.conf directly to the Enum.

use cases

  • Assumptions:
    • Config conf = ConfigFactory.load();
    • application.conf has structure:
      predefined {
          ...
      }
      
      conf {
          ...
      }
      
  • Basic example
    conf {
        project_name = typesafe-config
    }
    
    conf.getString("conf.project_name") // typesafe-config
    
  • Substitutions (way of eliminating copy-paste)
    predefined {
        version : 1.0-SNAPSHOT
    }
    conf {
        project_version : ${predefined.version}
        artifact_version : ${predefined.version}
    }
    
    conf.getString("conf.project_version") // 1.0-SNAPSHOT
    conf.getString("conf.artifact_version") // 1.0-SNAPSHOT
    
    Note:
    Remember if there is no defined yyy then xxx : ${yyy} will cause
    ExceptionInInitializerError: Could not resolve substitution to a value: ${yyy}
    
  • Handling JSON objects
    author : {name : michal, surname : tumilowicz}
    
    ConfigObject author = conf.getObject("conf.author");
    Config asConfig = author.toConfig(); // treat it as a Config
    asConfig.getString("name"); // michal
    asConfig.getString("surname"); // tumilowicz
    
  • Mapping JSON to JavaBean
    If you have a Java object that follows JavaBean conventions (zero-args constructor, getters and setters), you can automatically initialize it from a Config.
    author : {name : michal, surname : tumilowicz}
    
    Author author = ConfigBeanFactory.create(conf.getObject("conf.author").toConfig(), Author.class);
    author.getName(); // michal
    author.getSurname(); // tumilowicz
    
  • Merging
    • Values on the same line are concatenated (for strings and arrays) or merged (for objects).
    • If you duplicate a field with an object value, then the objects are merged with last-one-wins.
    persistence : {specification: JPA, provider : Hibernate, cache : true}
    persistence : {provider : EclipseLink, cache : false, database : Oracle}
    
    is merged to:
    persistence : {specification: JPA, provider : EclipseLink, cache : false, database : Oracle}
    
  • Merging + substitution
    Merging is especially useful with substitutions.
    predefined {
        headquarters : {name : "mtumilowicz holding"}
    }
    conf {
        branch_east : ${predefined.headquarters} {branch_name : east}
    }
    
    is merged to:
    headquarters : {name : "mtumilowicz holding", branch_name : east}
    
  • Substitutions with default value
    predefined {
    }
    conf {
        web_container : GlassFish
        web_container : ${?predefined.web_container}
    }
    
    Here, the field web_container : ${?predefined.web_container} simply vanishes if there's no value for predefined.web_container. But if you set predefined.web_container - it would be used.
    conf.getString("conf.web_container") // GlassFish
    
    Contrary:
    predefined {
        login: admin
    ]
    conf {
        login : ${?predefined.login}
    }
    
    conf.getString("conf.login") // admin
    
  • Lists
    languages : [english, polish, french]
    
    conf.getStringList("conf.languages") // [english, polish, french]
    
  • Enums
    user_status : [PENDING, ACTIVE, INACTIVE, DELETED]
    
    public enum UserStatus {
        PENDING, ACTIVE, INACTIVE, DELETED
    }
    
    conf.getEnumList(UserStatus.class, "conf.user_status") // [PENDING, ACTIVE, INACTIVE, DELETED]
    

typesafe-config's People

Contributors

mtumilowicz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.