From 2c226b90e26a4ad87f6035a0e09c4a898826c93d Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Wed, 23 Aug 2017 13:30:08 +0700 Subject: [PATCH] Support of enums in API generator --- godot/godotapigen.nim | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/godot/godotapigen.nim b/godot/godotapigen.nim index 65de77d..f718c60 100644 --- a/godot/godotapigen.nim +++ b/godot/godotapigen.nim @@ -164,13 +164,16 @@ proc incDerivedCount(types: var Table[string, GodotType], types.incDerivedCount(typ.baseName) proc toNimType(godotType: string): string = - case godotType: + result = case godotType: of "float": "float64" of "int": "int64" of "String": "string" of "File": "godottypes.File" else: godotType + if result.startsWith("enum."): + result = if result == "enum.Error": "Error" else: "int64" + proc toNimType(types: Table[string, GodotType], godotType: string): string = let origType = if godotType.contains(','): @@ -878,6 +881,18 @@ proc genApi*(targetDir: string, apiJsonFile: string) = if typ.isSingleton: genSingletonWithDerived(tree, typ, types) + if "enums" in obj: + # Quite often Godot enums are flags that need to be bitwise ORed + # Nim's enums don't serve well for that purpose, so we just generate + # integer constants. The only enum used in API is Error. + for enumDef in obj["enums"]: + let uniqueConstants = newJObject() + for k, v in enumDef["values"]: + if k notin obj["constants"]: + uniqueConstants[k] = v + if uniqueConstants.len > 0: + tree.add(makeConstSection(uniqueConstants)) + # first, generate declarations only for ease of # human readability of the file for property in obj["properties"]: