Coder Social home page Coder Social logo

kotlin-baker's Introduction

Maven Central Build Status

Tl;DR

Create Kotlin data classes in a highly customizable way from Spring Cloud Config.

Description

Spring Cloud Config is a great way to manage application configuration. Kotlin is one of the best JVM languages, but there is a gap between that two - even though Spring provides @ConfiugrationProperties, they don't really work with data classes.

Another concern is that it's hard to achieve hierarchical configuration with collection/map properties.

Kotlin Baker library facilitates data classes creation with highly customizable setup and support for hierarchical configuration.

Example

Consider that we have the following domain:

data class Tasks(val tasks: Map<String, TaskConfig>)

data class TaskConfig(val id: String, val properties: Map<String, Any>)

We can create configs hierarchy for it:

Now we can configure our domain class in Spring using Kotlin Baker:

@Bean
open fun tasks(creator: KotlinCreator, context: Context): Tasks {
    return creator.create("", Tasks::class.createType(), context)
}

@Bean
open fun creator(environment: Environment): KotlinCreator {
    return CacheAwareCreator()
}

@Bean
open fun creatorContext(environment: Environment): Context {
    val allKeys = getAllPropertyKeys(environment)
    return Context.builder {
        environment[it]
    }.withMapKeyStrategy { baseName, _ ->
        val prefix = "$baseName."
        allKeys.mapNotNull { key ->
            if (key.startsWith(prefix)) {
                val i = key.indexOf(".", prefix.length)
                if (i < 0) {
                    key.substring(prefix.length)
                } else {
                    key.substring(prefix.length, i)
                }
            } else {
                null
            }
        }.toSet()
    }.build()
}

When Spring context with profiles production,APAC is initialized, we can see that

  • two tasks are picked up from the configuration in runtime
  • APAC profile config values are preferred to common production values and common values
  • common production values are preferred to common values

Note: the last declared profile has higher precedence, i.e. if we want 'APAC' values to override 'common production' values, we need to define profiles like 'production,APAC'. Feel free to read more about that in Spring documentation

The example above can be found here:

  1. Launch ServerApplication
  2. Launch ClientApplication
  3. Check that client application reports the following domain class setup:
Found the following tasks: Tasks(tasks={task2=TaskConfig(id=task2, properties={prop2=apacTask2Prop2, prop1=commonTask2Prop1}), task1=TaskConfig(id=task1, properties={prop2=productionTask1Prop2, prop1=commonTask1Prop1})})

Releases

Release Notes

How to Contribute

kotlin-baker's People

Contributors

denis-zhdanov avatar abogushevsky avatar

Watchers

James Cloos 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.