From 4a637898fd43b60e7e8958ad8cb5b0bde12daa7a Mon Sep 17 00:00:00 2001 From: HaseenaSainul <41037131+HaseenaSainul@users.noreply.github.com> Date: Tue, 19 Dec 2023 20:50:19 +0530 Subject: [PATCH] CPPSDK: handle patternProperties (#160) --- .../patternProperties.cpp | 9 +++ src/macrofier/types.mjs | 59 ++++++++++++++++--- 2 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 languages/cpp/templates/parameter-serialization/patternProperties.cpp diff --git a/languages/cpp/templates/parameter-serialization/patternProperties.cpp b/languages/cpp/templates/parameter-serialization/patternProperties.cpp new file mode 100644 index 00000000..9346bb6b --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/patternProperties.cpp @@ -0,0 +1,9 @@ + ${if.impl.optional}if (${property}.has_value()) { + WPEFramework::Core::JSON::VariantContainer ${property}Container; + ${property}Container.FromString(${property}.value()); + WPEFramework::Core::JSON::Variant ${property}Variant(${property}Container); + jsonParameters.Set(_T("${property}"), ${property}Variant); + }${end.if.impl.optional}${if.impl.non.optional}WPEFramework::Core::JSON::VariantContainer ${property}Container; + ${property}Container.FromString(${property}); + WPEFramework::Core::JSON::Variant ${property}Variant(${property}Container); + jsonParameters.Set(_T("${property}"), ${property}Variant);${end.if.impl.non.optional} diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index 435db418..1d2378c0 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -267,6 +267,36 @@ const insertObjectAdditionalPropertiesMacros = (content, schema, module, title, return content } +const insertObjectPatternPropertiesMacros = (content, schema, module, title, options) => { + const options2 = options ? JSON.parse(JSON.stringify(options)) : {} + options2.parent = title + options2.level = options.level + 1 + options2.required = options.required + + let patternSchema; + Object.entries(schema.patternProperties).forEach(([pattern, sch]) => { + patternSchema = sch + }) + + if (patternSchema) { + const shape = getSchemaShape(patternSchema, module, options2) + let type = getSchemaType(patternSchema, module, options2).trimEnd() + const propertyNames = localizeDependencies(schema, module).propertyNames + + content = content + .replace(/\$\{shape\}/g, shape) + .replace(/\$\{parent\.title\}/g, title) + .replace(/\$\{title\}/g, title) + .replace(/\$\{type\}/g, type) + .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, '') + .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/g, '') + .replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/g, options.required ? '' : '$1') + .replace(/\$\{if\.impl.non.optional\}(.*?)\$\{end\.if\.impl.non.optional\}/g, options.required ? '$1' : '') + } + + return content +} + const getIndents = level => level ? ' ' : '' const insertObjectMacros = (content, schema, module, title, property, options) => { const options2 = options ? JSON.parse(JSON.stringify(options)) : {} @@ -526,8 +556,13 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren shape = insertObjectAdditionalPropertiesMacros(additionalPropertiesTemplate, schema, module, theTitle, { level, parent, templateDir, namespace: true, required }) } else { - 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, primitive }) + const patternPropertiesTemplate = getTemplate(path.join(templateDir, 'patternProperties')) + if (patternPropertiesTemplate && schema.patternProperties) { + shape = insertObjectPatternPropertiesMacros(patternPropertiesTemplate, schema, module, theTitle, { level, parent, templateDir, namespace: true, required }) + } else { + 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, primitive }) + } } result = result.replace(/\$\{shape\}/g, shape) if (level === 0) { @@ -806,17 +841,23 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin // } } else if (schema.type) { - const template = getTemplate(path.join(templateDir, 'additionalProperties')) + let template = getTemplate(path.join(templateDir, 'additionalProperties')) if (schema.additionalProperties && template ) { 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 baseDir = (templateDir !== 'json-types' ? 'types': templateDir) - const primitive = getPrimitiveType(schemaType, baseDir) - const type = allocatedProxy ? allocatedPrimitiveProxies[schemaType] || primitive : primitive - return wrap(type, code ? '`' : '') + template = getTemplate(path.join(templateDir, 'patternProperties')) + if (schema.paternProperties && template ) { + 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 baseDir = (templateDir !== 'json-types' ? 'types': templateDir) + const primitive = getPrimitiveType(schemaType, baseDir) + const type = allocatedProxy ? allocatedPrimitiveProxies[schemaType] || primitive : primitive + return wrap(type, code ? '`' : '') + } } } else {