Skip to content

Commit 321a128

Browse files
Used new, more structured, blocks
1 parent 4fb6e88 commit 321a128

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

env-model-generator/src/env-model-generator.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as program from "commander";
66
import * as fs from "fs-extra";
77
import * as _ from "lodash";
88
import * as pathUtils from "path";
9-
import { BlankLine, Block, Class, FnCall, Method, Null, Return, Statement } from "./javaCode";
9+
import { BlankLine, CatchBlock, Class, FnCall, IfThenElse, Method, Null, Return, Statement, StringConstant, TryCatch } from "./javaCode";
1010
import * as model from "./model";
1111
import parseSpringConfigFiles from "./parserDriver";
1212
import { createPath } from "./utils";
@@ -64,14 +64,13 @@ async function transformConfigFiles(fileName: string, options: any) {
6464
new Method(
6565
"public Object getBean(String name)",
6666
[
67-
new Block(
68-
"try",
67+
new TryCatch(
6968
(<Statement[]>[]).concat(
7069
model.Repository.getNamed()
7170
.sort((a, b) => a.name < b.name ? -1 : a.name === b.name ? 0 : 1)
7271
.map((bean) =>
73-
new Block(
74-
`if (name.equals("${bean.name}"))`,
72+
new IfThenElse(
73+
new FnCall("name.equals", [ new StringConstant(bean.name) ]),
7574
[
7675
new Return(new FnCall(bean.getter, [])),
7776
])),
@@ -80,17 +79,14 @@ async function transformConfigFiles(fileName: string, options: any) {
8079
const bean = model.Repository.tryGet(beanId);
8180
if (bean === undefined)
8281
throw new Error("Found alias to bean that doesn't exist");
83-
return new Block(
84-
`if (name.equals("${alias}"))`,
82+
return new IfThenElse(
83+
new FnCall("name.equals", [ new StringConstant(alias) ]),
8584
[
8685
new Return(new FnCall(bean.getter, [])),
8786
]);
8887
}),
8988
),
90-
),
91-
new Block(
92-
"catch(Exception ex)",
93-
[]),
89+
[ new CatchBlock("Exception") ]),
9490
new Return(new Null()),
9591
],
9692
[],

env-model-generator/src/model.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ import * as _ from "lodash";
22
import { ConfigParseError } from "./configParseError";
33
import {
44
Assignment,
5+
BinaryOperator,
56
BlankLine,
6-
Block,
77
BraceInitialiser,
88
Declaration,
99
Expression,
1010
FnCall,
1111
FnStatement,
12+
IfThenElse,
1213
Member,
1314
Method,
1415
Null,
16+
RangedFor,
1517
Return,
1618
Statement,
1719
StringConstant,
@@ -190,8 +192,8 @@ export class Bean implements IBean {
190192
this._javaMembers.push(new Method(
191193
`public static ${typeName} ${this.getter}()`,
192194
[
193-
new Block(
194-
`if (${variableName} == null)`,
195+
new IfThenElse(
196+
new BinaryOperator(new Symbol(variableName), "==", new Null()),
195197
[
196198
new Assignment(variableName, new FnCall(creatorFn, constructorArgs)),
197199
...initialisers,
@@ -432,8 +434,10 @@ export class PropertiesValue extends Value {
432434
"public static java.util.Properties listToProperties(java.util.List<java.util.AbstractMap.SimpleEntry<String, String>> properties)",
433435
[
434436
new Declaration("java.util.Properties", "result", new FnCall("new java.util.Properties", [])),
435-
new Block(
436-
"for (java.util.AbstractMap.SimpleEntry<String, String> prop : properties)",
437+
new RangedFor(
438+
"java.util.AbstractMap.SimpleEntry<String, String>",
439+
"prop",
440+
new Symbol("properties"),
437441
[
438442
new FnStatement(new FnCall("result.put", [ new FnCall("prop.getKey", []), new FnCall("prop.getValue", []) ])),
439443
]),
@@ -481,8 +485,10 @@ export class MapValue extends Value {
481485
"public static java.util.Map<String, String> listToMap(java.util.List<java.util.AbstractMap.SimpleEntry<String, String>> entries)",
482486
[
483487
new Declaration("java.util.Map<String, String>", "result", new FnCall("new java.util.HashMap<String, String>", [])),
484-
new Block(
485-
"for (java.util.AbstractMap.SimpleEntry<String, String> entry : entries)",
488+
new RangedFor(
489+
"java.util.AbstractMap.SimpleEntry<String, String>",
490+
"entry",
491+
new Symbol("entries"),
486492
[
487493
new FnStatement(new FnCall("result.put", [ new FnCall("entry.getKey", []), new FnCall("entry.getValue", []) ])),
488494
]),

0 commit comments

Comments
 (0)