Skip to content
Open
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
9 changes: 2 additions & 7 deletions languages/c/src/types/NativeHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const getNativeType = (json, fireboltString = false) => {
}
else if (jsonType === 'null' ) {
type = 'void'
}
}
return type
}

Expand Down Expand Up @@ -125,18 +125,13 @@ const getArrayAccessors = (arrayName, propertyType, valueType) => {
return res
}

const enumValue = (val,prefix) => {
const keyName = getSafeEnumKeyName(val)
return ` ${prefix.toUpperCase()}_${keyName.toUpperCase()}`
}

const generateEnum = (schema, prefix)=> {
if (!schema.enum) {
return ''
}
else {
let str = `typedef enum {\n`
str += schema.enum.map(e => enumValue(e, prefix)).join(',\n')
str += schema.enum.map(e => getSafeEnumKeyName(val, prefix)).join(',\n')
str += `\n} ${prefix};\n`
return str
}
Expand Down
1 change: 1 addition & 0 deletions languages/cpp/templates/callback-initialization/tuple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${if.namespace.notsame}${parent.Title}::${end.if.namespace.notsame}${title} ${property};
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
}
}${end.if.optional}${if.non.optional}auto index((*proxyResponse)${Property.dependency}.${Property}.Elements());
while (index.Next() == true) {
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.value().push_back(index.Current().Value());${end.if.non.object}
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.push_back(index.Current().Value());${end.if.non.object}
}${end.if.non.optional}
4 changes: 2 additions & 2 deletions languages/cpp/templates/sdk/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ GetVersion()
Version=${array[2]}
}

Version=0.0
GetVersion
Version=1.3.0-next.1
#GetVersion
ReleaseName=firebolt-${ModuleName}-native-sdk-${Version}
ReleasePath=${InstallPath}/${ReleaseName}

