Giter Club home page Giter Club logo

sq-conf's Introduction

Sq-Conf - unified interface for all your configuration needs

Build Status Coverage Status Maven

Description

SqConf is unified interface for all your configuration needs. Essentially it's a thin wrapper for typesafe-config library ("com.typesafe.config" % "1.3.3") that was mainly created to make configuring applications running on containers easier. Mainly overriding values from environment variables is made easier, no need to specifically define this in config. Also key does not need to exist in the config in the first place.

    // reads application.conf
    val sqConf = new SqConf()

    val sqConf = SqConf.default()

    val sqConf = SqConf.forFilename("other.conf") // reads other.conf from classpath instead.

    val sqConf = SqConf.forConfig(typeSafeConfig) // wrap existing instance of typesafe config.

    val sqConf = SqConf.forFile(configAsFile) // java.util.File

Value overrides example

Values in the config can be programatically overriden. This is convenient especially when testing.

    val overrideMap = Map("some.testIntValue" -> "15", "some.testStringValue" -> "overridenstring")

    val sqConf: SqConf = SqConf.default.withOverrides(overrides = overrideMap)

    sqConf.getInt("some.testIntValue") // returns 15

Using environment variables

Environment variables are preferred over all other options and work for keys that are not available in the original config file.

    export SOME_TESTINTVALUE="150"

    val sqConf: SqConf = SqConf.default()

    sqConf.getInt("some.testIntValue") // returns 150

Other formats

SqConf supports all the formats that the underlying typesafe config also supports.

Java Wrapper

SqConf also provides a java wrapper that returns java data types.

    val javaWrapper = new SqConf().asJava()

    javaWrapper.getInt("some.testIntValue") // returns java.lang.Integer

    javaWrapper.getString("some.testStringValue") // returns java.lang.String

Converting to Properties class

Both native scala sqConf and the javaWrapper now have a convenience method to convert to Properties object.

    val asProperties: Properties = new SqConf().toProperties()

    val someIntVal = asProperties.getProperty("some.testIntValue").toInt // properties always returns string

    val someStringVal = asProperties.getProperty("some.testStringValue")

Configure order of preference

Order of preference defines in which order configuration keys are read. By default the order goes:

  1. valueOverrides given during creation of config
  2. environment variables
  3. config file Order can be configured freely, for example:
  val orderOfPreference: List[OrderOfPreference.Value] = List(
    OrderOfPreference.CONF_FIlE,
    OrderOfPreference.ENV_VARIABLE,
    OrderOfPreference.VALUE_OVERRIDES)

  val conf = new SqConf().configureOrder(oop)

Now order would be reversed in comparison to the default.

Getting started

Sq-conf is cross compiled for scala 2.11, 2.12 and 2.13. For the latest of the latest, sq-conf snapshots are available in sonatype repo: sonatype-snapshots. Add sqConf to your project, with sbt add this simple line:

    libraryDependencies +=  "io.sqooba" %% "sq-conf" % "0.5.2"

Sq-conf works also with maven. Just add this to your pom.xml:

    <dependency>
      <groupId>io.sqooba</groupId>
      <artifactId>sq-conf_2.12</artifactId>
      <version>0.5.2</version>
    </dependency>

Future Plans

  • improve api documentation from nonexistent to something
  • yml support
  • test more complex type conversions from app.conf, also with java transformers

Change History

  • 0.5.3 getAvailableKeys redefined and also available for a key, getAsOption
  • 0.5.2 Scala 2.13 support
  • 0.5.1 Allow configuring order of preference, bug fix value overrides not being passed on to sub confix, scala 2.13 support
  • 0.5.0 Add get config method to java wrapper, deployed first snapshot to sonatype repo.
  • 0.4.2 New toProperties convenience method, transformer interface for java to transform from string to any type.
  • 0.4.1 Remove dependency to option for the generic getter.
  • 0.4.0 Dependency version upgrades, better documentation.
  • 0.3.6 Remove options from constructors so java wrapper works without scala standard lib.

sq-conf's People

Contributors

dependabot[bot] avatar killerwhile avatar pietrotull avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

pietrotull

sq-conf's Issues

getDuration() is not available

I notice that getDuration() method is not available. It is defined in com.typesafe.config though.
Is there any particular reason not to have it ?

Convert SqConf to java.util.Properties

It would be useful to a have a method to convert an JavaSqConf instance to a java.util.Properties object for interop with other client libraries (e.g Kafka clients)

Something in the vein of:

 public static Properties toProperties(JavaSqConf config) {

    Properties properties = new Properties();
    config.getSqConf()
        .conf()
        .entrySet()
        .forEach(e -> properties.setProperty(e.getKey(), config.getString(e.getKey())));
    return properties;
  }

Overrides are not propagated after getConfig()

With the following application.conf:

proxy {
  enabled: false
}

The first assert passes in the following code, but the second fails:

Map<String, String> properties = new HashMap<>();
properties.put("proxy.enabled", "true");
JavaSqConf sqConf = new SqConf().asJava().withOverrides(properties);

// works
assertTrue(sqConf.getBoolean("proxy.enabled"));

// fails but would be expected to pass
assertTrue(sqConf.getConfig("proxy").getBoolean("enabled"));

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.