Skip to content

Commit

Permalink
Polymorphic reducer types & accessor generation support added
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Jun 6, 2023
1 parent 60f9762 commit c0a4a4c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
33 changes: 29 additions & 4 deletions languages/c/Types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@ const deepMergeAll = (module, name, schema, schemas, options) => {
return union
}

const getPolymorphicReducerParamSchema = (method) => {
let reducedParamSchema = {
name: `${method.name}Params`,
schema: {
type: "array",
items: {
title: `${method.name}Param`,
type: "object",
properties: {}
}
},
required: true
}
method.params.forEach(p => reducedParamSchema.schema.items.properties[p.name] = p)
return reducedParamSchema
}

function getMethodSignature(method, module, { destination, isInterface = false }) {
const extraParam = '${method.result.type}* ${method.result.name}'

Expand Down Expand Up @@ -312,12 +329,12 @@ function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, pref
return structure
}

function getSchemaShape(json, module, { name = '', prefix = '', level = 0, title, summary, descriptions = true, destination = '', section = '', enums = true } = {}) {
function getSchemaShape(json, module, { name = '', prefix = '', type = '', level = 0, title, summary, descriptions = true, destination = '', section = '', enums = true } = {}) {

let shape = getSchemaShapeInfo(json, module, module['x-schemas'], { name, prefix, merged: false, level, title, summary, descriptions, destination, section, enums })
let shape = getSchemaShapeInfo(json, module, module['x-schemas'], { name, prefix, type, merged: false, level, title, summary, descriptions, destination, section, enums })
return shape
}
function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = '', merged = false, level = 0, title, summary, descriptions = true, destination = '', section = '', enums = true } = {}) {
function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = '', type = '', merged = false, level = 0, title, summary, descriptions = true, destination = '', section = '', enums = true } = {}) {
let shape = ''

if (destination && section) {
Expand All @@ -326,6 +343,14 @@ function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = ''
json = JSON.parse(JSON.stringify(json))

name = json.title || name

if (type === 'reducer') {
let recuderParam = getPolymorphicReducerParamSchema(json)
name = recuderParam.name
json = recuderParam.schema
type = ''
}

if (json['$ref']) {
if (json['$ref'][0] === '#') {
//Ref points to local schema
Expand Down Expand Up @@ -492,7 +517,7 @@ function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = ''

if (info.type && info.type.length > 0) {
let type = getArrayElementSchema(json, module, schemas, info.name)
let arrayName = capitalize(info.name) + capitalize(type.type)
let arrayName = capitalize(name) + capitalize(type.type)
let objName = getTypeName(info.namespace, arrayName, prefix)
let tName = objName + 'Array'
let moduleName = info.namespace
Expand Down
24 changes: 14 additions & 10 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import isString from 'crocks/core/isString.js'
import predicates from 'crocks/predicates/index.js'
const { isObject, isArray, propEq, pathSatisfies, propSatisfies } = predicates

import { isRPCOnlyMethod, isProviderInterfaceMethod, getProviderInterface, getPayloadFromEvent, providerHasNoParameters, isTemporalSetMethod, hasMethodAttributes, getMethodAttributes, isEventMethodWithContext, getSemanticVersion, getSetterFor, getProvidedCapabilities, isPolymorphicPullMethod, hasPublicAPIs } from '../shared/modules.mjs'
import { isRPCOnlyMethod, isProviderInterfaceMethod, getProviderInterface, getPayloadFromEvent, providerHasNoParameters, isTemporalSetMethod, hasMethodAttributes, getMethodAttributes, isEventMethodWithContext, getSemanticVersion, getSetterFor, getProvidedCapabilities, isPolymorphicPullMethod, isPolymorphicReducer, hasPublicAPIs } from '../shared/modules.mjs'
import isEmpty from 'crocks/core/isEmpty.js'
import { getLinkedSchemaPaths, getSchemaConstraints, isSchema, localizeDependencies, isDefinitionReferencedBySchema } from '../shared/json-schema.mjs'

Expand Down Expand Up @@ -646,12 +646,11 @@ function generateSchemas(json, templates, options) {

const schemas = (options.section.includes('methods') ? (hasMethodsSchema(json) ? json.methods : '') : (json.definitions || (json.components && json.components.schemas) || {}))

const generate = (name, schema, uri, { prefix = '' } = {}) => {
const generate = (name, schema, uri, { prefix = '', type = '' } = {}) => {
// these are internal schemas used by the firebolt-openrpc tooling, and not meant to be used in code/doc generation
if (['ListenResponse', 'ProviderRequest', 'ProviderResponse', 'FederatedResponse', 'FederatedRequest'].includes(name)) {
return
}

let content = getTemplate('/schemas/default', templates)

if (!schema.examples || schema.examples.length === 0) {
Expand All @@ -667,7 +666,7 @@ function generateSchemas(json, templates, options) {
else {
content = content.replace(/\$\{if\.description\}(.*?)\{end\.if\.description\}/gms, '$1')
}
const schemaShape = types.getSchemaShape(schema, json, { name, prefix, destination: state.destination, section: options.section })
const schemaShape = types.getSchemaShape(schema, json, { name, prefix, type, destination: state.destination, section: options.section })

content = content
.replace(/\$\{schema.title\}/, (schema.title || name))
Expand Down Expand Up @@ -712,13 +711,18 @@ function generateSchemas(json, templates, options) {
}
else if (schema.tags) {
if (!isDeprecatedMethod(schema)) {
schema.params.forEach(param => {
if (param.schema && (param.schema.type === 'object')) {
list.push([param.name, param.schema, '', { prefix : schema.name}])
if (isPolymorphicReducer(schema)) {
list.push([schema.name, schema, '', { type : 'reducer' }])
}
else {
schema.params.forEach(param => {
if (param.schema && (param.schema.type === 'object')) {
list.push([param.name, param.schema, '', { prefix : schema.name }])
}
})
if (schema.result.schema && (schema.result.schema.type === 'object')) {
list.push([schema.result.name, schema.result.schema, '', { prefix : schema.name }])
}
})
if (schema.result.schema && (schema.result.schema.type === 'object')) {
list.push([schema.result.name, schema.result.schema, '', { prefix : schema.name}])
}
}
}
Expand Down

0 comments on commit c0a4a4c

Please sign in to comment.