Skip to content

Commit a99877b

Browse files
authored
Merge pull request #20 from entityc/bobgarner/added-map-template-variable-type
Adding Map template variable type
2 parents 2df9976 + 2fb8228 commit a99877b

File tree

11 files changed

+387
-2
lines changed

11 files changed

+387
-2
lines changed

docs/emc/EMC_foundation.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The model classes of this type are intended to provide support for foundation ty
99
| Class | Description |
1010
|-----|-----|
1111
|[`MFArray`](#class_MFArray)|This class represents a ordered array of objects. To create an empty array you can use: `$[let myList = @[]@]` then can use these methods on that variable (`myList` in this example) to access the array functionality that is exposed here.|
12+
|[`MFMap`](#class_MFMap)|This class represents a map of objects. To create an empty map you can use: `$[let myMap = @{}@]` then can use these methods on that variable (`myMap` in this example) to access the map functionality that is exposed here.|
1213
|[`MFSet`](#class_MFSet)|This class represents a unique set of objects. You cannot create an object of this class in template code, however, some classes have methods that will return such an object. Since this class does not offer methods to manipulate the object, it should be considered immutable. You can iterate through the items in a set using the `foreach` instruction. For example: `$[foreach item in mySet] ... $[/foreach]` would allow you to process each item(object) in the set.|
1314

1415
<a name="class_MFArray"></a>
@@ -122,6 +123,123 @@ Returns all the values of the array. When iterating this array with a `foreach`
122123

123124

124125

126+
<a name="class_MFMap"></a>
127+
## MFMap Class
128+
129+
This class represents a map of objects. To create an empty map you can use: `$[let myMap = @{}@]` then can use these methods on that variable (`myMap` in this example) to access the map functionality that is exposed here.
130+
131+
The following properties and methods are available for this class:
132+
133+
<hr/>
134+
135+
#### `MFMap` **`clear`**
136+
137+
This method removes all items in the map.
138+
139+
<hr/>
140+
141+
#### `boolean containsKey(Object key)`
142+
143+
Indicates if the map contains the specified key.
144+
145+
| Parameter | Description |
146+
|-----|-----|
147+
|`Object key` | The key of which to determine its presence in the map. |
148+
149+
<hr/>
150+
151+
#### `boolean containsValue(Object value)`
152+
153+
Indicates if the map contains the specified value.
154+
155+
| Parameter | Description |
156+
|-----|-----|
157+
|`Object value` | The object of which to determine its presence in the map. |
158+
159+
<hr/>
160+
161+
#### `MFMap` **`copy`**
162+
163+
Allows you to make a copy of this map. Since it returns the new map you can chain other map methods after this one.
164+
165+
<hr/>
166+
167+
#### `int` **`count`**
168+
169+
Returns the number of items in the map.
170+
171+
<hr/>
172+
173+
#### `Object get(Object key)`
174+
175+
Returns the value in the map by its specified key.
176+
177+
| Parameter | Description |
178+
|-----|-----|
179+
|`Object key` | The key. |
180+
181+
<hr/>
182+
183+
#### `boolean` **`isEmpty`**
184+
185+
Indicates if the map is empty (no items).
186+
187+
<hr/>
188+
189+
#### `Collection` **`keys`**
190+
191+
Returns all the keys of the map
192+
193+
<hr/>
194+
195+
#### `MFMap put(Object key, Object o)`
196+
197+
Allows you to add an object to this array. The `do` template instruction can be used to do this (e.g., `$[do array.add(obj)]`. This method returns the array itself so you can chain other array methods after this one.
198+
199+
| Parameter | Description |
200+
|-----|-----|
201+
|`Object key` | The key to put to the map. |
202+
|`Object o` | The object to set to the map. |
203+
204+
<hr/>
205+
206+
#### `MFMap putAll(MFMap otherMap)`
207+
208+
Allows you to add all elements of another map to this map. The `do` template instruction can be used to do this (e.g., `$[do map.putAll(otherMap)]`. This method returns the map itself so you can chain other map methods after this one.
209+
210+
| Parameter | Description |
211+
|-----|-----|
212+
|`MFMap otherMap` | The other map to add to this map. |
213+
214+
<hr/>
215+
216+
#### `MFMap remove(Object key)`
217+
218+
Allows you to remove a specified entry from the map based on its key. This method returns the map itself so you can chain other map methods after this one.
219+
220+
| Parameter | Description |
221+
|-----|-----|
222+
|`Object key` | The key to remove from this map. |
223+
224+
<hr/>
225+
226+
#### `MFMap remove(Object key, Object o)`
227+
228+
Allows you to remove a specified object from the map based on its key. This method returns the map itself so you can chain other map methods after this one.
229+
230+
| Parameter | Description |
231+
|-----|-----|
232+
|`Object key` | The key to remove from this map. |
233+
|`Object o` | The object to remove from this map. |
234+
235+
<hr/>
236+
237+
#### `Collection` **`values`**
238+
239+
Returns all the values of the map. When iterating this array of values with a `foreach` you do **not** need to use this method.
240+
241+
242+
125243
<a name="class_MFSet"></a>
126244
## MFSet Class
127245

src/main/antlr4/org/entityc/compiler/transform/template/TemplateGrammer.g4

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ expression
289289
| expression bop='||' expression
290290
| <assoc=right> expression bop='?' expression Colon expression
291291
| arraySpecifier
292+
| mapSpecifier
292293
;
293294

294295
expressionList
@@ -299,6 +300,18 @@ arraySpecifier
299300
: '@[' expressionList? ']@'
300301
;
301302

303+
mapItem
304+
: expression ':' expression
305+
;
306+
307+
mapItemList
308+
: mapItem (',' mapItem)*
309+
;
310+
311+
mapSpecifier
312+
: MapOpen mapItemList? MapClose
313+
;
314+
302315
filterParamExpression
303316
: OpenParen expression CloseParen
304317
| identifier

src/main/antlr4/org/entityc/compiler/transform/template/TemplateLexer.g4

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Pipe : '|' ;
6060
Outputs : '->' ;
6161
ArrayOpen : '@[' ;
6262
ArrayClose : ']@' ;
63+
MapOpen : '@{' ;
64+
MapClose : '}@' ;
6365
Dollar : '$' ;
6466
File : 'file' ;
6567
IfDoesNotExist : 'ifdoesnotexist' ;
@@ -139,6 +141,7 @@ DIGIT : [0-9];
139141
VERSION_NUM : (INTEGER '.' INTEGER '.' INTEGER) ;
140142
FLOAT : (INTEGER '.' DIGIT*) ;
141143
IDENT : (LETTER|'_') (LETTER|DIGIT|'_'|'#')* ;
144+
//FILTER_IDENT : (LETTER) (LETTER|'_')* ; we should use this for filters but have to wait until I sort out issue with intellij plugin
142145
STRING : '"' (ESC | ~["\\])* '"' ;
143146
fragment ESC : '\\' ["\bfnrt] ;
144147
DOT : '.' ;
@@ -201,6 +204,8 @@ VarIsEqual : IsEqual -> type(IsEqual) ;
201204
VarNotEqual : NotEqual -> type(NotEqual) ;
202205
VarArrayOpen : ArrayOpen -> type(ArrayOpen) ;
203206
VarArrayClose : ArrayClose -> type(ArrayClose) ;
207+
VarMapOpen : MapOpen -> type(MapOpen) ;
208+
VarMapClose : MapClose -> type(MapClose) ;
204209
205210
VarDomainType : DomainType -> type(DomainType) ;
206211
VarEntityType : EntityType -> type(EntityType) ;

src/main/java/org/entityc/compiler/ASTVisitor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,9 @@ public Object visitRepository(EntityLanguageParser.RepositoryContext ctx) {
700700
}
701701
repository.setType(type);
702702
}
703+
if (currentSpace() == null) {
704+
ECLog.logFatal("Space must be defined before configuration.");
705+
}
703706
currentSpace().addRepository(repository);
704707

705708
return repository;

src/main/java/org/entityc/compiler/EntityCompiler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,12 @@ public static void RunProtoc(MTRoot root, MTConfiguration configuration) {
226226
processBuilder.redirectError(errorOutputFile);
227227
try {
228228
if (EntityCompiler.isVerbose()) {
229-
ECLog.logInfo("Running PROTOC...");
229+
StringBuilder sb = new StringBuilder();
230+
for (String cmd : processBuilder.command())
231+
{
232+
sb.append(cmd + " ");
233+
}
234+
ECLog.logInfo("Running PROTOC: " + sb);
230235
}
231236
// ECLog.logInfo("Running PROTOC: " + processBuilder.command());
232237
Process process = processBuilder.start();

src/main/java/org/entityc/compiler/model/domain/MTNamingMethod.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
public enum MTNamingMethod {
1212

1313
STANDARD("standard", ""),
14+
UNDERSCORE("underscore", "_"),
1415
UNDERSCORE_LOWERCASE("underscoreLowercase", "_"),
1516
UNDERSCORE_UPPERCASE("underscoreUppercase", "_"),
1617
LOWERCASE("lowercase", ""),
@@ -46,6 +47,8 @@ public String rename(String name) {
4647
switch (this) {
4748
case STANDARD:
4849
return name;
50+
case UNDERSCORE:
51+
return ECStringUtil.CamelToSeparator(name,delimiter);
4952
case UNDERSCORE_LOWERCASE:
5053
case DASHES_LOWERCASE:
5154
return ECStringUtil.CamelToSeparator(name, delimiter).toLowerCase();

src/main/java/org/entityc/compiler/model/entity/MTEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public boolean hasBitFields() {
502502
category = ModelMethodCategory.RELATIONSHIP,
503503
description = "Indicates whether this entity defines any relationships.")
504504
public boolean hasRelationships() {
505-
return relationships.size() > 0 || secondaryRelationshipMap.size() > 0;
505+
return relationships.size() > 0 || (secondaryRelationshipMap != null && secondaryRelationshipMap.size() > 0);
506506
}
507507

508508
@ModelMethod(
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright (c) 2019-2022 The EntityC Project. All rights reserved.
3+
* Use of this file is governed by the BSD 3-clause license that
4+
* can be found in the LICENSE.md file in the project root.
5+
*/
6+
7+
package org.entityc.compiler.model.foundation;
8+
9+
import org.entityc.compiler.doc.annotation.ModelClass;
10+
import org.entityc.compiler.doc.annotation.ModelClassType;
11+
import org.entityc.compiler.doc.annotation.ModelMethod;
12+
import org.entityc.compiler.doc.annotation.ModelMethodParameter;
13+
import org.entityc.compiler.util.ECLog;
14+
15+
import java.util.Collection;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
19+
@ModelClass(type = ModelClassType.FOUNDATION,
20+
description = "This class represents a map of objects. "
21+
+ "To create an empty map you can use: `$[let myMap = @{}@]` then can use these methods on that "
22+
+ "variable (`myMap` in this example) to access the map functionality that is exposed here.")
23+
public class MFMap extends MFObject {
24+
25+
private final HashMap map = new HashMap();
26+
27+
public MFMap() {
28+
super();
29+
}
30+
31+
public MFMap(Map map) {
32+
super();
33+
map.putAll(map);
34+
}
35+
36+
@ModelMethod(description =
37+
"Returns all the keys of the map")
38+
public Collection getKeys() {
39+
return map.keySet();
40+
}
41+
42+
@ModelMethod(description =
43+
"Returns all the values of the map. When iterating this array of values with a `foreach` you do "
44+
+ "**not** need to use this method.")
45+
public Collection getValues() {
46+
return map.values();
47+
}
48+
49+
@ModelMethod(description =
50+
"Allows you to add an object to this array. The `do` template instruction can be used to do "
51+
+ "this (e.g., `$[do array.add(obj)]`. This method returns the array itself so you "
52+
+ "can chain other array methods after this one.")
53+
public MFMap put(
54+
@ModelMethodParameter(description = "The key to put to the map.")
55+
Object key,
56+
@ModelMethodParameter(description = "The object to set to the map.")
57+
Object o) {
58+
map.put(key, o);
59+
return this;
60+
}
61+
62+
@ModelMethod(description = "Allows you to make a copy of this map. Since it returns the new map "
63+
+ "you can chain other map methods after this one.")
64+
public MFMap copy() {
65+
MFMap newMap = new MFMap();
66+
newMap.map.putAll(this.map);
67+
return newMap;
68+
}
69+
70+
@ModelMethod(description =
71+
"Allows you to add all elements of another map to this map. The `do` template instruction "
72+
+ "can be used to do this (e.g., `$[do map.putAll(otherMap)]`. This method returns the "
73+
+ "map itself so you can chain other map methods after this one.")
74+
public MFMap putAll(
75+
@ModelMethodParameter(description = "The other map to add to this map.")
76+
MFMap otherMap) {
77+
map.putAll(otherMap.map);
78+
return this;
79+
}
80+
81+
@ModelMethod(description = "Allows you to remove a specified object from the map based on its key. This method returns the "
82+
+ "map itself so you can chain other map methods after this one.")
83+
public MFMap remove(
84+
@ModelMethodParameter(description = "The key to remove from this map.")
85+
Object key,
86+
@ModelMethodParameter(description = "The object to remove from this map.")
87+
Object o) {
88+
map.remove(key, o);
89+
return this;
90+
}
91+
92+
@ModelMethod(description = "Allows you to remove a specified entry from the map based on its key. This method returns the "
93+
+ "map itself so you can chain other map methods after this one.")
94+
public MFMap remove(
95+
@ModelMethodParameter(description = "The key to remove from this map.")
96+
Object key) {
97+
map.remove(key);
98+
return this;
99+
}
100+
101+
@ModelMethod(description = "This method removes all items in the map.")
102+
public MFMap clear() {
103+
map.clear();
104+
return this;
105+
}
106+
107+
@ModelMethod(description = "Returns the value in the map by its specified key.")
108+
public Object get(
109+
@ModelMethodParameter(description = "The key.")
110+
Object key) {
111+
return map.get(key);
112+
}
113+
114+
@ModelMethod(description = "Indicates if the map is empty (no items).")
115+
public boolean isEmpty() {
116+
return map.isEmpty();
117+
}
118+
119+
@ModelMethod(description = "Indicates if the map contains the specified key.")
120+
public boolean containsKey(
121+
@ModelMethodParameter(description = "The key of which to determine its presence in the map.")
122+
Object key) {
123+
return map.containsKey(key);
124+
}
125+
126+
@ModelMethod(description = "Indicates if the map contains the specified value.")
127+
public boolean containsValue(
128+
@ModelMethodParameter(description = "The object of which to determine its presence in the map.")
129+
Object value) {
130+
return map.containsValue(value);
131+
}
132+
133+
@ModelMethod(description = "Returns the number of items in the map.")
134+
public int getCount() {
135+
return map.size();
136+
}
137+
}

0 commit comments

Comments
 (0)