diff --git a/languages/cpp/templates/additional-types/boolean.cpp b/languages/cpp/templates/additional-types/boolean.cpp new file mode 100644 index 00000000..feef819e --- /dev/null +++ b/languages/cpp/templates/additional-types/boolean.cpp @@ -0,0 +1 @@ +Boolean() \ No newline at end of file diff --git a/languages/cpp/templates/additional-types/integer.cpp b/languages/cpp/templates/additional-types/integer.cpp new file mode 100644 index 00000000..15c659a4 --- /dev/null +++ b/languages/cpp/templates/additional-types/integer.cpp @@ -0,0 +1 @@ +Number() \ No newline at end of file diff --git a/languages/cpp/templates/additional-types/number.cpp b/languages/cpp/templates/additional-types/number.cpp new file mode 100644 index 00000000..a5d368a4 --- /dev/null +++ b/languages/cpp/templates/additional-types/number.cpp @@ -0,0 +1 @@ +Float() \ No newline at end of file diff --git a/languages/cpp/templates/additional-types/string.cpp b/languages/cpp/templates/additional-types/string.cpp new file mode 100644 index 00000000..d2a52c84 --- /dev/null +++ b/languages/cpp/templates/additional-types/string.cpp @@ -0,0 +1 @@ +String() \ No newline at end of file diff --git a/languages/cpp/templates/callback-parameter-serialization/generic.cpp b/languages/cpp/templates/callback-parameter-serialization/generic.cpp new file mode 100644 index 00000000..530005ca --- /dev/null +++ b/languages/cpp/templates/callback-parameter-serialization/generic.cpp @@ -0,0 +1 @@ + WPEFramework::Core::ProxyType<${method.result.json}>* proxyResponse = reinterpret_cast*>(jsonResponse); \ No newline at end of file diff --git a/languages/cpp/templates/callback-parameter-serialization/primitive.cpp b/languages/cpp/templates/callback-parameter-serialization/primitive.cpp new file mode 100644 index 00000000..23bbace7 --- /dev/null +++ b/languages/cpp/templates/callback-parameter-serialization/primitive.cpp @@ -0,0 +1 @@ +${type} \ No newline at end of file diff --git a/languages/cpp/templates/json-types/default.cpp b/languages/cpp/templates/callback-parameter-serialization/ref.h similarity index 100% rename from languages/cpp/templates/json-types/default.cpp rename to languages/cpp/templates/callback-parameter-serialization/ref.h diff --git a/languages/cpp/templates/callback-response-instantiation/generic.cpp b/languages/cpp/templates/callback-response-instantiation/generic.cpp new file mode 100644 index 00000000..c8f3f090 --- /dev/null +++ b/languages/cpp/templates/callback-response-instantiation/generic.cpp @@ -0,0 +1 @@ +response \ No newline at end of file diff --git a/languages/cpp/templates/callback-response-instantiation/primitive.cpp b/languages/cpp/templates/callback-response-instantiation/primitive.cpp new file mode 100644 index 00000000..23bbace7 --- /dev/null +++ b/languages/cpp/templates/callback-response-instantiation/primitive.cpp @@ -0,0 +1 @@ +${type} \ No newline at end of file diff --git a/languages/cpp/templates/json-types/ref.c b/languages/cpp/templates/callback-response-instantiation/ref.h similarity index 100% rename from languages/cpp/templates/json-types/ref.c rename to languages/cpp/templates/callback-response-instantiation/ref.h diff --git a/languages/cpp/templates/callback-result-instantiation/additionalProperties.cpp b/languages/cpp/templates/callback-result-instantiation/additionalProperties.cpp new file mode 100644 index 00000000..a304af7e --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/additionalProperties.cpp @@ -0,0 +1,5 @@ + ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} elements = (*proxyResponse)->Variants(); + ${if.namespace.notsame}${info.Title}::${end.if.namespace.notsame}${title} response; + while (elements.Next()) { + response.insert(elements.Label(), elements.Current().${additional.type}.Value()); + } \ No newline at end of file diff --git a/languages/cpp/templates/callback-result-instantiation/array.cpp b/languages/cpp/templates/callback-result-instantiation/array.cpp new file mode 100644 index 00000000..603644e3 --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/array.cpp @@ -0,0 +1,5 @@ + auto index((*proxyResponse)->Elements()); + ${type} response; + while (index.Next() == true) { + response.push_back(index.Current().Value()); + } diff --git a/languages/cpp/templates/callback-result-instantiation/enum.cpp b/languages/cpp/templates/callback-result-instantiation/enum.cpp new file mode 100644 index 00000000..30fdf717 --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/enum.cpp @@ -0,0 +1 @@ + ${if.namespace.notsame}${info.Title}::${end.if.namespace.notsame}${title} response = (*proxyResponse)->Value(); \ No newline at end of file diff --git a/languages/cpp/templates/callback-result-instantiation/generic.cpp b/languages/cpp/templates/callback-result-instantiation/generic.cpp new file mode 100644 index 00000000..8c7a2fea --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/generic.cpp @@ -0,0 +1 @@ + ${type} response = (*proxyResponse)->Value(); \ No newline at end of file diff --git a/languages/cpp/templates/callback-result-instantiation/object.cpp b/languages/cpp/templates/callback-result-instantiation/object.cpp new file mode 100644 index 00000000..8722a92e --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/object.cpp @@ -0,0 +1,2 @@ + ${title} response; +${properties} \ No newline at end of file diff --git a/languages/cpp/templates/callback-result-instantiation/primitive.cpp b/languages/cpp/templates/callback-result-instantiation/primitive.cpp new file mode 100644 index 00000000..23bbace7 --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/primitive.cpp @@ -0,0 +1 @@ +${type} \ No newline at end of file diff --git a/languages/cpp/templates/callback-result-instantiation/property.cpp b/languages/cpp/templates/callback-result-instantiation/property.cpp new file mode 100644 index 00000000..31648b06 --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/property.cpp @@ -0,0 +1 @@ +${shape} ${if.non.object}response.${property} = (*proxyResponse)->${Property};${end.if.non.object} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/default.cpp b/languages/cpp/templates/callback-result-instantiation/ref.h similarity index 100% rename from languages/cpp/templates/result-instantiation/default.cpp rename to languages/cpp/templates/callback-result-instantiation/ref.h diff --git a/languages/cpp/templates/callback-result-instantiation/sub-property/object-separator.cpp b/languages/cpp/templates/callback-result-instantiation/sub-property/object-separator.cpp new file mode 100644 index 00000000..945c9b46 --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/sub-property/object-separator.cpp @@ -0,0 +1 @@ +. \ No newline at end of file diff --git a/languages/cpp/templates/callback-result-instantiation/sub-property/object.cpp b/languages/cpp/templates/callback-result-instantiation/sub-property/object.cpp new file mode 100644 index 00000000..c4569c3e --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/sub-property/object.cpp @@ -0,0 +1 @@ +${properties} \ No newline at end of file diff --git a/languages/cpp/templates/callback-result-instantiation/sub-property/property.cpp b/languages/cpp/templates/callback-result-instantiation/sub-property/property.cpp new file mode 100644 index 00000000..1f8541f2 --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/sub-property/property.cpp @@ -0,0 +1 @@ +${shape} ${if.non.object}response.${property.dependency}${property} = (*proxyResponse)->${Property.dependency}${Property};${end.if.non.object} \ No newline at end of file diff --git a/languages/cpp/templates/callback-result-instantiation/sub-property/ref.h b/languages/cpp/templates/callback-result-instantiation/sub-property/ref.h new file mode 100644 index 00000000..4da5b5d9 --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/sub-property/ref.h @@ -0,0 +1 @@ +${shape} \ No newline at end of file diff --git a/languages/cpp/templates/callback-result-instantiation/sub-property/title.cpp b/languages/cpp/templates/callback-result-instantiation/sub-property/title.cpp new file mode 100644 index 00000000..2a420b4b --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/sub-property/title.cpp @@ -0,0 +1 @@ +${Title} \ No newline at end of file diff --git a/languages/cpp/templates/callback-result-instantiation/title.cpp b/languages/cpp/templates/callback-result-instantiation/title.cpp new file mode 100644 index 00000000..2a420b4b --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/title.cpp @@ -0,0 +1 @@ +${Title} \ No newline at end of file diff --git a/languages/cpp/templates/callback-result-instantiation/tuple.cpp b/languages/cpp/templates/callback-result-instantiation/tuple.cpp new file mode 100644 index 00000000..76dde980 --- /dev/null +++ b/languages/cpp/templates/callback-result-instantiation/tuple.cpp @@ -0,0 +1,4 @@ + auto index((*proxyResponse)->Elements()); + ${if.namespace.notsame}${info.Title}::${end.if.namespace.notsame}${title} response; + response.first = index.Get(0); + response.second = index.Get(1); \ No newline at end of file diff --git a/languages/cpp/templates/json-types/additionalProperties.c b/languages/cpp/templates/json-types/additionalProperties.c deleted file mode 100644 index b814e5fb..00000000 --- a/languages/cpp/templates/json-types/additionalProperties.c +++ /dev/null @@ -1 +0,0 @@ -// need cpp code to init, get, set, clear additional properties... \ No newline at end of file diff --git a/languages/cpp/templates/json-types/additionalProperties.cpp b/languages/cpp/templates/json-types/additionalProperties.cpp new file mode 100644 index 00000000..3466cdce --- /dev/null +++ b/languages/cpp/templates/json-types/additionalProperties.cpp @@ -0,0 +1 @@ + using ${title} = WPEFramework::Core::JSON::VariantContainer; \ No newline at end of file diff --git a/languages/cpp/templates/json-types/array.c b/languages/cpp/templates/json-types/array.c deleted file mode 100644 index 508a9eac..00000000 --- a/languages/cpp/templates/json-types/array.c +++ /dev/null @@ -1,4 +0,0 @@ -uint32_t ${info.Title}_${Title}Array_Size(${type} handle); -${type} ${title}Array_Get(${type} handle, uint32_t index); -void ${info.Title}_${Title}Array_Add(${propertyType} handle, ${valueType} value); -void ${info.Title}_${Title}Array_Clear(${propertyType} handle); diff --git a/languages/cpp/templates/json-types/array.h b/languages/cpp/templates/json-types/array.h new file mode 100644 index 00000000..4ab0d5ac --- /dev/null +++ b/languages/cpp/templates/json-types/array.h @@ -0,0 +1 @@ +WPEFramework::Core::JSON::ArrayType<${json.type}> \ No newline at end of file diff --git a/languages/cpp/templates/json-types/boolean.c b/languages/cpp/templates/json-types/boolean.h similarity index 100% rename from languages/cpp/templates/json-types/boolean.c rename to languages/cpp/templates/json-types/boolean.h diff --git a/languages/cpp/templates/json-types/const.c b/languages/cpp/templates/json-types/const.h similarity index 100% rename from languages/cpp/templates/json-types/const.c rename to languages/cpp/templates/json-types/const.h diff --git a/languages/cpp/templates/json-types/default.h b/languages/cpp/templates/json-types/default.h new file mode 100644 index 00000000..4da5b5d9 --- /dev/null +++ b/languages/cpp/templates/json-types/default.h @@ -0,0 +1 @@ +${shape} \ No newline at end of file diff --git a/languages/cpp/templates/json-types/enum.cpp b/languages/cpp/templates/json-types/enum.cpp index 6776b41a..9acb1ea5 100644 --- a/languages/cpp/templates/json-types/enum.cpp +++ b/languages/cpp/templates/json-types/enum.cpp @@ -1,4 +1 @@ - /* ${title} ${description} */ - ENUM_CONVERSION_BEGIN(${name}) - { ${NAME}_${key}, _T("${value}") }, - ENUM_CONVERSION_END(${name}) + using JsonData_${type} = WPEFramework::Core::JSON::EnumType<${type}>; diff --git a/languages/cpp/templates/json-types/enum.h b/languages/cpp/templates/json-types/enum.h deleted file mode 100644 index 92108c75..00000000 --- a/languages/cpp/templates/json-types/enum.h +++ /dev/null @@ -1,4 +0,0 @@ -/* ${title} ${description} */ -typedef enum { - ${NAME}_${key}, -} ${name}; diff --git a/languages/cpp/templates/json-types/integer.c b/languages/cpp/templates/json-types/integer.h similarity index 100% rename from languages/cpp/templates/json-types/integer.c rename to languages/cpp/templates/json-types/integer.h diff --git a/languages/cpp/templates/json-types/namespace.c b/languages/cpp/templates/json-types/namespace.c deleted file mode 100644 index 8ea9d7de..00000000 --- a/languages/cpp/templates/json-types/namespace.c +++ /dev/null @@ -1 +0,0 @@ -FireboltSDK::${info.Title}:: \ No newline at end of file diff --git a/languages/cpp/templates/json-types/namespace.h b/languages/cpp/templates/json-types/namespace.h new file mode 100644 index 00000000..c63c1089 --- /dev/null +++ b/languages/cpp/templates/json-types/namespace.h @@ -0,0 +1 @@ +Firebolt::${info.Title}:: \ No newline at end of file diff --git a/languages/cpp/templates/json-types/null.h b/languages/cpp/templates/json-types/null.h new file mode 100644 index 00000000..fc121f63 --- /dev/null +++ b/languages/cpp/templates/json-types/null.h @@ -0,0 +1 @@ +WPEFramework::Core::JSON::VariantContainer \ No newline at end of file diff --git a/languages/cpp/templates/json-types/float.c b/languages/cpp/templates/json-types/number.h similarity index 100% rename from languages/cpp/templates/json-types/float.c rename to languages/cpp/templates/json-types/number.h diff --git a/languages/cpp/templates/json-types/object.cpp b/languages/cpp/templates/json-types/object.cpp index db24d12c..43c667d3 100644 --- a/languages/cpp/templates/json-types/object.cpp +++ b/languages/cpp/templates/json-types/object.cpp @@ -6,20 +6,20 @@ ${title}() : WPEFramework::Core::JSON::Container() { - ${properties.register} +${properties.register} } ${title}(const ${title}& other) { - ${properties.assign} +${properties.assign} } ${title}& operator=(const ${title}& other) { - ${properties.assign} +${properties.assign} return (*this); } public: - ${properties} +${properties} }; diff --git a/languages/cpp/templates/json-types/primitive.c b/languages/cpp/templates/json-types/primitive.c deleted file mode 100644 index e69de29b..00000000 diff --git a/languages/cpp/templates/json-types/primitive.h b/languages/cpp/templates/json-types/primitive.h new file mode 100644 index 00000000..23bbace7 --- /dev/null +++ b/languages/cpp/templates/json-types/primitive.h @@ -0,0 +1 @@ +${type} \ No newline at end of file diff --git a/languages/cpp/templates/json-types/property-assign.cpp b/languages/cpp/templates/json-types/property-assign.cpp index 88795136..36bdbf83 100644 --- a/languages/cpp/templates/json-types/property-assign.cpp +++ b/languages/cpp/templates/json-types/property-assign.cpp @@ -1,2 +1,2 @@ - Add(_T("${property}"), &${Property}); - ${Property} = other.${Property} + Add(_T("${property}"), &${Property}); + ${Property} = other.${Property}; diff --git a/languages/cpp/templates/json-types/property-register.cpp b/languages/cpp/templates/json-types/property-register.cpp index 400b4fd5..01e2a7e0 100644 --- a/languages/cpp/templates/json-types/property-register.cpp +++ b/languages/cpp/templates/json-types/property-register.cpp @@ -1,2 +1 @@ - Add(_T("${property}"), &${Property}); - \ No newline at end of file + Add(_T("${property}"), &${Property}); \ No newline at end of file diff --git a/languages/cpp/templates/json-types/ref.h b/languages/cpp/templates/json-types/ref.h new file mode 100644 index 00000000..4da5b5d9 --- /dev/null +++ b/languages/cpp/templates/json-types/ref.h @@ -0,0 +1 @@ +${shape} \ No newline at end of file diff --git a/languages/cpp/templates/json-types/string.c b/languages/cpp/templates/json-types/string.h similarity index 100% rename from languages/cpp/templates/json-types/string.c rename to languages/cpp/templates/json-types/string.h diff --git a/languages/cpp/templates/json-types/title.c b/languages/cpp/templates/json-types/title.h similarity index 100% rename from languages/cpp/templates/json-types/title.c rename to languages/cpp/templates/json-types/title.h diff --git a/languages/cpp/templates/json-types/tuple.c b/languages/cpp/templates/json-types/tuple.c deleted file mode 100644 index db24d12c..00000000 --- a/languages/cpp/templates/json-types/tuple.c +++ /dev/null @@ -1,25 +0,0 @@ - class ${title}: public WPEFramework::Core::JSON::Container { - public: - ~${title}() override = default; - - public: - ${title}() - : WPEFramework::Core::JSON::Container() - { - ${properties.register} - } - - ${title}(const ${title}& other) - { - ${properties.assign} - } - - ${title}& operator=(const ${title}& other) - { - ${properties.assign} - return (*this); - } - - public: - ${properties} - }; diff --git a/languages/cpp/templates/json-types/tuple.cpp b/languages/cpp/templates/json-types/tuple.cpp new file mode 100644 index 00000000..f734872b --- /dev/null +++ b/languages/cpp/templates/json-types/tuple.cpp @@ -0,0 +1 @@ + using ${title} = WPEFramework::Core::JSON::ArrayType<${json.type}>; \ No newline at end of file diff --git a/languages/cpp/templates/json-types/types/object.c b/languages/cpp/templates/json-types/types/object.c deleted file mode 100644 index e69de29b..00000000 diff --git a/languages/cpp/templates/json-types/void.cpp b/languages/cpp/templates/json-types/void.cpp new file mode 100644 index 00000000..fc121f63 --- /dev/null +++ b/languages/cpp/templates/json-types/void.cpp @@ -0,0 +1 @@ +WPEFramework::Core::JSON::VariantContainer \ No newline at end of file diff --git a/languages/cpp/templates/json-types/x-method.c b/languages/cpp/templates/json-types/x-method.h similarity index 100% rename from languages/cpp/templates/json-types/x-method.c rename to languages/cpp/templates/json-types/x-method.h diff --git a/languages/cpp/templates/language/enum-item.c b/languages/cpp/templates/language/enum-item.h similarity index 100% rename from languages/cpp/templates/language/enum-item.c rename to languages/cpp/templates/language/enum-item.h diff --git a/languages/cpp/templates/language/enum.c b/languages/cpp/templates/language/enum.h similarity index 100% rename from languages/cpp/templates/language/enum.c rename to languages/cpp/templates/language/enum.h diff --git a/languages/cpp/templates/language/parameter.c b/languages/cpp/templates/language/parameter.h similarity index 100% rename from languages/cpp/templates/language/parameter.c rename to languages/cpp/templates/language/parameter.h diff --git a/languages/cpp/templates/language/schema-item.c b/languages/cpp/templates/language/schema-item.h similarity index 100% rename from languages/cpp/templates/language/schema-item.c rename to languages/cpp/templates/language/schema-item.h diff --git a/languages/cpp/templates/language/schema.c b/languages/cpp/templates/language/schema.h similarity index 100% rename from languages/cpp/templates/language/schema.c rename to languages/cpp/templates/language/schema.h diff --git a/languages/cpp/templates/parameter-serialization/array.cpp b/languages/cpp/templates/parameter-serialization/array.cpp new file mode 100644 index 00000000..d45916ab --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/array.cpp @@ -0,0 +1,7 @@ + WPEFramework::Core::JSON::ArrayType ${Property}; + for (auto& element : ${property}) { +${if.object}${items.with.indent}${end.if.object}${if.non.object}${Property}.Add() = element;${end.if.non.object} + } + WPEFramework::Core::JSON::Variant ${Property}Variant; + ${Property}Variant.Array(${Property}); + jsonParameters.Set(_T("${property}"), ${Property}Variant); diff --git a/languages/cpp/templates/parameter-serialization/boolean.cpp b/languages/cpp/templates/parameter-serialization/boolean.cpp deleted file mode 100644 index f924ac94..00000000 --- a/languages/cpp/templates/parameter-serialization/boolean.cpp +++ /dev/null @@ -1,2 +0,0 @@ - WPEFramework::Core::JSON::Variant ${Property} = ${property}; - jsonParameters.Set(_T("${property}"), ${Property}); diff --git a/languages/cpp/templates/parameter-serialization/default.cpp b/languages/cpp/templates/parameter-serialization/default.cpp deleted file mode 100644 index f924ac94..00000000 --- a/languages/cpp/templates/parameter-serialization/default.cpp +++ /dev/null @@ -1,2 +0,0 @@ - WPEFramework::Core::JSON::Variant ${Property} = ${property}; - jsonParameters.Set(_T("${property}"), ${Property}); diff --git a/languages/cpp/templates/parameter-serialization/enum.cpp b/languages/cpp/templates/parameter-serialization/enum.cpp new file mode 100644 index 00000000..2367c118 --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/enum.cpp @@ -0,0 +1,3 @@ + ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} jsonValue = ${property}${if.optional}.value()${end.if.optional}; + WPEFramework::Core::JSON::Variant ${Property}(jsonValue.Data()); + jsonParameters.Set(_T("${property}"), ${Property}); \ No newline at end of file diff --git a/languages/cpp/templates/parameter-serialization/generic.cpp b/languages/cpp/templates/parameter-serialization/generic.cpp index 8ccb8295..566c7354 100644 --- a/languages/cpp/templates/parameter-serialization/generic.cpp +++ b/languages/cpp/templates/parameter-serialization/generic.cpp @@ -1,2 +1,2 @@ - WPEFramework::Core::JSON::Variant ${Property} = ${property}; - jsonParameters.Set(_T("${property}"), ${Property}); \ No newline at end of file + WPEFramework::Core::JSON::Variant ${Property}(${property}${if.optional}.value()${end.if.optional}); + jsonParameters.Set(_T("${property}"), ${Property}); diff --git a/languages/cpp/templates/parameter-serialization/object-array.cpp b/languages/cpp/templates/parameter-serialization/object-array.cpp new file mode 100644 index 00000000..0965afae --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/object-array.cpp @@ -0,0 +1,7 @@ + ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} ${Property}Container; +${properties} + string ${Property}Str; + ${Property}Container.ToString(${Property}Str); + WPEFramework::Core::JSON::VariantContainer ${Property}VariantContainer(${Property}Str); + WPEFramework::Core::JSON::Variant ${Property}Variant = ${Property}VariantContainer; + ${Property}.Add() = ${Property}Variant; diff --git a/languages/cpp/templates/parameter-serialization/object.cpp b/languages/cpp/templates/parameter-serialization/object.cpp index f924ac94..42226921 100644 --- a/languages/cpp/templates/parameter-serialization/object.cpp +++ b/languages/cpp/templates/parameter-serialization/object.cpp @@ -1,2 +1,8 @@ - WPEFramework::Core::JSON::Variant ${Property} = ${property}; + auto element = ${property}; + ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} ${Property}Container; +${properties} + string ${Property}Str; + ${Property}Container.ToString(${Property}Str); + WPEFramework::Core::JSON::VariantContainer ${Property}VariantContainer(${Property}Str); + WPEFramework::Core::JSON::Variant ${Property} = ${Property}VariantContainer; jsonParameters.Set(_T("${property}"), ${Property}); diff --git a/languages/cpp/templates/parameter-serialization/primitive.cpp b/languages/cpp/templates/parameter-serialization/primitive.cpp index f924ac94..23bbace7 100644 --- a/languages/cpp/templates/parameter-serialization/primitive.cpp +++ b/languages/cpp/templates/parameter-serialization/primitive.cpp @@ -1,2 +1 @@ - WPEFramework::Core::JSON::Variant ${Property} = ${property}; - jsonParameters.Set(_T("${property}"), ${Property}); +${type} \ No newline at end of file diff --git a/languages/cpp/templates/parameter-serialization/property.cpp b/languages/cpp/templates/parameter-serialization/property.cpp new file mode 100644 index 00000000..7036ea07 --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/property.cpp @@ -0,0 +1 @@ +${shape} ${if.non.object}${base.Title}Container.${Property} = element${if.base.optional}.value()${end.if.base.optional}.${property}${if.optional}.value()${end.if.optional};${end.if.non.object} \ No newline at end of file diff --git a/languages/cpp/templates/parameter-serialization/ref.h b/languages/cpp/templates/parameter-serialization/ref.h new file mode 100644 index 00000000..4da5b5d9 --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/ref.h @@ -0,0 +1 @@ +${shape} \ No newline at end of file diff --git a/languages/cpp/templates/parameter-serialization/string.cpp b/languages/cpp/templates/parameter-serialization/string.cpp deleted file mode 100644 index f924ac94..00000000 --- a/languages/cpp/templates/parameter-serialization/string.cpp +++ /dev/null @@ -1,2 +0,0 @@ - WPEFramework::Core::JSON::Variant ${Property} = ${property}; - jsonParameters.Set(_T("${property}"), ${Property}); diff --git a/languages/cpp/templates/parameter-serialization/sub-property/object-separator.cpp b/languages/cpp/templates/parameter-serialization/sub-property/object-separator.cpp new file mode 100644 index 00000000..945c9b46 --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/sub-property/object-separator.cpp @@ -0,0 +1 @@ +. \ No newline at end of file diff --git a/languages/cpp/templates/parameter-serialization/sub-property/object.cpp b/languages/cpp/templates/parameter-serialization/sub-property/object.cpp new file mode 100644 index 00000000..c4569c3e --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/sub-property/object.cpp @@ -0,0 +1 @@ +${properties} \ No newline at end of file diff --git a/languages/cpp/templates/parameter-serialization/sub-property/property.cpp b/languages/cpp/templates/parameter-serialization/sub-property/property.cpp new file mode 100644 index 00000000..58aa5943 --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/sub-property/property.cpp @@ -0,0 +1 @@ +${shape} ${if.non.object}${title}.${Property.dependency}${Property} = element${if.base.optional}.value()${end.if.base.optional}.${property.dependency}${property}${if.optional}.value()${end.if.optional};${end.if.non.object} \ No newline at end of file diff --git a/languages/cpp/templates/parameter-serialization/sub-property/ref.h b/languages/cpp/templates/parameter-serialization/sub-property/ref.h new file mode 100644 index 00000000..4da5b5d9 --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/sub-property/ref.h @@ -0,0 +1 @@ +${shape} \ No newline at end of file diff --git a/languages/cpp/templates/parameter-serialization/sub-property/title.cpp b/languages/cpp/templates/parameter-serialization/sub-property/title.cpp new file mode 100644 index 00000000..e2e80c7c --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/sub-property/title.cpp @@ -0,0 +1 @@ +${Title} diff --git a/languages/cpp/templates/parameters/optional.h b/languages/cpp/templates/parameters/optional.h index 69cb4659..d21cf868 100644 --- a/languages/cpp/templates/parameters/optional.h +++ b/languages/cpp/templates/parameters/optional.h @@ -1 +1 @@ -std::optional<${method.param.type}> ${method.param.name} \ No newline at end of file +const std::optional<${method.param.type}>& ${method.param.name} \ No newline at end of file diff --git a/languages/cpp/templates/result-initialization/additionalProperties.cpp b/languages/cpp/templates/result-initialization/additionalProperties.cpp new file mode 100644 index 00000000..c33c2ea0 --- /dev/null +++ b/languages/cpp/templates/result-initialization/additionalProperties.cpp @@ -0,0 +1 @@ + ${Title} ${property}; \ No newline at end of file diff --git a/languages/cpp/templates/result-initialization/array.cpp b/languages/cpp/templates/result-initialization/array.cpp new file mode 100644 index 00000000..2f0629ac --- /dev/null +++ b/languages/cpp/templates/result-initialization/array.cpp @@ -0,0 +1 @@ + ${type} ${property}; diff --git a/languages/cpp/templates/result-initialization/boolean.cpp b/languages/cpp/templates/result-initialization/boolean.cpp new file mode 100644 index 00000000..23bbf1be --- /dev/null +++ b/languages/cpp/templates/result-initialization/boolean.cpp @@ -0,0 +1 @@ + ${type} ${property} = false; \ No newline at end of file diff --git a/languages/cpp/templates/result-initialization/generic.cpp b/languages/cpp/templates/result-initialization/generic.cpp new file mode 100644 index 00000000..1bcce663 --- /dev/null +++ b/languages/cpp/templates/result-initialization/generic.cpp @@ -0,0 +1 @@ + ${type} ${property}; \ No newline at end of file diff --git a/languages/cpp/templates/result-initialization/namespace.cpp b/languages/cpp/templates/result-initialization/namespace.cpp new file mode 100644 index 00000000..b3afde8a --- /dev/null +++ b/languages/cpp/templates/result-initialization/namespace.cpp @@ -0,0 +1 @@ +${if.namespace.notsame}${parent.Title}::${end.if.namespace.notsame} \ No newline at end of file diff --git a/languages/cpp/templates/result-initialization/number.cpp b/languages/cpp/templates/result-initialization/number.cpp new file mode 100644 index 00000000..6e34ae51 --- /dev/null +++ b/languages/cpp/templates/result-initialization/number.cpp @@ -0,0 +1 @@ + ${type} ${property} = 0; \ No newline at end of file diff --git a/languages/cpp/templates/result-initialization/primitive.cpp b/languages/cpp/templates/result-initialization/primitive.cpp new file mode 100644 index 00000000..23bbace7 --- /dev/null +++ b/languages/cpp/templates/result-initialization/primitive.cpp @@ -0,0 +1 @@ +${type} \ No newline at end of file diff --git a/languages/cpp/templates/result-initialization/string.cpp b/languages/cpp/templates/result-initialization/string.cpp new file mode 100644 index 00000000..2f0629ac --- /dev/null +++ b/languages/cpp/templates/result-initialization/string.cpp @@ -0,0 +1 @@ + ${type} ${property}; diff --git a/languages/cpp/templates/result-initialization/title.cpp b/languages/cpp/templates/result-initialization/title.cpp new file mode 100644 index 00000000..2a420b4b --- /dev/null +++ b/languages/cpp/templates/result-initialization/title.cpp @@ -0,0 +1 @@ +${Title} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/additionalProperties.cpp b/languages/cpp/templates/result-instantiation/additionalProperties.cpp new file mode 100644 index 00000000..e5d7b116 --- /dev/null +++ b/languages/cpp/templates/result-instantiation/additionalProperties.cpp @@ -0,0 +1,6 @@ + while (jsonResult.Variants().Next()) { + std::string label = jsonResult.Variants().Label(); + ${property}.emplace(std::piecewise_construct, + std::forward_as_tuple(label), + std::forward_as_tuple(jsonResult.Variants().Current().${additional.type})); + } \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/array.cpp b/languages/cpp/templates/result-instantiation/array.cpp new file mode 100644 index 00000000..2fab5f07 --- /dev/null +++ b/languages/cpp/templates/result-instantiation/array.cpp @@ -0,0 +1,4 @@ + auto index(jsonResult.Elements()); + while (index.Next() == true) { +${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}.push_back(index.Current().Value());${end.if.non.object} + } \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/boolean.cpp b/languages/cpp/templates/result-instantiation/boolean.cpp deleted file mode 100644 index 036b4a7d..00000000 --- a/languages/cpp/templates/result-instantiation/boolean.cpp +++ /dev/null @@ -1,2 +0,0 @@ - *${property} = jsonResult.Value(); - \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/generic.cpp b/languages/cpp/templates/result-instantiation/generic.cpp new file mode 100644 index 00000000..8949a5fb --- /dev/null +++ b/languages/cpp/templates/result-instantiation/generic.cpp @@ -0,0 +1 @@ + ${property} = jsonResult.Value(); \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/object-array.cpp b/languages/cpp/templates/result-instantiation/object-array.cpp new file mode 100644 index 00000000..0c396b43 --- /dev/null +++ b/languages/cpp/templates/result-instantiation/object-array.cpp @@ -0,0 +1,4 @@ + ${type} ${property}Result${level}; + ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} jsonResult = index.Current(); +${properties} + ${property}.push_back(${property}Result${level}); \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/object.cpp b/languages/cpp/templates/result-instantiation/object.cpp index dcf0b333..5119c58b 100644 --- a/languages/cpp/templates/result-instantiation/object.cpp +++ b/languages/cpp/templates/result-instantiation/object.cpp @@ -1,4 +1,3 @@ - WPEFramework::Core::ProxyType* resultPtr = new WPEFramework::Core::ProxyType(); - *resultPtr = WPEFramework::Core::ProxyType::Create(); - *(*resultPtr) = jsonResult; - *${property} = static_cast<${info.Title}_${title}>(resultPtr); + ${type} ${property}Result${level}; +${properties} + ${property} = ${property}Result${level}; \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/property.cpp b/languages/cpp/templates/result-instantiation/property.cpp new file mode 100644 index 00000000..fe19dab1 --- /dev/null +++ b/languages/cpp/templates/result-instantiation/property.cpp @@ -0,0 +1 @@ +${shape} ${if.non.array}${if.non.object}${base.title}Result${level}.${property} = jsonResult.${Property}.Value();${end.if.non.object}${end.if.non.array} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/ref.h b/languages/cpp/templates/result-instantiation/ref.h new file mode 100644 index 00000000..4da5b5d9 --- /dev/null +++ b/languages/cpp/templates/result-instantiation/ref.h @@ -0,0 +1 @@ +${shape} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/string.cpp b/languages/cpp/templates/result-instantiation/string.cpp index b6c9dcb5..886728a4 100644 --- a/languages/cpp/templates/result-instantiation/string.cpp +++ b/languages/cpp/templates/result-instantiation/string.cpp @@ -1,2 +1 @@ - FireboltSDK::JSON::String* strResult = new FireboltSDK::JSON::String(jsonResult); - *value = static_cast(strResult); + ${property} = jsonResult.Value().c_str(); \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/sub-property/array.cpp b/languages/cpp/templates/result-instantiation/sub-property/array.cpp new file mode 100644 index 00000000..efa1adeb --- /dev/null +++ b/languages/cpp/templates/result-instantiation/sub-property/array.cpp @@ -0,0 +1,4 @@ + auto index(jsonResult.${Property}.Elements()); + while (index.Next() == true) { +${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}.push_back(index.Current().Value());${end.if.non.object} + } \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/sub-property/object-array.cpp b/languages/cpp/templates/result-instantiation/sub-property/object-array.cpp new file mode 100644 index 00000000..06a4d04d --- /dev/null +++ b/languages/cpp/templates/result-instantiation/sub-property/object-array.cpp @@ -0,0 +1,4 @@ + ${type} ${property}Result${level}; + ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} jsonResult = index.Current(); +${properties} + ${property}Result.${property}${if.impl.optional}.value()${end.if.impl.optional}.push_back(${property}Result${level}); \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/sub-property/object-separator.cpp b/languages/cpp/templates/result-instantiation/sub-property/object-separator.cpp new file mode 100644 index 00000000..945c9b46 --- /dev/null +++ b/languages/cpp/templates/result-instantiation/sub-property/object-separator.cpp @@ -0,0 +1 @@ +. \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/sub-property/object.cpp b/languages/cpp/templates/result-instantiation/sub-property/object.cpp new file mode 100644 index 00000000..c04d0a50 --- /dev/null +++ b/languages/cpp/templates/result-instantiation/sub-property/object.cpp @@ -0,0 +1 @@ +${properties} diff --git a/languages/cpp/templates/result-instantiation/sub-property/property.cpp b/languages/cpp/templates/result-instantiation/sub-property/property.cpp new file mode 100644 index 00000000..97e0dc0f --- /dev/null +++ b/languages/cpp/templates/result-instantiation/sub-property/property.cpp @@ -0,0 +1 @@ +${shape} ${if.non.object}${base.title}Result${level}.${property.dependency}${if.impl.optional}value().${end.if.impl.optional}${property} = jsonResult.${Property.dependency}${Property};${end.if.non.object} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/sub-property/ref.h b/languages/cpp/templates/result-instantiation/sub-property/ref.h new file mode 100644 index 00000000..4da5b5d9 --- /dev/null +++ b/languages/cpp/templates/result-instantiation/sub-property/ref.h @@ -0,0 +1 @@ +${shape} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/sub-property/title.cpp b/languages/cpp/templates/result-instantiation/sub-property/title.cpp new file mode 100644 index 00000000..2a420b4b --- /dev/null +++ b/languages/cpp/templates/result-instantiation/sub-property/title.cpp @@ -0,0 +1 @@ +${Title} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/title.cpp b/languages/cpp/templates/result-instantiation/title.cpp new file mode 100644 index 00000000..2a420b4b --- /dev/null +++ b/languages/cpp/templates/result-instantiation/title.cpp @@ -0,0 +1 @@ +${Title} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/tuple.cpp b/languages/cpp/templates/result-instantiation/tuple.cpp new file mode 100644 index 00000000..58f79f1d --- /dev/null +++ b/languages/cpp/templates/result-instantiation/tuple.cpp @@ -0,0 +1,3 @@ + auto index(jsonResult.Elements()); + ${property}.first = index.Get(0); + ${property}.second = index.Get(1); \ No newline at end of file diff --git a/languages/cpp/templates/types/additionalProperties.h b/languages/cpp/templates/types/additionalProperties.h index 05581106..eced27fe 100644 --- a/languages/cpp/templates/types/additionalProperties.h +++ b/languages/cpp/templates/types/additionalProperties.h @@ -1 +1 @@ -std::unordered_map<${key}, ${type}> ${title}; +using ${title} = std::unordered_map<${key}, ${type}>; diff --git a/languages/cpp/templates/types/const.h b/languages/cpp/templates/types/const.h index e69de29b..c6d1c815 100644 --- a/languages/cpp/templates/types/const.h +++ b/languages/cpp/templates/types/const.h @@ -0,0 +1 @@ +std::string \ No newline at end of file diff --git a/languages/cpp/templates/types/default.cpp b/languages/cpp/templates/types/default.cpp deleted file mode 100644 index 1f1fe89d..00000000 --- a/languages/cpp/templates/types/default.cpp +++ /dev/null @@ -1,4 +0,0 @@ -/* - * ${title} - ${description} - */ -${shape} diff --git a/languages/cpp/templates/types/default.h b/languages/cpp/templates/types/default.h new file mode 100644 index 00000000..a034b220 --- /dev/null +++ b/languages/cpp/templates/types/default.h @@ -0,0 +1 @@ +${shape} diff --git a/languages/cpp/templates/types/items.h b/languages/cpp/templates/types/items.h new file mode 100644 index 00000000..52268157 --- /dev/null +++ b/languages/cpp/templates/types/items.h @@ -0,0 +1 @@ +${title}${delimiter}, ${end.delimiter} \ No newline at end of file diff --git a/languages/cpp/templates/types/null.h b/languages/cpp/templates/types/null.h index e69de29b..ab6cc8f6 100644 --- a/languages/cpp/templates/types/null.h +++ b/languages/cpp/templates/types/null.h @@ -0,0 +1 @@ +void \ No newline at end of file diff --git a/languages/cpp/templates/types/object.h b/languages/cpp/templates/types/object.h index c7877332..88d35c97 100644 --- a/languages/cpp/templates/types/object.h +++ b/languages/cpp/templates/types/object.h @@ -1,5 +1,3 @@ struct ${title} { - ${properties} - }; \ No newline at end of file diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index c853e324..4def4f6e 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -1252,15 +1252,16 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) const pullsResultType = pullsResult && types.getSchemaShape(pullsResult, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) const pullsForType = pullsResult && types.getSchemaType(pullsResult, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) const pullsParamsType = pullsParams ? types.getSchemaShape(pullsParams, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) : '' - const serializedParams = flattenedMethod.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'parameter-serialization', property: param.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true })).join('\n') + const serializedParams = flattenedMethod.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'parameter-serialization', property: param.name, required: param.required || false, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true })).join('\n') const resultInst = types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-instantiation', property: flattenedMethod.result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) // w/out level: 1, getSchemaShape skips anonymous types, like primitives - const serializedEventParams = event ? flattenedMethod.params.filter(p => p.name !== 'listen').map(param => types.getSchemaShape(param.schema, json, {templateDir: 'parameter-serialization', property: param.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true })).join('\n') : '' + const resultInit = types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-initialization', property: flattenedMethod.result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) // w/out level: 1, getSchemaShape skips anonymous types, like primitives + const serializedEventParams = event ? flattenedMethod.params.filter(p => p.name !== 'listen').map(param => types.getSchemaShape(param.schema, json, {templateDir: 'parameter-serialization', property: param.name, required: param.required || false, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true })).join('\n') : '' // this was wrong... check when we merge if it was fixed - const callbackSerializedParams = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'parameter-serialization', property: result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) : '' - const callbackResultInst = event ? types.getSchemaShape(event, json, { templateDir: 'result-instantiation' }) : '' -// const callbackResponseInst = event ? types.getSchemaInstantiation(event, json, event.name, { instantiationType: 'callback.response' }) : '' + const callbackSerializedParams = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-parameter-serialization', property: result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) : '' + + const callbackResultInst = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-result-instantiation', property: result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) : '' // hmm... how is this different from callbackSerializedParams? i guess they get merged? - const callbackResponseInst = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'parameter-serialization', property: result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) : '' + const callbackResponseInst = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-response-instantiation', property: result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) : '' const resultType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: state.typeTemplateDir }) : '' const resultJsonType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: 'json-types' }) : '' const resultParams = generateResultParams(result.schema, json, templates, { name: result.name}) diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index f25a9689..e7bd5e71 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -51,6 +51,21 @@ function setAllocatedPrimitiveProxies(m) { } const capitalize = str => str ? str[0].toUpperCase() + str.substr(1) : str +const indent = (str, padding) => { + let first = true + if (str) { + return str.split('\n').map(line => { + if (first) { + first = false + return line + } + else { + return padding + line + } + }).join('\n') + } +} + const safeName = value => value.split(':').pop().replace(/[\.\-]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase() // TODO: This is what's left of getMethodSignatureParams. We need to figure out / handle C's `FireboltTypes_StringHandle` @@ -78,7 +93,7 @@ function getMethodSignatureParams(method, module, { destination, callback }) { }).join(', ') } -function getMethodSignatureResult(method, module, { destination, callback, overrideRule = false }) { +function getMethodSignatureResult(method, module, { destination, callback }) { let type = getSchemaType(method.result.schema, module, { destination, namespace : true }) let result = '' @@ -124,7 +139,7 @@ const getXSchemaGroup = (schema, module) => { return group } -function insertSchemaMacros(content, schema, module, name, parent, property, recursive = true) { +function insertSchemaMacros(content, schema, module, { name = '', parent = '', property = '', required = true, recursive = true, templateDir = 'types'}) { const title = name || schema.title || '' let moduleTitle = getXSchemaGroup(schema, module) @@ -134,9 +149,11 @@ function insertSchemaMacros(content, schema, module, name, parent, property, rec .replace(/\$\{TITLE\}/g, title.toUpperCase()) .replace(/\$\{property\}/g, property) .replace(/\$\{Property\}/g, capitalize(property)) - .replace(/\$\{if\.namespace\.notsame}(.*?)\$\{end\.if\.namespace\.notsame\}/g, (module.info.title !== parent) ? '$1' : '') + .replace(/\$\{if\.namespace\.notsame}(.*?)\$\{end\.if\.namespace\.notsame\}/g, (module.info.title !== (parent || moduleTitle)) ? '$1' : '') .replace(/\$\{parent\.title\}/g, parent) .replace(/\$\{parent\.Title\}/g, capitalize(parent)) + .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, required ? '' : '$1') + .replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, required ? '' : '$1') .replace(/\$\{description\}/g, schema.description ? schema.description : '') .replace(/\$\{summary\}/g, schema.description ? schema.description.split('\n')[0] : '') .replace(/\$\{name\}/g, title) @@ -146,14 +163,14 @@ function insertSchemaMacros(content, schema, module, name, parent, property, rec .replace(/\$\{info.TITLE\}/g, moduleTitle.toUpperCase()) if (recursive) { - content = content.replace(/\$\{type\}/g, getSchemaType(schema, module, { destination: state.destination, section: state.section, code: false, namespace: true })) + content = content.replace(/\$\{type\}/g, getSchemaType(schema, module, { templateDir: templateDir, destination: state.destination, section: state.section, code: false, namespace: true })) } return content } // TODO using JSON.stringify probably won't work for many languages... const insertConstMacros = (content, schema, module, name) => { - content = content.replace(/\$\{value\}/g, JSON.stringify(schema.const)) + content = content.replace(/\$\{value\}/g, (typeof schema.const === 'string' ? `'${schema.const}'` : schema.const)) return content } @@ -205,6 +222,7 @@ const insertObjectAdditionalPropertiesMacros = (content, schema, module, title, .replace(/\$\{key\}/g, key) .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, '') .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/g, '') + .replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/g, '') return content } @@ -212,7 +230,9 @@ const insertObjectAdditionalPropertiesMacros = (content, schema, module, title, const insertObjectMacros = (content, schema, module, title, property, options) => { const options2 = options ? JSON.parse(JSON.stringify(options)) : {} options2.parent = title + options2.parentLevel = options.parentLevel options2.level = options.level + 1 + options2.templateDir = options.templateDir ;(['properties', 'properties.register', 'properties.assign']).forEach(macro => { const indent = (content.split('\n').find(line => line.includes("${" + macro + "}")) || '').match(/^\s+/) || [''][0] @@ -223,13 +243,19 @@ const insertObjectMacros = (content, schema, module, title, property, options) = if (schema.properties) { Object.entries(schema.properties).forEach(([name, prop], i) => { - options2.property = name + let localizedProp = localizeDependencies(prop, module) + const subProperty = getTemplate(path.join(options2.templateDir, 'sub-property/object')) + options2.templateDir += subProperty ? '/sub-property' : '' + const objSeparator = getTemplate(path.join(options2.templateDir, 'object-separator')) + + options2.required = prop.required ? prop.required : false const schemaShape = getSchemaShape(prop, module, options2) + const type = getSchemaType(prop, module, options2) // don't push properties w/ unsupported types if (type) { - properties.push((i !== 0 ? indent : '') + template + let replacedTemplate = template .replace(/(^\s+)/g, '$1'.repeat(options2.level)) .replace(/\$\{property\}/g, name) .replace(/\$\{Property\}/g, capitalize(name)) @@ -239,7 +265,30 @@ const insertObjectMacros = (content, schema, module, title, property, options) = .replace(/\$\{description\}/g, prop.description || '') .replace(/\$\{summary\}/g, prop.description ? prop.description.split('\n')[0] : '') .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/gms, i === schema.properties.length - 1 ? '' : '$1') - .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, schema.required && schema.required.includes(name) ? '' : '$1')) + .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, schema.required && schema.required.includes(name) ? '' : '$1') + .replace(/\$\{if\.base\.optional\}(.*?)\$\{end\.if\.base\.optional\}/gms, options.required ? '' : '$1') + .replace(/\$\{if\.non\.object\}(.*?)\$\{end\.if\.non\.object\}/gms, (localizedProp.type === 'object') ? '' : '$1') + .replace(/\$\{if\.non\.array\}(.*?)\$\{end\.if\.non\.array\}/gms, (localizedProp.type === 'array') ? '' : '$1') + let baseTitle = options.property + if (localizedProp.type === 'object') { + replacedTemplate = replacedTemplate + .replace(/\$\{property.dependency\}/g, ((options.level > 1) ? '${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}' : '') + name + objSeparator) + .replace(/\$\{Property.dependency\}/g, ((options.level > 1) ? '${Property.dependency}' : '') + capitalize(name) + (objSeparator)) + } + else { + if (options2.level <= 1) { + replacedTemplate = replacedTemplate + .replace(/\$\{property.dependency\}/g, '') + .replace(/\$\{Property.dependency\}/g, '') + .replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, '') + } + } + replacedTemplate = replacedTemplate + .replace(/\$\{obj\.separator}/g, objSeparator) + .replace(/\$\{base.title\}/g, (baseTitle ? (baseTitle)[0].toLowerCase() + (baseTitle).substr(1) : '')).trimEnd() + .replace(/\$\{base.Title\}/g, (baseTitle ? (baseTitle)[0].toUpperCase() + (baseTitle).substr(1) : '')).trimEnd() + .replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, prop.required ? '' : '$1') + properties.push((i !== 0 ? indent : '') + replacedTemplate) } }) } @@ -289,12 +338,17 @@ const insertObjectMacros = (content, schema, module, title, property, options) = const regex = new RegExp("\\$\\{" + macro + "\\}", "g") content = content.replace(regex, properties.join('\n')) + .replace(/\$\{level}/g, options.parentLevel > 0 ? options.parentLevel : '') }) return content } -const insertArrayMacros = (content, schema, module, name) => { +const insertArrayMacros = (content, schema, module, level = 0, items) => { + content = content + .replace(/\$\{json\.type\}/g, getSchemaType(schema.items, module, { templateDir: 'json-types', destination: state.destination, section: state.section, code: false, namespace: true })) + .replace(/\$\{items\}/g, items) + .replace(/\$\{items\.with\.indent\}/g, indent(items, ' ')) return content } @@ -321,16 +375,17 @@ const insertTupleMacros = (content, schema, module, title, options) => { .replace(/\$\{summary\}/g, prop.description ? prop.description.split('\n')[0] : '') .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, i === schema.items.length - 1 ? '' : '$1') .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, '') + .replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, '') } content = content.replace(/\$\{properties\}/g, schema.items.map((prop, i) => doMacroWork(propTemplate, prop, i, propIndent)).join(tupleDelimiter)) content = content.replace(/\$\{items\}/g, schema.items.map((prop, i) => doMacroWork(itemsTemplate, prop, i, itemsIndent)).join(tupleDelimiter)) - + content = content.replace(/\$\{json\.type\}/g, getSchemaType(schema.items[0], module, { templateDir: 'json-types', destination: state.destination, section: state.section, code: false, namespace: true })) return content } -const getPrimitiveType = (type, templateDir) => { - const template = getTemplate(path.join(templateDir, type)) +const getPrimitiveType = (type, templateDir = 'types') => { + const template = getTemplate(path.join(templateDir, type)) || getTemplate(path.join(templateDir, 'generic')) return primitives[type] || template } @@ -369,7 +424,7 @@ const sanitize = (schema) => { return result } -function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name = '', parent = '', property = '', level = 0, summary, descriptions = true, destination, section, enums = true, skipTitleOnce = false } = {}) { +function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', parent = '', property = '', required = true, parentLevel = 0, level = 0, summary, descriptions = true, destination, section, enums = true, skipTitleOnce = false, array = false } = {}) { schema = sanitize(schema) state.destination = destination @@ -380,37 +435,38 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name } const suffix = destination && ('.' + destination.split('.').pop()) || '' - const theTitle = insertSchemaMacros(getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, schema.title || name, parent, property, false) + const theTitle = insertSchemaMacros(getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, { name: schema.title, parent, property, required, recursive: false}) let result = level === 0 ? getTemplate(path.join(templateDir, 'default' + suffix)) : '${shape}' + let genericTemplate = getTemplate(path.join(templateDir, 'generic' + suffix)) if (enums && level === 0 && Array.isArray(schema.enum) && ((schema.type === "string") || (schema.type[0] === "string"))) { - result = getTemplate(path.join(templateDir, 'enum' + suffix)) - return insertSchemaMacros(insertEnumMacros(result, schema, module, theTitle, suffix, templateDir), schema, module, theTitle, parent, property) + result = getTemplate(path.join(templateDir, 'enum' + suffix)) || genericTemplate + return insertSchemaMacros(insertEnumMacros(result, schema, module, theTitle, suffix, templateDir), schema, module, { name: theTitle, parent, property, required }) } if (schema['$ref']) { const someJson = getPath(schema['$ref'], module) if (someJson) { - return getSchemaShape(someJson, module, { templateDir, name, parent, property, level, summary, descriptions, destination, enums }) + return getSchemaShape(someJson, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums, array }) } throw "Unresolvable $ref: " + schema['ref'] + ", in " + module.info.title } else if (schema.hasOwnProperty('const')) { const shape = insertConstMacros(getTemplate(path.join(templateDir, 'const' + suffix)), schema, module, theTitle) - result = insertSchemaMacros(result.replace(/\$\{shape\}/g, shape), schema, module, theTitle, parent, property) + result = insertSchemaMacros(result.replace(/\$\{shape\}/g, shape), schema, module, { name: theTitle, parent, property, required }) return result } else if (!skipTitleOnce && (level > 0) && schema.title) { let enumType = (schema.type === 'string' && Array.isArray(schema.enum)) // TODO: allow the 'ref' template to actually insert the shape using getSchemaShape - const innerShape = getSchemaShape(schema, module, { skipTitleOnce: true, templateDir, name, parent, property, level, summary, descriptions, destination, enums: enumType }) + const innerShape = getSchemaShape(schema, module, { skipTitleOnce: true, templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: enumType, array }) const shape = getTemplate(path.join(templateDir, 'ref' + suffix)) .replace(/\$\{shape\}/g, innerShape) result = result.replace(/\$\{shape\}/g, shape) - return insertSchemaMacros(result, schema, module, theTitle, parent, property) + return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required }) } else if (schema.type === 'object') { let shape @@ -419,16 +475,17 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name shape = insertObjectAdditionalPropertiesMacros(additionalPropertiesTemplate, schema, module, theTitle, { level, parent, templateDir, namespace: true }) } else { - shape = insertObjectMacros(getTemplate(path.join(templateDir, 'object' + suffix)), schema, module, theTitle, property, { level, parent, property, templateDir, descriptions, destination, section, enums, namespace: true }) + let objectLevel = array ? 0 : level + shape = insertObjectMacros(getTemplate(path.join(templateDir, 'object' + (array ? '-array' : '') + suffix)) || genericTemplate, schema, module, theTitle, property, { parentLevel, level: objectLevel, parent, property, required, templateDir, descriptions, destination, section, enums, namespace: true }) } result = result.replace(/\$\{shape\}/g, shape) - return insertSchemaMacros(result, schema, module, theTitle, parent, property) + return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required, templateDir }) } else if (schema.anyOf || schema.oneOf) { const template = getTemplate(path.join(templateDir, 'anyOfSchemaShape' + suffix)) let shape if (template) { - shape = insertAnyOfMacros(template, schema, module, theTitle) + shape = insertAnyOfMacros(template, schema, module, theTitle) } else { // borrow anyOf logic, note that schema is a copy, so we're not breaking it. @@ -439,7 +496,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name } if (shape) { result = result.replace(/\$\{shape\}/g, shape) - return insertSchemaMacros(result, schema, module, theTitle, parent, property) + return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required }) } else { return '' @@ -467,26 +524,28 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name } delete union['$ref'] - return getSchemaShape(union, module, { templateDir, name, parent, property, level, summary, descriptions, destination, enums: false }) + return getSchemaShape(union, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: false, array }) } else if (schema.type === "array" && schema.items && isSupportedTuple(schema)) { // tuple - const shape = insertTupleMacros(getTemplate(path.join(templateDir, 'tuple' + suffix)), schema, module, theTitle, { level, templateDir, descriptions, destination, section, enums }) + const shape = insertTupleMacros(getTemplate(path.join(templateDir, 'tuple' + suffix)) || genericTemplate, schema, module, theTitle, { level, templateDir, descriptions, destination, section, enums }) result = result.replace(/\$\{shape\}/g, shape) - return insertSchemaMacros(result, schema, module, theTitle, parent, property) + return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required, templateDir }) } else if (schema.type === "array" && schema.items && !Array.isArray(schema.items)) { // array - const items = getSchemaShape(schema.items, module, { templateDir, name, parent, property, level, summary, descriptions, destination, enums: false }) - const shape = insertArrayMacros(getTemplate(path.join(templateDir, 'array' + suffix)), schema, module, items) + const items = getSchemaShape(schema.items, module, { templateDir, parent, property, required, parentLevel: parentLevel + 1, level, summary, descriptions, destination, enums: false , array: true}) + const shape = insertArrayMacros(getTemplate(path.join(templateDir, 'array' + suffix)) || genericTemplate, schema, module, level, items) result = result.replace(/\$\{shape\}/g, shape) - return insertSchemaMacros(result, schema, module, items, parent, property) + .replace(/\$\{if\.object\}(.*?)\$\{end\.if\.object\}/gms, (schema.items.type === 'object') ? '$1' : '') + .replace(/\$\{if\.non\.object\}(.*?)\$\{end\.if\.non\.object\}/gms, (schema.items.type !== 'object') ? '$1' : '') + return insertSchemaMacros(result, schema, module, { name: items, parent, property, required, templateDir }) } else if (schema.type) { - const shape = insertPrimitiveMacros(getTemplate(path.join(templateDir, 'primitive' + suffix)), schema, module, theTitle, templateDir) + const shape = insertPrimitiveMacros(getTemplate(path.join(templateDir, 'primitive' + suffix) || genericTemplate), schema, module, theTitle, templateDir) result = result.replace(/\$\{shape\}/g, shape) if (level > 0) { - return insertSchemaMacros(result, schema, module, theTitle, parent, property) + return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required, templateDir}) } } @@ -548,7 +607,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin const suffix = destination && ('.' + destination.split('.').pop()) || '' const namespaceStr = namespace ? getTemplate(path.join(templateDir, 'namespace' + suffix)) : '' - const theTitle = insertSchemaMacros(namespaceStr + getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, schema.title || '', getXSchemaGroup(schema, module), '', false) + const theTitle = insertSchemaMacros(namespaceStr + getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, { name: schema.title, parent: getXSchemaGroup(schema, module), recursive: false}) const allocatedProxy = event || result const title = schema.type === "object" || schema.enum ? true : false @@ -579,7 +638,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin } } else if (schema.const) { - return (typeof schema.const === 'string' ? `'${schema.const}'` : schema.const) + return insertConstMacros(getTemplate(path.join(templateDir, 'const' + suffix)), schema, module) } else if (schema['x-method']) { const target = JSON.parse(JSON.stringify(module.methods.find(m => m.name === schema['x-method'].split('.').pop()))) @@ -598,7 +657,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin const params = getMethodSignatureParams(target, module, { destination }) const template = getTemplate(path.join(templateDir, 'x-method')) - return insertSchemaMacros(template.replace(/\$\{params\}/g, params), target.result.schema, module, theTitle, '', '', false) + return insertSchemaMacros(template.replace(/\$\{params\}/g, params), target.result.schema, module, { name: theTitle, recursive: false }) } else if (schema.type === 'string' && schema.enum) { let type = expandEnums ? schema.enum.map(e => wrap(e, '\'')).join(' | ') : schema.type @@ -645,16 +704,17 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin // Tuple -> Array if (convertTuplesToArraysOrObjects && isTuple(schema) && isHomogenous(schema)) { template = insertArrayMacros(getTemplate(path.join(templateDir, 'array')), schema, module) - template = insertSchemaMacros(template, firstItem, module, getSchemaType(firstItem, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace }), '', '', false) + template = insertSchemaMacros(template, firstItem, module, { name: getSchemaType(firstItem, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace }), recursive: false }) } // Normal Array else if (!isTuple(schema)) { - template = insertArrayMacros(getTemplate(path.join(templateDir, 'array')), schema, module) - template = insertSchemaMacros(template, schema.items, module, getSchemaType(schema.items, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace }), '', '', true) + const baseDir = (templateDir !== 'json-types' ? 'types': templateDir) + template = insertArrayMacros(getTemplate(path.join(baseDir, 'array')), schema, module) + template = insertSchemaMacros(template, schema.items, module, { name: getSchemaType(schema.items, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace }) }) } else { template = insertTupleMacros(getTemplate(path.join(templateDir, 'tuple')), schema, module, '', { templateDir }) - template = insertSchemaMacros(template, firstItem, module, '', '', '', false) + template = insertSchemaMacros(template, firstItem, module, { recursive: false }) } if (code) { @@ -668,7 +728,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin if (schema.title) { union.title = schema.title } - return getSchemaType(union, module, { destination, link, title, code, asPath, baseUrl, namespace }) + return getSchemaType(union, module, { templateDir, destination, link, title, code, asPath, baseUrl, namespace }) } else if (schema.oneOf || schema.anyOf) { if (!schema.anyOf) { @@ -676,7 +736,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin } // todo... we probably shouldn't allow untitled anyOfs, at least not w/out a feature flag const shape = insertAnyOfMacros(getTemplate(path.join(templateDir, 'anyOf' + suffix)), schema, module, theTitle) - return insertSchemaMacros(shape, schema, module, theTitle, '', '', false) + return insertSchemaMacros(shape, schema, module, { name: theTitle, recursive: false }) // if (event) { @@ -693,19 +753,21 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin else if (schema.type) { const template = getTemplate(path.join(templateDir, 'additionalProperties')) if (schema.additionalProperties && template ) { - return insertSchemaMacros(getTemplate(path.join(templateDir, 'Title')), schema, module, theTitle, '', '', false) + return insertSchemaMacros(getTemplate(path.join(templateDir, 'Title')), schema, module, { name: theTitle, recursive: false}) } else { // TODO: this assumes that when type is an array of types, that it's one other primative & 'null', which isn't necessarily true. const schemaType = !Array.isArray(schema.type) ? schema.type : schema.type.find(t => t !== 'null') - const primitive = getPrimitiveType(schemaType, templateDir) + const baseDir = (templateDir !== 'json-types' ? 'types': templateDir) + const primitive = getPrimitiveType(schemaType, baseDir) const type = allocatedProxy ? allocatedPrimitiveProxies[schemaType] || primitive : primitive return wrap(type, code ? '`' : '') } } else { + const template = getTemplate(path.join(templateDir, 'void')) || 'void' // TODO this is TypeScript specific - return wrap('void', code ? '`' : '') + return wrap(template, code ? '`' : '') } }