Skip to content

jupf/staticonf

Repository files navigation

StatiConf

Kotlin license Download Dependency Status
This is a statically typed library for handling configuration files through properties in Kotlin.
With StatiConf you can back a Kotlin property by a configuration file without taking care of any string parsing or stream handling.

Table of Contents

Getting Started

The source/target compatibility is Java 1.5.
The Kotlin Reflect API is needed to be present for this library.
This library is uploaded to jCenter and Maven Central.
Here is also an example to look at.

Gradle

dependencies {
    compile 'io.github.jupf.staticonf:staticonf:1.0.0'
}

Maven

<dependency>
  <groupId>io.github.jupf.staticonf</groupId>
  <artifactId>staticonf</artifactId>
  <version>1.0.0</version>
  <type>pom</type>
</dependency>

Config File Syntax

This is the standard syntax for a StatiConf file. But it can be changed through the StatiConf class constructor.

# this is a UUID hex string
globalID = 38400000-8cf0-11bd-b23e-10b96e4ef00d

# local identifier
localID = 31337

# ports to connect to (list)
ports = 5000 ;  5001 ;  5005

# decides if the service discovery is enabled
serviceDiscovery = true

Loading from a Configuration File

To load a configuration file, first you need to construct a StatiConf object from it:

val statiConf = StatiConf("config.file")

Then you can delegate properties to it:

val ports: List<Short> by statiConf
val localID: Int by statiConf

The properties have the values parsed from the config entry with the same name as the property.
It is possible to use the Kotlin.collections.List. The standard separator in the config file is a ;

Saving to a Configuration File

To save to a configuration file, you can just use non-final properties:

val statiConf = StatiConf("config.file")
var localID: Int by statiConf

...

localID = 2048 // This will change the line in the config file to the set value.

If the property does not exist in the config file a new line will be appended to the end with the set value.

Types in StatiConf

Out of the box, the following property types can be used for automatic parsing:

  • All Kotlin basic types (e.g. Int, Float, Boolean, ...)
  • java.util.UUID

Adding new types

You can easily add new types to the parser. You just need a function that parses a string to the desired object:

val statiConf = StatiConf("config.file")
statiConf.addType("java.util.UUID", { uuid -> UUID.fromString(uuid) })

If you save such a type to a property file the toString method is called to parse it into a string.
With other words, your parsing function has to play nicely with the toString representation of the type!

About

Statically typed library for handling configuration files through properties in Kotlin

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages