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: changes to handle context params #161

Merged
merged 1 commit into from
Jan 3, 2024
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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std::optional<${type}> ${property};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${type}
30 changes: 20 additions & 10 deletions languages/cpp/templates/callback-instantiation/generic.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
WPEFramework::Core::JSON::VariantContainer::Iterator elements = proxyResponse->Variants();
while (elements.Next()) {
${callback.result.instantiation}
else if (strcmp(elements.Label(), "context") == 0) {
WPEFramework::Core::JSON::VariantContainer::Iterator params = elements.Current().Object().Variants();
while (params.Next()) {
${callback.param.instantiation.with.indent}
std::string str;
proxyResponse->ToString(str);
WPEFramework::Core::JSON::VariantContainer variantContainer(str);
WPEFramework::Core::JSON::Variant resultVariant;
if (variantContainer.HasLabel("context") == true) {
WPEFramework::Core::JSON::VariantContainer::Iterator elements = variantContainer.Variants();
while (elements.Next()) {
${callback.result.initialization.with.indent}
else if (strcmp(elements.Label(), "context") == 0) {
WPEFramework::Core::JSON::VariantContainer::Iterator params = elements.Current().Object().Variants();
while (params.Next()) {
${callback.param.instantiation.with.indent}
}
} else {
ASSERT(false);
}
} else {
ASSERT(false);
}
}
} else {
resultVariant = variantContainer;
}

${callback.result.instantiation}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
if (strcmp(elements.Label(), "${property}") == 0) {
${property} = elements.Current().Value().c_str();
resultVariant = elements.Current();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
${if.namespace.notsame}${info.Title}::${end.if.namespace.notsame}JsonData_${title}::Iterator elements = resultVariant.Variants();
while (elements.Next()) {
${property}.insert(elements.Label(), elements.Current().${additional.type};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${property};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
auto index(element.Current().Elements());
while (index.Next() == true) {
${property}.push_back(resultVariant.Value());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${property} = WPEFramework::Core::EnumerateType<${if.namespace.notsame}${info.Title}::${end.if.namespace.notsame}${title}>(resultVariant.String().c_str(), false).Value();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${property} = resultVariant.Value();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
WPEFramework::Core::JSON::VariantContainer container = resultVariant.Object();
std::string strContainer;
container.ToString(strContainer);
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} response;
response.FromString(strContainer);
${properties}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${type}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${base.title}.${property} = response.${Property};${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
@@ -0,0 +1 @@
${property} = resultVariant.Value().c_str();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
string ${property}Str;
response${Property.dependency}.${Property}.ToString(${property}Str);
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = ${property}Str;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
${if.impl.array.optional}${base.title}.${property} = std::make_optional<${type}>();${end.if.impl.array.optional}
auto index(response.${Property}.Elements());
while (index.Next() == true) {
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${base.title}.${property}${if.impl.array.optional}.value()${end.if.impl.array.optional}.push_back(index.Current().Value());${end.if.non.object}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = response${Property.dependency}.${Property};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = response${Property.dependency}.${Property};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
${type} ${property}Result${level};
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} jsonResult = index.Current();
${properties}
${base.title}.${property}${if.impl.array.optional}.value()${end.if.impl.array.optional}.push_back(${property}Result${level});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
${if.impl.optional} ${base.title}${property.dependency} = std::make_optional<${type}>();${end.if.impl.optional}
{
${properties}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = response${Property.dependency}.${Property};${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
@@ -0,0 +1 @@
${shape}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
${property}.first = resultVariant.Get(0);
${property}.second = resultVariant.Get(1);
2 changes: 1 addition & 1 deletion languages/cpp/templates/declarations/event.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ${method.name} - ${method.description} */
struct I${method.Name}Notification {
virtual void ${method.rpc.name}( ${event.signature.callback.params}${if.event.params}, ${end.if.event.params}${event.result.type} ) = 0;
virtual void ${method.rpc.name}( ${event.signature.callback.params}${if.event.callback.params}, ${end.if.event.callback.params}${event.result.type} ) = 0;
};
// signature callback params: ${event.signature.callback.params}
// method result properties : ${method.result.properties}
Expand Down
26 changes: 18 additions & 8 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ const isPropertyMethod = (m) => {
return hasTag(m, 'property') || hasTag(m, 'property:immutable') || hasTag(m, 'property:readonly')
}

const eventHasOptionalParam = (event) => {
return event.params.length && event.params.find(param => !(param.required && param.required === true))
}

const isOptionalParam = (param) => {
return (!(param.required && param.required === true))
}

// Pick methods that call RCP out of the methods array
const rpcMethodsOrEmptyArray = compose(
option([]),
Expand Down Expand Up @@ -1297,24 +1305,26 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
const resultInit = types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-initialization', property: flattenedMethod.result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) // w/out primitive: true, 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, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join('\n') : ''
// this was wrong... check when we merge if it was fixed
const callbackSerializedList = event ? types.getSchemaShape(event.result.schema, json, { templateDir: event.params && event.params.length && !event.tags.find(t => t.name === 'provider') ? 'callback-serialization' : 'callback-result-serialization', property: result.name, required: event.result.schema.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : ''
const callbackInitialization = event ? ((event.params && event.params.length && !event.tags.find(t => t.name === 'provider') ? (event.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'callback-initialization', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join('\n')) + '\n' : '' ) + (types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-initialization', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }))) : ''
const callbackSerializedList = event ? types.getSchemaShape(event.result.schema, json, { templateDir: eventHasOptionalParam(event) && !event.tags.find(t => t.name === 'provider') ? 'callback-serialization' : 'callback-result-serialization', property: result.name, required: event.result.schema.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : ''
const callbackInitialization = event ? (eventHasOptionalParam(event) && !event.tags.find(t => t.name === 'provider') ? (event.params.map(param => isOptionalParam(param) ? types.getSchemaShape(param.schema, json, { templateDir: 'callback-initialization-optional', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '').filter(param => param).join('\n') + '\n') : '' ) + (types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-initialization', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })) : ''
let callbackInstantiation = ''
if (event) {
if (event.params && event.params.length && !event.tags.find(t => t.name === 'provider')) {
if (eventHasOptionalParam(event) && !event.tags.find(t => t.name === 'provider')) {
callbackInstantiation = types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })
let paramInstantiation = event.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'callback-context-instantiation', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join('\n')
let resultInstantiation = types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-context-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })
let paramInstantiation = event.params.map(param => isOptionalParam(param) ? types.getSchemaShape(param.schema, json, { templateDir: 'callback-context-instantiation', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '').filter(param => param).join('\n')
let resultInitialization = types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-value-initialization', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })
let resultInstantiation = types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-value-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })
callbackInstantiation = callbackInstantiation
.replace(/\$\{callback\.param\.instantiation\.with\.indent\}/g, indent(paramInstantiation, ' ', 2))
.replace(/\$\{callback\.param\.instantiation\.with\.indent\}/g, indent(paramInstantiation, ' ', 3))
.replace(/\$\{callback\.result\.initialization\.with\.indent\}/g, indent(resultInitialization, ' ', 1))
.replace(/\$\{callback\.result\.instantiation\}/g, resultInstantiation)
}
else {
callbackInstantiation = types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-result-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })
}
}
// hmm... how is this different from callbackSerializedList? i guess they get merged?
const callbackResponseInst = event ? (event.params && event.params.length ? (event.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'callback-response-instantiation', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join(', ')) + ', ' : '' ) + (types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-response-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })) : ''
const callbackResponseInst = event ? (eventHasOptionalParam(event) ? (event.params.map(param => isOptionalParam(param) ? types.getSchemaShape(param.schema, json, { templateDir: 'callback-response-instantiation', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '').filter(param => param).join(', ') + ', ') : '' ) + (types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-response-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })) : ''
const resultType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: state.typeTemplateDir }) : ''
const resultSchemaType = result.schema.type
const resultJsonType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: 'json-types' }) : ''
Expand Down Expand Up @@ -1345,7 +1355,6 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
}

template = insertExampleMacros(template, examples[methodObj.name] || [], methodObj, json, templates)

template = template.replace(/\$\{method\.name\}/g, method.name)
.replace(/\$\{method\.rpc\.name\}/g, methodObj.rpc_name || methodObj.name)
.replace(/\$\{method\.summary\}/g, methodObj.summary)
Expand Down Expand Up @@ -1381,6 +1390,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
.replace(/\$\{event\.params\}/g, eventParams)
.replace(/\$\{event\.params\.table\.rows\}/g, eventParamsRows)
.replace(/\$\{if\.event\.params\}(.*?)\$\{end\.if\.event\.params\}/gms, event && event.params.length ? '$1' : '')
.replace(/\$\{if\.event\.callback\.params\}(.*?)\$\{end\.if\.event\.callback\.params\}/gms, event && eventHasOptionalParam(event) ? '$1' : '')
.replace(/\$\{event\.signature\.params\}/g, event ? types.getMethodSignatureParams(event, json, { destination: state.destination, section: state.section }) : '')
.replace(/\$\{event\.signature\.callback\.params\}/g, event ? types.getMethodSignatureParams(event, json, { destination: state.destination, section: state.section, callback: true }) : '')
.replace(/\$\{event\.params\.serialization\}/g, serializedEventParams)
Expand Down
Loading
Loading