Jsonnex Logo

An Elixir wrapper for the Golang Jsonnet compiler.

Hex.pmGitHub Workflow Status (master)Coveralls master branchSupport Jsonnex


Contents

Installation

Add Jsonnex to your list of dependencies in mix.exs:

def deps do
  [
    {:jsonnex, "~> 0.2.0"}
  ]
end

Introduction

Jsonnet is a configuration language that compiles to JSON. While JSON only has support for arrays, objects and primitives, Jsonnet has support for those as well as variables, conditionals, arithmetic, functions, imports and error propagation.

For example, the following Jsonnet code:

{
  person1: {
    name: "Alice",
    welcome: "Hello " + self.name + "!",
  },
  person2: self.person1 { name: "Bob" },
}

Would compile to the following JSON:

{
  "person1": {
    "name": "Alice",
    "welcome": "Hello Alice!"
  },
  "person2": {
    "name": "Bob",
    "welcome": "Hello Bob!"
  }
}

For more information and examples about the Jsonnet language, be sure to check out the official documentation.

Quick Start

After you have installed the :jsonnex dependency, you can use the library in your application as follows to compile Jsonnet code:

{:ok, %{"person1" => _, "person2" => _}} =
  Jsonnex.compile(~S[
  // A function that returns an object.
  local Person(name='Alice') = {
    name: name,
    welcome: 'Hello ' + name + '!',
  };
  {
    person1: Person(),
    person2: Person('Bob'),
  }])

If you would like to embed and format Jsonnet code in your application, you can use the ~JSONNET sigil and add the following to your .formater.exs file:

# Used by "mix format"
[
  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
  plugins: [Jsonnex.MixFormatter],
  jsonnet: [ # For supported options look at the `Jsonnex.Format` module
    string_style: :single
  ]
]