Expand Down
6 changes: 3 additions & 3 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ const promoteAndNameSubSchemas = (obj) => {
}
})
if (isSubSchema(method.result.schema)) {
addContentDescriptorSubSchema(method.result, '', obj)
addContentDescriptorSubSchema(method.result, '', obj)
}
else if (isEventMethod(method) && isSubSchema(getPayloadFromEvent(method))) {
// TODO: the `1` below is brittle... should find the index of the non-ListenResponse schema
Expand Down Expand Up @@ -1319,7 +1319,7 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa
const pullsForParamType = pullsParams ? types.getSchemaType(pullsParams, json, { destination: state.destination, section: state.section }) : ''
const pullsForJsonType = pullsResult ? types.getSchemaType(pullsResult, json, { templateDir: 'json-types' }) : ''
const pullsForParamJsonType = pullsParams ? types.getSchemaType(pullsParams, json, { templateDir: 'json-types' }) : ''

const pullsEventParamName = event ? types.getSchemaInstantiation(event.result, json, event.name, { instantiationType: 'pull.param.name' }) : ''

let seeAlso = ''
Expand Down Expand Up @@ -1621,7 +1621,7 @@ function generateResultParams(result, json, templates, { name = '' } = {}) {
return Object.entries(result.properties).map( ([name, type]) => template
.replace(/\$\{method\.param\.name\}/g, name)
.replace(/\$\{method\.param\.type\}/g, types.getSchemaType(type, json, { moduleTitle: moduleTitle, result: true, namespace: true}))
).join(', ') // most languages separate params w/ a comma, so leaving this here for now
).join(', ') // most languages separate params w/ a comma, so leaving this here for now
}
// tuples get unwrapped
else if (config.unwrapResultObjects && result.type && result.type === 'array' && Array.isArray(result.items)) {
Expand Down
14 changes: 7 additions & 7 deletions src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const macrofy = async (

const outputFiles = Object.fromEntries(Object.entries(await readFiles( staticCodeList, staticContent))
.map( ([n, v]) => [path.join(output, n), v]))

let primaryOutput = []

Object.keys(templates).forEach(file => {
Expand Down Expand Up @@ -231,7 +231,7 @@ const macrofy = async (

if (!checked.includes(entry)) {
imports = importedFiles(code, base)
checked.push(entry)
checked.push(entry)
}

imports = imports.map(imp => Array.from(new Set([imp, ...treeShake(imp, path.dirname(imp).substring(output.length), checked)]))).flat()
Expand All @@ -255,7 +255,7 @@ const macrofy = async (
// Grab all schema groups w/ a URI string. These came from some external json-schema that was bundled into the OpenRPC
const externalSchemas = {}
openrpc['x-schemas']
&& Object.entries(openrpc['x-schemas']).forEach(([name, schema]) => {
&& Object.entries(openrpc['x-schemas']).forEach(([name, schema]) => {
if (schema.uri) {
const id = schema.uri
externalSchemas[id] = externalSchemas[id] || { $id: id, info: {title: name }, methods: []}
Expand Down Expand Up @@ -289,19 +289,19 @@ const macrofy = async (
}
})
})

// Output any schema templates for each bundled external schema document
Object.values(externalSchemas).forEach( document => {
if (templatesPerSchema) {
templatesPerSchema.forEach( t => {
const macros = engine.generateMacros(document, templates, exampleTemplates, {hideExcluded: hideExcluded, createPolymorphicMethods: createPolymorphicMethods, destination: t})
let content = getTemplate('/schemas', t, templates)

// NOTE: whichever insert is called first also needs to be called again last, so each phase can insert recursive macros from the other
content = engine.insertMacros(content, macros)

const location = createModuleDirectories ? path.join(output, document.info.title, t) : path.join(output, t.replace(/module/, document.info.title.toLowerCase()).replace(/index/, document.info.title))

outputFiles[location] = content
logSuccess(`Generated macros for schema ${path.relative(output, location)}`)
})
Expand Down
12 changes: 6 additions & 6 deletions src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ const insertEnumMacros = (content, schema, module, name, suffix, templateDir = "
schema.enum.map(value => {
if (!value) {
value = getTemplate(path.join(templateDir, 'unset' + suffix))
}
value ? values.push(template[i].replace(/\$\{key\}/g, getSafeEnumKeyName(value))
}
value ? values.push(template[i].replace(/\$\{key\}/g, getSafeEnumKeyName(value, schema.enumKeyPrefix))
.replace(/\$\{value\}/g, value)) : ''
})
template[i] = values.map((value, id) => {
Expand Down Expand Up @@ -360,7 +360,7 @@ const insertObjectMacros = (content, schema, module, title, property, options) =
.replace(/\$\{if\.base\.optional\}(.*?)\$\{end\.if\.base\.optional\}/gms, options.required ? '' : '$1')
.replace(/\$\{if\.non\.object\}(.*?)\$\{end\.if\.non\.object\}/gms, isObject(localizedProp) ? '' : '$1')
.replace(/\$\{if\.non\.array\}(.*?)\$\{end\.if\.non\.array\}/gms, (localizedProp.type === 'array') ? '' : '$1')
.replace(/\$\{if\.non\.anyOf\}(.*?)\$\{end\.if\.non\.anyOf\}/gms, (localizedProp.anyOf || localizedProp.anyOneOf) ? '' : '$1')
.replace(/\$\{if\.non\.anyOf\}(.*?)\$\{end\.if\.non\.anyOf\}/gms, (localizedProp.anyOf || localizedProp.oneOf) ? '' : '$1')
.replace(/\$\{if\.non\.const\}(.*?)\$\{end\.if\.non\.const\}/gms, (typeof localizedProp.const === 'string') ? '' : '$1')
let baseTitle = options.property

Expand Down Expand Up @@ -543,7 +543,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren
const suffix = destination && ('.' + destination.split('.').pop()) || ''
const theTitle = insertSchemaMacros(getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, { name: schema.title, parent, property, required, recursive: false })

let result = getTemplate(path.join(templateDir, 'default' + suffix)) || '${shape}'
let result = 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"))) {
Expand Down Expand Up @@ -724,7 +724,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin
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.anyOf || schema.oneOf || Array.isArray(schema.type) && schema.type.includes("object") || schema.enum ? true : false
const title = schema.type === "object" || Array.isArray(schema.type) && schema.type.includes("object") || schema.enum ? true : false

if (schema['$ref']) {
if (schema['$ref'][0] === '#') {
Expand Down Expand Up @@ -852,7 +852,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin
const shape = insertAnyOfMacros(getTemplate(path.join(templateDir, 'anyOf' + suffix)), schema, module, theTitle)
return insertSchemaMacros(shape, schema, module, { name: theTitle, recursive: false })


// if (event) {
// return getSchemaType((schema.oneOf || schema.anyOf)[0], module, { destination, link, title, code, asPath, baseUrl })
// }
Expand Down
41 changes: 24 additions & 17 deletions src/shared/json-schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const objectPaths = obj => {
return isObject(value) ?
product.concat(paths(value, fullPath))
: product.concat(fullPath)
}, []) : []
}, []) : []
}
return paths(obj);
}
Expand Down Expand Up @@ -100,7 +100,7 @@ const replaceUri = (existing, replacement, schema) => {
Object.keys(schema).forEach(key => {
replaceUri(existing, replacement, schema[key])
})
}
}
}

const replaceRef = (existing, replacement, schema) => {
Expand Down Expand Up @@ -244,7 +244,7 @@ function getSchemaConstraints(schema, module, options = { delimiter: '\n' }) {
typeof schema.exclusiveMinimum === 'number' ? constraints.push(`exclusiveMinimum: ${schema.exclusiveMinimum}`) : null
typeof schema.multipleOf === 'number' ? constraints.push(`multipleOf: ${schema.multipleOf}`) : null

return constraints.join(options.delimiter)
return constraints.join(options.delimiter)
}
else if (schema.type === 'array' && schema.items) {
let constraints = []
Expand All @@ -256,7 +256,7 @@ function getSchemaConstraints(schema, module, options = { delimiter: '\n' }) {
constraints = [getSchemaConstraints(schema.items, module, options)]
}

return constraints.join(options.delimiter)
return constraints.join(options.delimiter)
}
else if (schema.oneOf || schema.anyOf) {
return '' //See OpenRPC Schema for `oneOf` and `anyOf` details'
Expand Down Expand Up @@ -296,7 +296,7 @@ const localizeDependencies = (json, document, schemas = {}, options = defaultLoc
if (!options.externalOnly) {
while (refs.length > 0) {
for (let i=0; i<refs.length; i++) {
let path = refs[i]
let path = refs[i]
const ref = getPathOr(null, path, definition)
path.pop() // drop ref
if (refToPath(ref).length > 1) {
Expand All @@ -309,7 +309,7 @@ const localizeDependencies = (json, document, schemas = {}, options = defaultLoc
resolvedSchema = { "$REF": ref}
unresolvedRefs.push([...path])
}

if (path.length) {
// don't loose examples from original object w/ $ref
// todo: should we preserve other things, like title?
Expand All @@ -320,22 +320,22 @@ const localizeDependencies = (json, document, schemas = {}, options = defaultLoc
else {
delete definition['$ref']
Object.assign(definition, resolvedSchema)
}
}
}
}
refs = getLocalSchemaPaths(definition)
}
}

refs = getExternalSchemaPaths(definition)
while (refs.length > 0) {
for (let i=0; i<refs.length; i++) {
let path = refs[i]
let path = refs[i]
const ref = getPathOr(null, path, definition)

path.pop() // drop ref
let resolvedSchema

if (!resolvedSchema) {
resolvedSchema = { "$REF": ref}
unresolvedRefs.push([...path])
Expand Down Expand Up @@ -494,12 +494,19 @@ function mergeOneOf(schema) {
return union(schema.oneOf)
}

const getSafeEnumKeyName = (value) => value.split(':').pop() // use last portion of urn:style:values
.replace(/[\.\-]/g, '_') // replace dots and dashes
.replace(/\+/g, '_plus') // change + to _plus
.replace(/([a-z])([A-Z0-9])/g, '$1_$2') // camel -> snake case
.replace(/^([0-9]+(\.[0-9]+)?)/, 'v$1') // insert `v` in front of things that look like version numbers
.toUpperCase()
const getSafeEnumKeyName = function(value, keyPrefix = '') {
if (keyPrefix != '') {
value = keyPrefix + '_' + value
}

let key = value.split(':').pop() // use last portion of urn:style:values
.replace(/\+/g, '_plus') // change + to _plus
.replace(/[\.\-\/\;]/g, '_') // replace special characters
.replace(/([a-z])([A-Z0-9])/g, '$1_$2') // camel -> snake case
.replace(/^([0-9]+\_([0-9]+)?)/, 'v$1') // insert `v` in front of things that look like version numbers

return key.toUpperCase()
}

export {
getSchemaConstraints,
Expand All @@ -520,4 +527,4 @@ export {
removeIgnoredAdditionalItems,
mergeAnyOf,
mergeOneOf
}
}
Loading