Skip to content

Commit 0c01658

Browse files
Implement collectBeansFromConfigFile for JSON files
1 parent ae55071 commit 0c01658

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

env-model-generator/src/parseJson.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import * as fs from "fs-extra";
12
import * as jsonConfig from "./annotationConfig";
3+
import { ConfigParseError } from "./configParseError";
24
import * as model from "./model";
5+
import { lowerInitialChar } from "./utils";
36

47
interface BeanWithName {
58
qualifiedClassName: string;
@@ -8,7 +11,31 @@ interface BeanWithName {
811

912
const beansById = new Map<string, BeanWithName>();
1013

11-
export async function collectBeansFromConfigFile(_filePath: string): Promise<string[]> {
14+
export async function collectBeansFromConfigFile(filePath: string): Promise<string[]> {
15+
let json: string;
16+
try {
17+
json = await fs.readFile(filePath, "utf8");
18+
} catch (err) {
19+
throw new ConfigParseError(`Can't open input file '${filePath}': ${err.message}`);
20+
}
21+
const annotationConfig: jsonConfig.AnnotationConfig = JSON.parse(json);
22+
for (const qualifiedClassName in annotationConfig.beans) {
23+
if (!annotationConfig.beans.hasOwnProperty(qualifiedClassName))
24+
continue;
25+
// Create bean ID
26+
// TODO: Handle name parameter to @Component constructor
27+
const id = lowerInitialChar(<string>qualifiedClassName.split(".").pop());
28+
// Check for duplicate ids from other beans
29+
if (beansById.has(id))
30+
throw new ConfigParseError(`Found multiple beans with id '${id}'`);
31+
const aliasedBeanId = model.Bean.getAlias(id);
32+
if (aliasedBeanId !== undefined)
33+
throw new ConfigParseError(`Found a bean with id same as an alias for '${aliasedBeanId}'`);
34+
// Create a bean model for the bean
35+
const bean = { qualifiedClassName: qualifiedClassName, bean: annotationConfig.beans[qualifiedClassName] };
36+
beansById.set(id, bean);
37+
}
38+
// TODO: Return paths to XML config files specified in code
1239
return [];
1340
}
1441

env-model-generator/src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import * as _ from "lodash";
33
import * as pathUtils from "path";
44
import * as xml2js from "xml2js";
55

6+
export function lowerInitialChar(value: string) {
7+
return value[0].toLowerCase() + value.substr(1);
8+
}
9+
610
export function upperInitialChar(value: string) {
711
return value[0].toUpperCase() + value.substr(1);
812
}

0 commit comments

Comments
 (0)