Giter VIP home page Giter VIP logo

config-annotation's Introduction

Config annotation

Build 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 project/build.properties to:

sbt.version = 0.13.5

sbt 0.13.6+ has NPE problem while compiling

Set up your build.sbt with:

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

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

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.

config-annotation's People

Contributors

eagoo avatar lustefaniak avatar orrsella avatar zhongl avatar

Watchers

 avatar  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.