Coder Social home page Coder Social logo

fig's Introduction

Fig

You probably don't need another data format, right?

Fig is a very small Scala library which makes it easy to read data out of JSON-based configuration files, with full support for Javascript-style comments. (We're not barbarians here.)

Requirements

  • Scala 2.8.1 or 2.9.0
  • Jerkson 0.2.1

Why JSON?

  • enough structure for most purposes
  • fairly human-readable
  • very well supported
  • easy to use with configuration management systems like Chef or Puppet

How To Use

First, specify Fig as a dependency:

val codaRepo = "Coda Hale's Repository" at "http://repo.codahale.com/"
val fig = "com.codahale" %% "fig" % "1.1.3"

Second, write your config file:

{
    /* Config allows you to use C-style comments.
     * This, for example, is a block comment.
     */
    "parent": { // Comments can terminate a line, too.
        "child": {
            "url": "http://example.com",
            // We don't care about comments inside string literals.
            "splody-string": "An-a one an-a two an-a // /*wah*/ YAY",
            "count": 100,
            "names": ["One", "Two", "Three"],
            "mapped": {
                "1": 1,
                "2": 2,
                "3": 3
            },
            "doubly-mapped": {
                "1": [1, 2, 3],
                "2": [2, 3, 4],
                "3": [3, 4, 5]
            }
        }
    }
}

Third, load your config file:

import com.codahale.fig.Configuration

val config = new Configuration("myapp/config.json")

You can also create a new Configuration instance from a Source or an InputStream.

Fourth, read values from it:

// Load a value directly.
val port = config("http.port").as[Int]

// Load a value as an option.
val uri = config("http.uri").asOption[Int]

// Load a value or a default value.
var server = config("http.server").or("Fig-Powered")

// Load a list of values.
val numbers = config("http.numbers").asList[Int]

// Or a map of values (key are required to be strings)
val properties = config("http.resource").asMap[String]

// Hell, even load a case class.
case class ResourceConfig(name: String, uri: String)
val resource = config("http.resource").as[ResourceConfig]

// Defy type erasure for lists!
val numbers = config("http.more-numbers").asMap[List[Int]]

License

Copyright (c) 2010 Coda Hale

with contributions from

  • Bryan J Swift

Published under The MIT License, see LICENSE

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.