yanet-platform / yanet

A high performance framework for forwarding traffic based on DPDK
Other
171 stars 18 forks source link

Holy crusade against copy-paste while parsing configs #108

Closed 3Hren closed 4 months ago

3Hren commented 8 months ago

The problem

We have a bit of strange code in https://github.com/yanet-platform/yanet/blob/d53654af2450a6ed7630a4f6e6a2ac16abe50d17/dataplane/dataplane.cpp#L1772 Here, we parse config file and fill the https://github.com/yanet-platform/yanet/blob/d53654af2450a6ed7630a4f6e6a2ac16abe50d17/dataplane/dataplane.h#L331 using the following pattern:

if (exist(json, "some_key")) {
    configValues[eConfigType::some_enum] = json["some_key"];
}

, but such pattern is prone to copy-paste errors. What if we slightly change the key and check for existing for another one?

I suggest to:

  1. Replace this pattern to a single call using https://json.nlohmann.me/api/basic_json/value/ Then, the code above will transform to something like:
    configValues[eConfigType::some_enum] = json.value("some_key", 0); // Or meaningful default value.
  2. Replace the map<enum, uint64> to a named struct.
  3. Decompose it into a separate file.
  4. Write an ADL deserializer nearby.
  5. Then, we won't need for https://github.com/yanet-platform/yanet/blob/d53654af2450a6ed7630a4f6e6a2ac16abe50d17/dataplane/dataplane.h#L127, and can access config values directly.

We can start with dataplane config, but, of course, this can be propagated further to other configs.

The profit

Alternatives