-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
165 lines (133 loc) · 4.63 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { readFile, writeFile } from 'fs/promises';
import { JSONSchema7 } from 'json-schema';
import path from 'path';
abstract class CodeGenerator {
dependencies = "";
headerFileContent = "";
sourceFileConctent = "";
writeHeader(value: string) {
this.headerFileContent = this.headerFileContent + value;
}
writeSource(value: string) {
this.sourceFileConctent = this.sourceFileConctent + value;
}
addDependency(ref: string) {
this.dependencies = this.dependencies + `#include "${path.parse(ref).name}.hpp"\n`;
}
abstract addHeaders(): void;
abstract startClass(name: string): void;
abstract defineProperty(name: string, type: string, optional: boolean): void;
abstract endClass(name: string): void;
}
class NlohmannJSONCodeGenerator extends CodeGenerator {
addHeaders(): void {
this.writeHeader(`
#include <cassert>
#include <memory>
#include <nlohmann/json.hpp>
#include "jsonschema2cppview_nlohmann.hpp"
`);
}
startClass(name: string) {
this.writeHeader(`
class ${name} {
const nlohmann::json& value;
public:
${name}(const nlohmann::json& value) : value(value) {}
`);
}
defineProperty(name: string, type: string, optional: boolean) {
if (optional) {
this.writeHeader(`
std::optional<${type}> ${name}() const {
if (value.contains("${name}")) {
return ${type}(value.at("${name}"));
} else {
return std::nullopt;
}
}`);
} else {
this.writeHeader(`
${type} ${name}() const {
assert(value.contains("${name}"));
return value.at("${name}");
}`);
}
}
endClass(_: string) {
this.writeHeader(`};`);
}
}
async function getRefId(ref: string): Promise<string|undefined> {
try {
return JSON.parse(await readFile(ref, { encoding: 'utf8' } ))['$id']
} catch (error) {
console.error(error);
return undefined;
}
}
async function generateCodeForObject(generator: CodeGenerator, schema: JSONSchema7, nameBase?: string): Promise<string|undefined> {
const name = schema.$id || nameBase || 'Temp';
generator.startClass(name);
if (schema.properties) {
for (const property of Object.keys(schema.properties)) {
const propertyDefinition = schema.properties[property];
if (typeof propertyDefinition === 'object') {
const propertyType = await generateCodeForSchema(generator, propertyDefinition, `${property}Type`);
if (propertyType) {
const isRequired = typeof schema.required === 'object' ? schema.required.indexOf(property) !== -1 : false;
generator.defineProperty(property, propertyType, !isRequired);
}
}
}
}
generator.endClass(name);
return name;
}
async function generateCodeForArray(generator: CodeGenerator, schema: JSONSchema7, nameBase?: string): Promise<string|undefined> {
if (!schema.items || Array.isArray(schema.items) || typeof schema.items === 'boolean') {
return "";
}
const itemType = await generateCodeForSchema(generator, schema.items, `${nameBase}Item`);
return `jsonschema2cppview::Array<${itemType}>` || "";
}
async function generateCodeForSchema(generator: CodeGenerator, schema: JSONSchema7, nameBase?: string): Promise<string|undefined> {
switch (schema.type) {
case 'number': return 'double';
case 'boolean': return 'bool';
case 'object': return await generateCodeForObject(generator, schema, nameBase);
case 'string': return 'std::string';
case 'array': return await generateCodeForArray(generator, schema, nameBase);
case 'null': return 'unknown';
case 'integer': return 'std::intmax_t';
}
if (schema.$ref) {
const type = await getRefId(schema.$ref);
generator.addDependency(schema.$ref);
return type;
}
return undefined;
}
async function generateCodeForFile(generator: CodeGenerator, filename: string) {
const document = JSON.parse(await readFile(filename, { encoding: 'utf8' }));
await generateCodeForSchema(generator, document as JSONSchema7);
}
async function main() {
const inputFile = process.argv[2];
const outputDirectory = process.argv[3];
const parsedPath = path.parse(inputFile);
const outputHeaderFilePath = path.join(outputDirectory, `${parsedPath.name}.hpp`);
const outputSourceFilePath = path.join(outputDirectory, `${parsedPath.name}.cpp`);
const generator = new NlohmannJSONCodeGenerator();
generator.addHeaders();
await generateCodeForFile(generator, inputFile);
await writeFile(outputHeaderFilePath, `#pragma once
#include <optional>
#include <string>
${generator.dependencies}
${generator.headerFileContent}`);
await writeFile(outputSourceFilePath, `#include "${parsedPath.name}.hpp"
${generator.sourceFileConctent}`);
}
main()
.catch(error => console.log(error));