Skip to content

dzikoysk/cdn

Repository files navigation

CDN CDN CI Reposilite Maven Central codecov CodeFactor

Simple and fast configuration library for JVM based apps, powered by CDN (Configuration Data Notation) format, based on enhanced JSON for Humans standard. Handles CDN, JSON and YAML-like configurations with built-in support for comments and automatic scheme updates.

Overview

  • Supports Java, Kotlin (dedicated extension) and Groovy
  • Automatically updates configuration structure and migrates user's values
  • Lightweight ~ 70kB
  • Respects properties order and comment entries
  • Bidirectional parse and render of CDN sources
  • Serialization and deserialization of Java entities
  • Indentation based configuration (YAML-like)
  • Compatibility with JSON format
  • Null-safe querying API

Table of Contents

Installation

Gradle

repositories {
    maven { url 'https://repo.panda-lang.org/releases' }
}

dependencies {
    // Default
    implementation 'net.dzikoysk:cdn:1.14.4'
    // Kotlin wrapper
    implementation 'net.dzikoysk:cdn-kt:1.14.4'
}

Manual

You can find all available versions in the repository:

Using the library

TLDR

A brief summary of how to use the library.

public final class AwesomeConfig {

    @Description("# Comment")
    public String property = "default value";

}

Handling:

Cdn cdn = CdnFactory.createStandard();
File configurationFile = new File("./config.cdn");

// Load configuration
AwesomeConfig configuration = cdn.load(Source.of(configurationFile), AwesomeConfig.class).orThrow(identity());

// Modify configuration
configuration.property = "modified value";

// Save configuration
cdn.render(configuration).orThrow(identity());

To explore all features, take a look at other chapters.

Load configuration

By default, CDN is meant to use class-based configuration. It means that configurations are accessed through the standard Java instance. Let's say we'd like to maintain this configuration:

hostname: 'localhost'

At first, every configuration is loaded into the Configuration object.

Configuration configuration = cdn.load("hostname: localhost")
String hostname = configuration.getString("hostname", "default value, if the requested one was not found")

To avoid such a cringe configuration handling, we can just map this configuration into the Java object. Let's declare the scheme:

public final class Config {
    
    public String hostname = "default value";
    
}

The last thing to do is to provide this class during the load process:

Config config = cdn.load("hostname: localhost", Config.class)
config.hostname // returns 'localhost'

Update properties

Configurations can be updated in both variants. As you can see in the previous chapter, we've declared hostname field as non-final. It means we can just update it's value and CDN will update this field during next render.

config.hostname = "new value";

For configuration without scheme, we can use setString method:

configuration.setString("hostname", "new value");

Save configuration

CDN can render configuration elements and entities using render methods. The output depends on the installed format.

Supported formats

Various formats ae supported through the Feature api. Available format features:

  • DefaultFeature (CDN)
  • JsonFeature
  • YamlFeature

Output comparison:

Standard JSON feature YAML-like feature
# entry comment
key: value
# section description
section {
  list [
    1st element
    2nd element
  ]
}
   
"key": "value",
"section": {
 "list": [
  "1st element",
  "2nd element"
 ]
}
   
# entry comment
key: value 
# section description
section:
  list:
    - 1st element
    - 2nd element
   

Who's using