config-annotation


License

License

Categories

Categories

config Application Layer Libs Configuration
GroupId

GroupId

com.wacai
ArtifactId

ArtifactId

config-annotation_2.12
Last Version

Last Version

0.3.7
Release Date

Release Date

Type

Type

jar
Description

Description

config-annotation
config-annotation
Project URL

Project URL

https://github.com/zhongl/config-annotation
Project Organization

Project Organization

com.wacai
Source Code Management

Source Code Management

https://github.com/zhongl/config-annotation

Download config-annotation_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.wacai/config-annotation_2.12/ -->
<dependency>
    <groupId>com.wacai</groupId>
    <artifactId>config-annotation_2.12</artifactId>
    <version>0.3.7</version>
</dependency>
// https://jarcasting.com/artifacts/com.wacai/config-annotation_2.12/
implementation 'com.wacai:config-annotation_2.12:0.3.7'
// https://jarcasting.com/artifacts/com.wacai/config-annotation_2.12/
implementation ("com.wacai:config-annotation_2.12:0.3.7")
'com.wacai:config-annotation_2.12:jar:0.3.7'
<dependency org="com.wacai" name="config-annotation_2.12" rev="0.3.7">
  <artifact name="config-annotation_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.wacai', module='config-annotation_2.12', version='0.3.7')
)
libraryDependencies += "com.wacai" % "config-annotation_2.12" % "0.3.7"
[com.wacai/config-annotation_2.12 "0.3.7"]

Dependencies

compile (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.0
org.scala-lang : scala-reflect jar 2.12.0
com.typesafe : config jar 1.2.1

test (1)

Group / Artifact Type Version
org.scalatest : scalatest_2.12 jar 3.0.1

Project Modules

There are no modules declared in this project.

CI Publish Maven Central Coverage Status Codacy Badge

A refactor-friendly configuration lib would help scala programmers to maintain config files without any pain, by using scala macro annotation.

Usage

Create a config-style trait as configuration definition, eg:

import com.wacai.config.annotation._
import scala.concurrent.duration._

@conf trait kafka {
  val server = new {
    val host = "wacai.com"
    val port = 12306
  }

  val socket = new {
    val timeout = 3 seconds
    val buffer = 1024 * 64L
  }

  val client = "wacai"
}

Use config by extending it,

class Consumer extends kafka {
  val client = new SimpleConsumer(
    server.host,
    server.port,
    socket.timeout,
    socket.buffer,
    client
  )

  ...
}

Compile, @conf will let scala compiler to insert codes to kafka.scala:

trait kafka {
  val server = new {
    val host = config.getString("kafka.server.host")
    val port = config.getInt("kafka.server.port")
  }
  val socket = new {
    val timeout = Duration(config.getDuration("kafka.socket.timeout", SECONDS))
    val buffer = config.getBytes("kafka.socket.buffer")
  }
  val client = config.getString("kafka.client")

  ...
}

After that, a config file named kafka.conf was generated at src/main/resources as blow:

kafka {
  server {
    host = wacai.com
    port = 12306
  }

  socket {
    timeout = 3s
    buffer = 64K
  }

  client = wacai
}

Last but not least, a application.conf need to be created to include kafka.conf like:

include "kafka.conf"

Installation

Caution: only support scala 2.11.0+

Set up your build.sbt with:

Scala 2.11

addCompilerPlugin("org.scalamacros" % "paradise" % "2.0.1" cross CrossVersion.full)

libraryDependencies += "com.wacai" %% "config-annotation" % "0.3.5"

Scala 2.12

addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

libraryDependencies += "com.wacai" %% "config-annotation" % "0.3.6"

Scala 2.13

libraryDependencies += "com.wacai" %% "config-annotation" % "0.4.0"

Type covenant

Scala type Config getter Value
Boolean getBoolean true/false
Int getInt number
Double getDouble float
String getString text
Long getBytes number with unit (B, K, M, G)
+Duration getDuration number with unit (ns, us, ms, s, m, h, d)

Integrate with akka actor

import com.wacai.config.annotation._

@conf trait kafka extends Configurable { self: Actor =>
  def config = context.system.settings.config

  ...
}

Change default generation directory

Config files would be generated at src/main/resources as default.

It can be changed by appending macro setting to scalacOption in build.sbt:

scalacOptions += "-Xmacro-settings:conf.output.dir=/path/to/out"

A runnable example

Please see config-annotation-example.

Versions

Version
0.3.7
0.3.6