Skip to content

Commit 6770681

Browse files
author
Bob Garner
committed
Added nameas filter to provide direct access to naming methods.
1 parent 636847b commit 6770681

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

docs/etl/ETL.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Here is the full list of filters:
130130
| [`name`](#filter_detail_name) | This simply calls `getName()` on the input. It is more of a convenient way to get the name by using a filter. |
131131
| [`path`](#filter_detail_path) | This will convert a string or namespace object into a string where by a '/' character is used as a delimiter instead of a '.'. This is useful when you need to convert a namespace into a filepath. |
132132
| [`plural`](#filter_detail_plural) | This will attempt to pluralize the last word of the input string. If it can't determine the pluralization it may just return the same string. |
133+
| [`rename`](#filter_detail_rename) | Places underscore between words then forces all characters to be lowercase. |
133134
| [`reverse`](#filter_detail_reverse) | Given a collection of objects, this will return a collection that has the reverse order of the input. |
134135
| [`sort`](#filter_detail_sort) | Given a collection of objects, this will return a collection that is sorted by name. This is useful when generating code that is consistent each time in terms of the order of objects in the output. |
135136
| [`title`](#filter_detail_title) | Given a string of words in camel case format, this will capitalize each word in the string except those words that are typically not capitalized in a title (such as 'of', 'the'. 'and', etc.). |
@@ -443,6 +444,25 @@ Valid inputs for this filter are:
443444

444445
<hr/>
445446

447+
<a name="filter_detail_rename"></a>
448+
##### Filter: `rename`
449+
450+
Places underscore between words then forces all characters to be lowercase.
451+
452+
Valid inputs for this filter are:
453+
454+
| Class of Valid Input | Description |
455+
|---|---|
456+
| `String` | The string to change into an underscore lowercase format. |
457+
458+
This filter has the following parameters:
459+
460+
| Usage with Parameter | Description |
461+
|---|---|
462+
| `rename:`*method* | Specifies the renaming method: standard, underscore, underscoreLowercase, underscoreUppercase, lowercase, uppercase, capitalize, dashesLowercase, dashesUppercase, parentPrefix |
463+
464+
<hr/>
465+
446466
<a name="filter_detail_reverse"></a>
447467
##### Filter: `reverse`
448468

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252

5353
public class EntityCompiler {
5454

55-
public static final String COMPILER_VERSION = "0.15.0";
56-
public static final String LANGUAGE_VERSION = "0.12.6";
55+
public static final String COMPILER_VERSION = "0.16.0";
56+
public static final String LANGUAGE_VERSION = "0.13.0";
5757
private static final Map<String, String> defineValues = new HashMap<>();
5858
private static final Set<String> templateSearchPath = new HashSet<>();
5959
private static CommandLine commandLine;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package org.entityc.compiler.model.domain;
88

9+
import org.entityc.compiler.util.ECLog;
910
import org.entityc.compiler.util.ECStringUtil;
1011

1112
public enum MTNamingMethod {
@@ -39,6 +40,10 @@ public static MTNamingMethod fromName(String name) {
3940
return null;
4041
}
4142

43+
public String getName() {
44+
return name;
45+
}
46+
4247
public String getDelimiter() {
4348
return delimiter;
4449
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.transform.template.tree.filter;
8+
9+
import org.antlr.v4.runtime.ParserRuleContext;
10+
import org.entityc.compiler.model.domain.MTNamingMethod;
11+
import org.entityc.compiler.transform.template.tree.FTTransformSession;
12+
import org.entityc.compiler.transform.template.tree.expression.FTExpression;
13+
import org.entityc.compiler.transform.template.tree.expression.FTOperand;
14+
import org.entityc.compiler.util.ECLog;
15+
16+
import java.util.Arrays;
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
public class FTNameAsFilter extends FTFilter {
21+
22+
public FTNameAsFilter() {
23+
super(null, "nameas",
24+
"Places underscore between words then forces all characters to be lowercase.");
25+
this.addFilterParam(new FTFilterParam("method",
26+
"Specifies the naming method: " + String.join(", ", Arrays.stream(MTNamingMethod.values()).map(MTNamingMethod::getName).toArray(String[]::new))));
27+
addSingleInputType(String.class, "The string to change into an underscore lowercase format.");
28+
}
29+
30+
@Override
31+
public Object filter(ParserRuleContext ctx, FTTransformSession session, Object input, List<FTExpression> paramValues, Map<String, Object> options) {
32+
checkInput(ctx, input, paramValues, options);
33+
34+
if (paramValues.size() == 0) {
35+
ECLog.logFatal(ctx, "Must specify a naming method. Valid values are: " + String.join(", ", Arrays.stream(MTNamingMethod.values()).map(MTNamingMethod::getName).toArray(String[]::new)));
36+
}
37+
38+
if (input instanceof String) {
39+
String inputString = (String) input;
40+
FTExpression methodParam = paramValues.get(0);
41+
String methodName = null;
42+
if (methodParam.isOperand()) {
43+
methodName = ((FTOperand) methodParam).getName();
44+
}
45+
else
46+
{
47+
ECLog.logFatal(ctx, "Naming method must be specified directly. For example: entity|nameas:uppercase");
48+
}
49+
MTNamingMethod namingMethod = MTNamingMethod.fromName(methodName);
50+
if (namingMethod == null) {
51+
ECLog.logFatal(ctx, "Unknown naming method: " + methodName);
52+
}
53+
return namingMethod.rename(inputString);
54+
}
55+
56+
return null;
57+
}
58+
}

0 commit comments

Comments
 (0)