Search code examples
configurationconfiguration-files

Are there configuration languages with classes?


I have a project where some common configurations are specified in json files which are processed by a simple python parser which generates source files in various languages which contain a bunch of constants specified in the json file. As a simplified example, one of the config files specify a bunch of parameters:

{
  "param1":
  {
    "type": "bool",
    "displayName": "Name 1",
    "defaultValue": false
  },

  "param2":
  {
    "type": "float",
    "displayName": "Name 2",
    "defaultValue": 0.0,
    "valueRange": [-24.0, 24.0],
    "interval": 0.1
  },

  "param3":
  {
    "type": "float",
    "displayName": "Name 3",
    "defaultValue": 0.0,
    "valueRange": [-24.0, 24.0],
    "interval": 0.1
  },

  "param4":
  {
    "type": "float",
    "displayName": "Name 4",  
    "defaultValue": 0.0,
    "valueRange": [-24.0, 24.0],
    "interval": 0.1
  }

Now in my real-world case, there can be up to 50 parameters with each single parameter having a lot more properties. As you see in the example above, param2, param3 and param4 have the same properties, except for their displayName.

I'd love to find some alternative configuration language that can be used here instead of json, which would allow me to specify something like classes, maybe like this pseudo-code snippet:

class GenericBoolParam (name_, defaultValue_)
{
    "type": "bool",
    "displayName": name_,
    "defaultValue": defaultValue_
}

class SpecialFloatParam (name_)
{
    "type": "float",
    "displayName": name_,
    "defaultValue": 0.0,
    "valueRange": [-24.0, 24.0],
    "interval": 0.1
}

{
   "param1" : GenericBoolParam ("Name1, false),
   "param2" : SpecialFloatParam ("Name2),
   "param3" : SpecialFloatParam ("Name3),
   "param4" : SpecialFloatParam ("Name4)
}

The exact syntax is not as important as the possibility to express these repeated pattern. A ready-to-use implementation to interpret the language with python would be a plus but is not mandatory. I am not aware of any suitable language, so I'm hoping to find some hints for a suitable language here.


Solution

  • Jsonnet comes to mind: it has almost exactly what you describe, calling them 'functions' rather than 'classes'. Multiple implementations are available, one in C++ and one in Go; the former has a simple Python binding.

    Taking an example from the homepage:

    local Person(name='Alice') = {
      name: name,
      welcome: 'Hello ' + name + '!',
    };
    {
      person1: Person(),
      person2: Person('Bob'),
    }
    

    becomes:

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

    Since the output of a Jsonnet program is a JSON document, you can feed that to your existing program.