diff --git a/source/openapi_client/schemas.d b/source/openapi_client/schemas.d index a7e696b..fda0599 100644 --- a/source/openapi_client/schemas.d +++ b/source/openapi_client/schemas.d @@ -11,6 +11,7 @@ import vibe.core.log : logDebug; import std.container.rbtree : RedBlackTree; import std.file : mkdir, mkdirRecurse, write; +import std.exception : enforce; import std.array : array, appender, split, Appender; import std.algorithm : canFind, skipOver; import std.path : buildNormalizedPath, dirName; @@ -55,30 +56,30 @@ JsonSchema[string] jsonSchemaByRef; * name is in CamelCase. */ string getClassNameFromSchemaName(string schemaName) { - static immutable RedBlackTree!string RESERVED_CLASSES = new RedBlackTree!string([ - "cpp_type_info_ptr", - "Error", - "Exception", - "Object", - "Throwable", - "TypeInfo", - "TypeInfo_Array", - "TypeInfo_AssociativeArray", - "TypeInfo_Class", - "TypeInfo_Const", - "TypeInfo_Delegate", - "TypeInfo_Enum", - "TypeInfo_Function", - "TypeInfo_Interface", - "TypeInfo_Invariant", - "TypeInfo_Pointer", - "TypeInfo_Shared", - "TypeInfo_StaticArray", - "TypeInfo_Struct", - "TypeInfo_Tuple", - "TypeInfo_Vector", - "TypeInfo_Wild" - ]); + static bool[string] RESERVED_CLASSES = [ + "cpp_type_info_ptr": true, + "Error": true, + "Exception": true, + "Object": true, + "Throwable": true, + "TypeInfo": true, + "TypeInfo_Array": true, + "TypeInfo_AssociativeArray": true, + "TypeInfo_Class": true, + "TypeInfo_Const": true, + "TypeInfo_Delegate": true, + "TypeInfo_Enum": true, + "TypeInfo_Function": true, + "TypeInfo_Interface": true, + "TypeInfo_Invariant": true, + "TypeInfo_Pointer": true, + "TypeInfo_Shared": true, + "TypeInfo_StaticArray": true, + "TypeInfo_Struct": true, + "TypeInfo_Tuple": true, + "TypeInfo_Vector": true, + "TypeInfo_Wild": true, + ]; string className = toUpperCamelCase(tr(schemaName, ".", "_")); if (className in RESERVED_CLASSES) return className ~ "_"; @@ -117,6 +118,9 @@ string getSchemaNameFromRef(string ref_) { * files can be known as "model" or "dto" files. */ void writeSchemaFiles(OasDocument oasDocument, string targetDir, string packageRoot) { + enforce(oasDocument !is null, "oasDocument must not be null"); + enforce(oasDocument.components !is null, "oasDocument.components must not be null"); + enforce(oasDocument.components.schemas !is null, "oasDocument.components.schemas must not be null"); foreach (string schemaName, OasSchema schema; oasDocument.components.schemas) { JsonSchema jsonSchema = new JsonSchema(); jsonSchema.schemaName = schemaName; @@ -134,71 +138,71 @@ void writeSchemaFiles(OasDocument oasDocument, string targetDir, string packageR /** * A collection of variable names that cannot be used to generate Dlang code. */ -static immutable RedBlackTree!string RESERVED_WORDS = new RedBlackTree!string([ - "abstract", - "alias", - "align", - "asm", - "assert", - "auto", - "bool", - "break", - "byte", - "case", - "catch", - "cast", - "char", - "class", - "const", - "continue", - "dchar", - "debug", - "default", - "delegate", - "double", - "dstring", - "else", - "enum", - "export", - "extern", - "finally", - "float", - "for", - "foreach", - "foreach_reverse", - "function", - "if", - "in", - "invariant", - "immutable", - "import", - "int", - "lazy", - "long", - "mixin", - "module", - "new", - "nothrow", - "package", - "private", - "protected", - "public", - "pure", - "real", - "ref", - "scope", - "short", - "string", - "struct", - "switch", - "ulong", - "union", - "version", - "wchar", - "while", - "with", - "wstring", - ]); +bool[string] RESERVED_WORDS = [ + "abstract": true, + "alias": true, + "align": true, + "asm": true, + "assert": true, + "auto": true, + "bool": true, + "break": true, + "byte": true, + "case": true, + "catch": true, + "cast": true, + "char": true, + "class": true, + "const": true, + "continue": true, + "dchar": true, + "debug": true, + "default": true, + "delegate": true, + "double": true, + "dstring": true, + "else": true, + "enum": true, + "export": true, + "extern": true, + "finally": true, + "float": true, + "for": true, + "foreach": true, + "foreach_reverse": true, + "function": true, + "if": true, + "in": true, + "invariant": true, + "immutable": true, + "import": true, + "int": true, + "lazy": true, + "long": true, + "mixin": true, + "module": true, + "new": true, + "nothrow": true, + "package": true, + "private": true, + "protected": true, + "public": true, + "pure": true, + "real": true, + "ref": true, + "scope": true, + "short": true, + "string": true, + "struct": true, + "switch": true, + "ulong": true, + "union": true, + "version": true, + "wchar": true, + "while": true, + "with": true, + "wstring": true, + ]; /** * Writes a Dlang source file for a module that contains a class representing an OpenAPI