Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPPSDK: template updates based on more complex schema generation #153

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions languages/cpp/templates/parameter-serialization/array.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WPEFramework::Core::JSON::ArrayType<WPEFramework::Core::JSON::Variant> ${Property};
WPEFramework::Core::JSON::ArrayType<WPEFramework::Core::JSON::Variant> ${property}Array;
for (auto& element : ${property}) {
${if.object}${items.with.indent}${end.if.object}${if.non.object}${Property}.Add() = element;${end.if.non.object}
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}Array.Add() = element;${end.if.non.object}
}
WPEFramework::Core::JSON::Variant ${Property}Variant;
${Property}Variant.Array(${Property});
jsonParameters.Set(_T("${property}"), ${Property}Variant);
WPEFramework::Core::JSON::Variant ${property}Variant;
${property}Variant.Array(${property}Array);
jsonParameters.Set(_T("${property}"), ${property}Variant);
4 changes: 2 additions & 2 deletions languages/cpp/templates/parameter-serialization/enum.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +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});
WPEFramework::Core::JSON::Variant ${property}Variant(jsonValue.Data());
jsonParameters.Set(_T("${property}"), ${property}Variant);
4 changes: 2 additions & 2 deletions languages/cpp/templates/parameter-serialization/generic.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
WPEFramework::Core::JSON::Variant ${Property}(${property}${if.optional}.value()${end.if.optional});
jsonParameters.Set(_T("${property}"), ${Property});
WPEFramework::Core::JSON::Variant ${property}Variant(${property}${if.optional}.value()${end.if.optional});
jsonParameters.Set(_T("${property}"), ${property}Variant);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${if.namespace.notsame}${parent.Title}::${end.if.namespace.notsame}
12 changes: 6 additions & 6 deletions languages/cpp/templates/parameter-serialization/object-array.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} ${Property}Container;
${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;
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}Array.Add() = ${property}Variant;
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
WPEFramework::Core::JSON::Variant ${Property}(${property}${if.optional}.value()${end.if.optional});
jsonParameters.Set(_T("${property}"), ${Property});
WPEFramework::Core::JSON::Variant ${property}Variant(${property}${if.optional}.value()${end.if.optional});
jsonParameters.Set(_T("${property}"), ${property}Variant);
12 changes: 6 additions & 6 deletions languages/cpp/templates/parameter-serialization/object.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
auto element = ${property}${if.impl.optional}.value()${end.if.impl.optional};
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} ${Property}Container;
${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});
string ${property}Str;
${property}Container.ToString(${property}Str);
WPEFramework::Core::JSON::VariantContainer ${property}VariantContainer(${property}Str);
WPEFramework::Core::JSON::Variant ${property}Variant = ${property}VariantContainer;
jsonParameters.Set(_T("${property}"), ${property}Variant);
Original file line number Diff line number Diff line change
@@ -1 +1 @@
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${base.Title}Container.${Property} = element.${property}${if.optional}.value()${end.if.optional};${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${base.title}Container.${Property} = element.${property}${if.optional}.value()${end.if.optional};${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
${base.Title}Container.${Property.dependency}${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}${if.optional}.value()${end.if.optional};
${base.title}Container.${Property.dependency}${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}${if.optional}.value()${end.if.optional};
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
WPEFramework::Core::JSON::ArrayType<WPEFramework::Core::JSON::Variant> ${Property};
${type} ${property} = element.${property}${if.impl.array.optional}.value()${end.if.impl.array.optional};
WPEFramework::Core::JSON::ArrayType<${json.type}> ${property}Array;
${type} ${property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}${if.impl.array.optional}.value()${end.if.impl.array.optional};
for (auto& element : ${property}) {
${if.object}${items.with.indent}${end.if.object}${if.non.object}${Property}.Add() = element;${end.if.non.object}
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}Array.Add() = element;${end.if.non.object}
}
WPEFramework::Core::JSON::Variant ${Property}Variant;
${Property}Variant.Array(${Property});
jsonParameters.Set(_T("${property}"), ${Property}Variant);
${base.title}Container.${Property.dependency}Add(_T("${property}"), &${property}Array);
Original file line number Diff line number Diff line change
@@ -1 +1 @@
${base.Title}Container.${Property.dependency}${Property} = element${property.dependency}.${property}${if.optional}.value()${end.if.optional};
${base.title}Container.${Property.dependency}${Property} = element${property.dependency}.${property}${if.optional}.value()${end.if.optional};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${if.namespace.notsame}${parent.Title}::${end.if.namespace.notsame}
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} ${Property}Container;
${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;
${property}Array.Add() = ${property}Container;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${base.Title}Container.${Property.dependency}${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}${if.optional}.value()${end.if.optional};${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${base.title}Container.${Property.dependency}${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}${if.optional}.value()${end.if.optional};${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
WPEFramework::Core::JSON::VariantContainer::Iterator variants = jsonResult.Variants();
WPEFramework::Core::JSON::VariantContainer::Iterator variants = jsonResult.Variants();
while (variants.Next()) {
${if.not.default} ${namespace}${key} key = WPEFramework::Core::EnumerateType<${namespace}${key}>(variants.Label(), false).Value();${end.if.not.default}${if.default} ${key} key = variants.Label();${end.if.default}
${property}.emplace(std::piecewise_construct,
Expand Down
2 changes: 1 addition & 1 deletion languages/cpp/templates/types/anyOfSchemaShape.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/* anyOf schema shape is not supported right now */
/* anyOf schema shape is not supported right now */
15 changes: 9 additions & 6 deletions src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ const getTemplate = (name) => {
const getXSchemaGroupFromProperties = (schema, title, properties, group) => {
if (properties) {
Object.entries(properties).forEach(([name, prop]) => {
if (schema.title === prop.title) {
if ((schema.title === prop.title) || (prop.items && prop.items.title === schema.title)) {
group = title
} else {
}
else {
group = getXSchemaGroupFromProperties(schema, title, prop.properties, group)
}
})
Expand Down Expand Up @@ -173,7 +174,7 @@ function insertSchemaMacros(content, schema, module, { name = '', parent = '', p
.replace(/\$\{if\.namespace\.notsame}(.*?)\$\{end\.if\.namespace\.notsame\}/g, (module.info.title !== (parent || moduleTitle)) ? '$1' : '')
.replace(/\$\{parent\.title\}/g, parent || moduleTitle)
.replace(/\$\{parent\.Title\}/g, capitalize(parent || moduleTitle))
.replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, required ? '' : '$1')
.replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, (Array.isArray(required) ? required.includes(property) : required) ? '' : '$1')
.replace(/\$\{description\}/g, schema.description ? schema.description : '')
.replace(/\$\{summary\}/g, schema.description ? schema.description.split('\n')[0] : '')
.replace(/\$\{name\}/g, title)
Expand Down Expand Up @@ -282,7 +283,7 @@ const insertObjectMacros = (content, schema, module, title, property, options) =
} else {
options2.property = options.property
}
options2.required = schema.required && schema.required.includes(name)
options2.required = schema.required
const schemaShape = getSchemaShape(localizedProp, module, options2)
const type = getSchemaType(localizedProp, module, options2)
// don't push properties w/ unsupported types
Expand Down Expand Up @@ -517,7 +518,9 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren
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, primitive })
}
result = result.replace(/\$\{shape\}/g, shape)
result = result.replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, required ? '' : '$1')
if (level === 0) {
result = result.replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, (Array.isArray(required) ? required.includes(property) : required) ? '' : '$1')
}
return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required, templateDir })
}
else if (schema.anyOf || schema.oneOf) {
Expand Down Expand Up @@ -574,7 +577,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren
else if (schema.type === "array" && schema.items && !Array.isArray(schema.items)) {
// array
const items = getSchemaShape(schema.items, module, { templateDir, parent, property, required, parentLevel: parentLevel + 1, level, summary, descriptions, destination, enums: false, array: true, primitive })
const shape = insertArrayMacros(getTemplate(path.join(templateDir, 'array' + suffix)) || genericTemplate, schema, module, level, items, schema.required)
const shape = insertArrayMacros(getTemplate(path.join(templateDir, 'array' + suffix)) || genericTemplate, schema, module, level, items, Array.isArray(required) ? required.includes(property) : required)
result = result.replace(/\$\{shape\}/g, shape)
.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' : '')
Expand Down
Loading