Skip to content

LuaForConfiguration

Toby Schneider edited this page May 26, 2016 · 5 revisions

Overview

Right now in Goby2 we're using Protocol Buffers Text Format syntax for configuration files, which is nice because you have to define a strict schema (the .proto file) for your configuration in the same (default) language used on the wire.

E.g.,

simple.proto (defines a "message" that gets compiled to a C++ / Java / Python class)

message SimpleConfig
{
    required double a_double = 1;
    optional bool a_bool = 2;
    optional string a_string = 3;
}

simple.pb.cfg (equivalent to a single ProcessConfig block of a ".moos" file)

a_double: 3.14159
a_bool: true
a_string: "hello"

Which becomes (in C++) simple.cpp

#include "simple.pb.h"
#include "goby/core.h"
SimpleConfig my_cfg; // instantiate a C++ class version of the SimpleConfig protobuf message
class Simple : public goby::core::Application
{
   Simple() : goby::core::Application(&my_cfg) // Application reads the "simple.pb.cfg" and populates "my_cfg" if valid
   {
      assert(my_cfg.a_double() == 3.14159);
      assert(my_cfg.a_bool() == true);
      assert(my_cfg.a_string() == "hello");
      std::cout << my_cfg.DebugString() << std::endl;
      exit(0);
   }
   void loop()
   {}
};
int main(int argc, char* argv[])
{
   return goby::run<Simple>(argc, argv);
}

which you can run by

protoc --cpp_out . simple.proto
g++ simple.cpp simple.pb.cc -o simple -lgoby_core -I ~/goby/2.0/include -L ~/goby/2.0/lib
./simple simple.pb.cfg -d

The problem is however, that simple.pb.cfg is static and contains no scripting capability. What would be powerful is combining the schema (.proto) enforced configuration that we already have with the scripting power provided by Lua. I think this could work perhaps by redefining (or as an option) simple.pb.cfg to simple.lua:

simple.lua

SimpleConfig =
{
  a_double  = math.pi,
  a_bool    = true,
  a_string  = "hello",
}

With some additional code in goby::core::Application to call the Lua API and instantiate the appropriate proto file, the simple.cpp code shouldn't need to change.

Support for class based configuration hierarchy