Coder Social home page Coder Social logo

konfy's Introduction

Konfy

Konfy is a statically typed and easy to use configuration library for Kotlin.

The main idea of Konfy is chains and views - you are creating a chain of configs to retrieve parameters from different storage through one interface; after it you can create view - a typeful interface to config parameters.

Konfy supports plenty of formats with corresponding providers:

  • Environment variables (out of the box) - support of environment variables
  • System properties (out of the box) - support of system properties
  • JNDI resources (out of the box) - support of JNDI resources in different contexts
  • Property files (out of the box) - support of property files
  • TOML (konfy-toml) - support of TOML
  • SSM (konfy-ssm) - support of AWS Simple System Manager parameters
  • KeePass (konfy-keepass) - support of parameters stored in kdbx encrypted files
  • Kara config (konfy-kara) - support of properties config with includes (Kara framework config)

Setup

To set it up, add a library to compile dependencies:

repositories {
    maven(url = "https://packages.jetbrains.team/maven/p/konfy/maven")
}

dependencies {
    compile("tanvd.konfy", "konfy", "0.1.23")
    //Other necessary providers
}

What's inside

Providers

First, you will need to create ConfigProvider object.

ConfigProvider can be asked directly for a config parameter via get call.

//Instantiation of EnvVarProvider with implicit default conversion service
val envVar = EnvVarProvider()
//Get value from a provider with explicit default value
val value = envVar.get("value", default = 5)

Conversions

Almost all providers (excluding JNDI) include a parameter in a constructor for a conversion function. This function will be used to convert values from string representation to specific type.

Conversion is needed only if a specific configuration format does not support such type of parameters, and it should be deserialized from text representation.

Konfy assumes the following formats of values:

  • Boolean: true, false
  • Integer: 1
  • Double: 1.0
  • String: "string"
  • Array: [1, 2], ["1", "2"]

Chaining

Few providers can be linked to a chain. Resolution of parameter will stop once it's found in a provider inside a chain.

val chain = ConfigChain(EnvVarProvider, KeepassProvider)

Views

Now you can create a view to a chain - it will provide you with a typeful interface to a configuration.

object Config: ConfigView() {
    /** Delegates to a `key` parameter in a config. No default. Will be cached. */
    
    val key: String by provided()
    /** Delegates to an `other-key` parameter in a config. Default is 0. Will be cached.  */
    val otherKey: Int by provided("other-key", 0)
    
    /** Delegates to a `lastKey` parameter in a config. No default. Will not be cached. */
    val lastKey: Int by provided("other-key", cached = false)
}

View can cache parameters and provide defaults for them.

GlobalKonfy

GlobalKonfy object is basically a global configuration interface. You may initialize it on the start of an application with a configuration chain and then use it everywhere to get configuration parameters.

Moreover, ConfigView by default will take its ConfigProvider from GlobalKonfy

Few advice

One of the interesting usages for Konfy is to split your configs into a secret and not-very-secret and put secret config into KeePass DB (kdbx) and not-very-secret into simple config file - for example, TOML. Chain will merge configs together and View will provide one typeful interface to all parameters.

konfy's People

Contributors

denlink avatar ferusm avatar sndl avatar tanvd avatar tapac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ferusm

konfy's Issues

Support HOCON

We need to support the reading via HOCON files. Likely we will need to introduce konfy-hocon module

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.