Skip to content

Commit 27fe5d1

Browse files
committed
move schema to json, use function for type mapping
1 parent f9c1321 commit 27fe5d1

File tree

3 files changed

+57
-54
lines changed

3 files changed

+57
-54
lines changed

src/generators/node-config-schema/constants.mjs

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ export const ERRORS = {
33
missingCCandHFiles:
44
'Both node_options.cc and node_options.h must be provided.',
55
headerTypeNotFound:
6-
'Header type for "{{headerKey}}" not found in the header file.',
7-
missingTypeDefinition:
8-
'No type definition found for header type "{{headerType}}" in TYPE_DEFINITION_MAP.',
6+
'A type for "{{headerKey}}" not found in the header file.',
7+
missingTypeDefinition: 'No type schema found for "{{type}}".',
98
};
109

1110
// Regex pattern to match calls to the AddOption function.
@@ -14,38 +13,3 @@ export const ADD_OPTION_REGEX =
1413

1514
// Regex pattern to match header keys in the Options class.
1615
export const OPTION_HEADER_KEY_REGEX = /Options::(\w+)/;
17-
18-
// Basic JSON schema for node.config.json
19-
export const BASIC_SCHEMA = {
20-
$schema: 'https://json-schema.org/draft/2020-12/schema',
21-
additionalProperties: false,
22-
properties: {
23-
$schema: {
24-
type: 'string',
25-
},
26-
nodeOptions: {
27-
additionalProperties: false,
28-
properties: {},
29-
type: 'object',
30-
},
31-
},
32-
type: 'object',
33-
};
34-
35-
// Schema Definition Map for Data Types
36-
export const TYPE_DEFINITION_MAP = {
37-
'std::vector<std::string>': {
38-
oneOf: [
39-
{ type: 'string' }, // Single string case
40-
{
41-
items: { type: 'string', minItems: 1 }, // Array of strings, ensuring at least one item
42-
type: 'array',
43-
},
44-
],
45-
},
46-
uint64_t: { type: 'number' }, // 64-bit unsigned integer maps to a number
47-
int64_t: { type: 'number' }, // 64-bit signed integer maps to a number
48-
HostPort: { type: 'number' }, // HostPort is a number, like 4000
49-
'std::string': { type: 'string' }, // String type
50-
bool: { type: 'boolean' }, // Boolean type
51-
};

src/generators/node-config-schema/index.mjs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { readFile, writeFile } from 'node:fs/promises';
22
import {
33
ERRORS,
44
ADD_OPTION_REGEX,
5-
BASIC_SCHEMA,
65
OPTION_HEADER_KEY_REGEX,
7-
TYPE_DEFINITION_MAP,
86
} from './constants.mjs';
97
import { join } from 'node:path';
108

@@ -48,10 +46,10 @@ export default {
4846
// Read the contents of the cc and h files
4947
const ccContent = await readFile(ccFile, 'utf-8');
5048
const hContent = await readFile(hFile, 'utf-8');
49+
const schema = JSON.parse(
50+
await readFile(new URL('./schema.json', import.meta.url))
51+
);
5152

52-
// Clone the BASIC_SCHEMA to avoid mutating the original schema object
53-
/** @type {typeof BASIC_SCHEMA} */
54-
const schema = Object.assign({}, BASIC_SCHEMA);
5553
const { nodeOptions } = schema.properties;
5654

5755
// Process the cc content and match AddOption calls
@@ -78,18 +76,10 @@ export default {
7876
);
7977
}
8078

81-
const headerType = headerTypeMatch[1].trim();
82-
83-
// Ensure the headerType exists in the TYPE_DEFINITION_MAP
84-
const typeDefinition = TYPE_DEFINITION_MAP[headerType];
85-
if (!typeDefinition) {
86-
throw new Error(
87-
formatErrorMessage(ERRORS.missingTypeDefinition, { headerType })
88-
);
89-
}
90-
9179
// Add the option to the schema after removing the '--' prefix
92-
nodeOptions.properties[option.replace('--', '')] = typeDefinition;
80+
nodeOptions.properties[option.replace('--', '')] = getTypeSchema(
81+
headerTypeMatch[1].trim()
82+
);
9383
}
9484

9585
nodeOptions.properties = Object.fromEntries(
@@ -116,3 +106,37 @@ export default {
116106
function formatErrorMessage(message, params) {
117107
return message.replace(/{{(\w+)}}/g, (_, key) => params[key] || `{{${key}}}`);
118108
}
109+
110+
/**
111+
* Returns the JSON Schema definition for a given C++ type.
112+
*
113+
* @param {string} type - The type to get the schema for.
114+
* @returns {object} JSON Schema definition for the given type.
115+
*/
116+
function getTypeSchema(type) {
117+
switch (type) {
118+
case 'std::vector<std::string>':
119+
return {
120+
oneOf: [
121+
{ type: 'string' },
122+
{
123+
type: 'array',
124+
items: { type: 'string' },
125+
minItems: 1,
126+
},
127+
],
128+
};
129+
case 'uint64_t':
130+
case 'int64_t':
131+
case 'HostPort':
132+
return { type: 'number' };
133+
case 'std::string':
134+
return { type: 'string' };
135+
case 'bool':
136+
return { type: 'boolean' };
137+
default:
138+
throw new Error(
139+
formatErrorMessage(ERRORS.missingTypeDefinition, { type })
140+
);
141+
}
142+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"additionalProperties": false,
4+
"properties": {
5+
"$schema": {
6+
"type": "string"
7+
},
8+
"nodeOptions": {
9+
"additionalProperties": false,
10+
"properties": {},
11+
"type": "object"
12+
}
13+
},
14+
"type": "object"
15+
}

0 commit comments

Comments
 (0)