Skip to content

Commit

Permalink
Http server: Code generation extended enum support
Browse files Browse the repository at this point in the history
When a return member will be declare as an enum string, a local enum
will be decalre for it with the proper values.

A wrapper class for that enum will be used as the return type, so the
enum values will be replaced by their string representation.

The wrapper class also support auto enum conversion based on enum values
as long as the origin enum will have all the declared values.

For example:

In a swagger json definition file the following entry was added to model Thread:
             "status": {
                  "type": "string",
                  "description": "thread status",
                  "enum": [
                  "invalid",
                  "prestarted"]
              }

In the code we have another enum class defined as:

enum class my_status {invalide, prestarted}

The created Thread class will have a member called status and a enum
class Thread_status.

if we'll declare value of my_status:
my_status status = my_status::invalid;

We can use it to set the value of:
T t;
t.status = status;

Signed-off-by: Amnon Heiman <[email protected]>
Signed-off-by: Pekka Enberg <[email protected]>
  • Loading branch information
amnonh authored and Pekka Enberg committed Aug 19, 2014
1 parent d7f8c50 commit 9f9c485
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions modules/httpserver/json2code.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,39 @@ def create_h_file(data, hfile_name, api_name, init_method):
member = model["properties"][member_name]
if "description" in member:
print_comment(hfile, member["description"])
fprint(hfile, " ", config.jsonns, "::",
type_change(member["type"], member), " ",
member_name, ";\n")
if "enum" in member:
enum_name = model_name + "_" + member_name
fprint(hfile, " enum class ", enum_name," {")
for enum_entry in member["enum"]:
fprint(hfile, " ",enum_entry, ", ")
fprint(hfile, "NUM_ITEMS};")
wrapper = member_name + "_wrapper"
fprint(hfile, " struct ", wrapper, " : public jsonable {")
fprint(hfile, " ", wrapper, "() = default;")
fprint(hfile, " virtual std::string to_json() const {")
fprint(hfile, " switch(v) {")
for enum_entry in member["enum"]:
fprint(hfile, " case ", enum_name, "::", enum_entry, ": return \"\\\"", enum_entry, "\\\"\";")
fprint(hfile, " default: return \"Unknown\";")
fprint(hfile, " }")
fprint(hfile, " }")
fprint(hfile, " template<class T>")
fprint(hfile, " ", wrapper, "(const T& _v) {")
fprint(hfile, " switch(_v) {")
for enum_entry in member["enum"]:
fprint(hfile, " case T::", enum_entry, ": v = ", enum_name, "::", enum_entry, "; break;")
fprint(hfile, " default: v = ", enum_name, "::NUM_ITEMS;")
fprint(hfile, " }")
fprint(hfile, " }")
fprint(hfile, " ", enum_name, " v;")
fprint(hfile, " };")
fprint(hfile, " ", config.jsonns, "::json_element<",
member_name, "_wrapper> ",
member_name, ";\n")
else:
fprint(hfile, " ", config.jsonns, "::",
type_change(member["type"], member), " ",
member_name, ";\n")
member_init += " add(&" + member_name + ',"'
member_init += member_name + '");\n'
member_assignment += " " + member_name + " = " + "e." + member_name + ";\n"
Expand Down

0 comments on commit 9f9c485

Please sign in to comment.