-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec-parse.ts
More file actions
244 lines (208 loc) · 7.81 KB
/
spec-parse.ts
File metadata and controls
244 lines (208 loc) · 7.81 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Filename: spec-parse.ts
* Author: simshadows <[email protected]>
* License: GNU Affero General Public License v3 (AGPL-3.0)
*/
import fs from "fs";
import yaml from "js-yaml";
import {
setIntersection,
objectValueMap,
} from "./utils";
// TODO: Not entirely sure why I'm getting type issues. We're using this as a crutch for now.
function ensureStr(obj: any, argName: string): string {
if (typeof obj === "string") return obj;
throw new Error(`'${argName}' spec key is not a string.`);
}
function ensureInt(obj: any, argName: string): number {
if (typeof obj !== "number")
throw new Error(`'${argName}' spec key is not a number.`);
if (!Number.isSafeInteger(obj))
throw new Error(`'${argName}' spec key is not a safe integer.`);
return obj;
}
function ensureUniqueStrArray(obj: any, argName: string): string[] {
if (!Array.isArray(obj)) {
throw new Error(`'${argName}' spec key is not an array of strings.`);
}
const newArr: string[] = [];
for (const e of obj) {
if (typeof e !== "string") {
throw new Error(`'${argName}' spec key must be an array of strings.`);
}
newArr.push(e);
}
if ((new Set(newArr)).size !== newArr.length) {
throw new Error(`'${argName}' spec key must contain unique strings.`);
}
return newArr;
}
//function ensureNum(obj: any, argName: string): number {
// if (typeof obj === "number") return obj;
// throw new Error(`'${argName}' spec key is not a number.`);
//}
function inputTransform(obj: any): InputPartialSpec {
// TODO: Simplify this later.
const title: string = ensureStr(obj.title, "input[].title");
const parameters: number = ensureInt(obj.params, "input[].params");
const initialValues: string[] = obj.initialValues
.map((s: any) => ensureStr(s, "input[].initialValues[]"));
return {
title,
parameters,
initialValues,
};
}
function inputIntegerTransform(obj: any): InputIntegerPartialSpec {
// TODO: Simplify this later.
const title: string = ensureStr(obj.title, "input-integer[].title");
const min: number = ensureInt(obj.min, "input-integer[].min");
const max: number = ensureInt(obj.max, "input-integer[].max");
const initial: number = ensureInt(obj.initial, "input-integer[].initial");
if (initial < min || initial > max) {
throw new Error(`Initial value ${initial} falls outside range [${min},${max}].`);
}
return {
title,
min,
max,
initial,
};
}
function fieldLabelsToIndices(
arr: string[],
reference: string[],
argName: string,
refName: string,
): number[] {
const indices: number[] = arr.map(x => reference.indexOf(x));
if (indices.some(x => (x < 0))) {
throw new Error(`'${argName}' spec key must contain strings found in ${refName}.`);
}
// Sanity checks
if ((new Set(indices)).size !== indices.length) {
throw new Error(`Critical bug: arr must map to unique indices.`);
}
if (indices.some(x => x >= reference.length)) {
throw new Error(`Critical bug: arr must map to indices within range.`);
}
return indices;
}
/********************************************************************/
interface InputPartialSpec {
title: string;
parameters: number;
initialValues: string[];
}
interface InputIntegerPartialSpec {
title: string;
min: number;
max: number;
initial: number;
}
/*** ***/
//type OutputFieldSortBy = "ascending" | "descending";
interface OutputFieldSpec {
label: string;
//sortBy: OutputFieldSortBy; // We are always ascending for now
}
interface OutputPartialSpec {
title: string;
fields: OutputFieldSpec[];
// Formatting
groupPriority: number[]; // closer to front = higher-priority
sortPriority: number[]; // closer to front = higher-priority
}
/*** ***/
export interface SpecValues {
name: string;
appTitle: string;
author: string;
description: string;
encoding: string;
constraintsCode: string;
inputBase: {[key: string]: InputPartialSpec};
inputInteger: {[key: string]: InputIntegerPartialSpec};
inputConstrained: {[key: string]: InputPartialSpec};
output: {[key: string]: OutputPartialSpec};
}
export function getSpecValues(specPath: string): SpecValues {
// TODO: Implement manual switching between JSON and YAML?
const fileData: any = (()=>{
try {
return JSON.parse(fs.readFileSync(specPath).toString());
} catch {
return yaml.load(fs.readFileSync(specPath, "utf-8").toString());
}
})();
const seenKeys: Set<string> = new Set();
function checkDuplicates(obj: Object): void {
const keys: Set<string> = new Set(Object.keys(obj));
const dups: Set<string> = setIntersection(seenKeys, keys);
if (dups.size > 0) {
throw new Error(`Duplicate keys found: \n${dups}`);
}
for (const k of keys) seenKeys.add(k);
}
const inputBase: any = fileData["input-base"];
if (typeof inputBase !== "object") {
throw new Error("Must provide an object for input bases.");
}
checkDuplicates(inputBase);
const inputInteger: any = fileData["input-integer"];
if (typeof inputInteger !== "object") {
throw new Error("Must provide an object for input integers.");
}
checkDuplicates(inputInteger);
const inputConstrained: any = fileData["input-constrained"];
if (typeof inputConstrained !== "object") {
throw new Error("Must provide an object for constrained input.");
}
checkDuplicates(inputConstrained);
const output: any = fileData.output;
if (typeof output !== "object") {
throw new Error("Must provide an object for outputs.");
}
checkDuplicates(output);
return {
name: ensureStr(fileData?.name, "name"),
appTitle: ensureStr(fileData?.appTitle, "appTitle"),
author: ensureStr(fileData?.author, "author"),
description: ensureStr(fileData?.description, "description"),
encoding: ensureStr(fileData?.encoding, "encoding"),
constraintsCode: ensureStr(fileData?.constraints, "constraints"),
inputBase: objectValueMap(inputBase, inputTransform),
inputInteger: objectValueMap(inputInteger, inputIntegerTransform),
inputConstrained: objectValueMap(inputConstrained, inputTransform),
output: objectValueMap(output, (obj: any) => {
const title: string = ensureStr(obj.title, "output[].title");
const fieldLabels: string[] = ensureUniqueStrArray(
obj.fieldLabels, "output[].fieldLabels",
);
const groupBy: string[] = ensureUniqueStrArray(
obj.format?.groupBy, "output[].format.groupBy",
);
const sortBy: string[] = ensureUniqueStrArray(
obj.format?.sortBy, "output[].format.sortBy",
);
const fields: OutputFieldSpec[] = fieldLabels.map(x => ({label: x}));
const groupPriority: number[] = fieldLabelsToIndices(
groupBy,
fieldLabels,
"output[].format.groupBy",
"output[].fieldLabels",
);
const sortPriority: number[] = fieldLabelsToIndices(
sortBy,
fieldLabels,
"output[].format.sortBy",
"output[].fieldLabels",
);
// We want to sort all fields, so we fill more values
for (let i = 0; i < fieldLabels.length; ++i) {
if (!sortPriority.includes(i)) sortPriority.push(i);
}
return {title, fields, groupPriority, sortPriority};
}),
};
}