-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore(codegen): add UnaryFunctionCall * chore(codegen): use SerdeElision knowledge index
- Loading branch information
Showing
8 changed files
with
120 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...main/java/software/amazon/smithy/aws/typescript/codegen/validation/UnaryFunctionCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.aws.typescript.codegen.validation; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* For handling expressions that may be unary function calls. | ||
*/ | ||
class UnaryFunctionCall { | ||
private static final Pattern CHECK_PATTERN = Pattern.compile("^(?!new ).+\\(((?!,).)*\\)$"); | ||
private static final Pattern TO_REF_PATTERN = Pattern.compile("(.*)\\(.*\\)$"); | ||
|
||
/** | ||
* @param expression - to be examined. | ||
* @return whether the expression is a single-depth function call with a single parameter. | ||
*/ | ||
public static boolean check(String expression) { | ||
if (expression.equals("_")) { | ||
// not a call per se, but this indicates a pass-through function. | ||
return true; | ||
} | ||
return maxCallDepth(expression) == 1 && CHECK_PATTERN.matcher(expression).matches(); | ||
} | ||
|
||
/** | ||
* @param callExpression the call expression to be converted. Check that | ||
* the expression is a unary call first. | ||
* @return the unary function call converted to a function reference. | ||
*/ | ||
public static String toRef(String callExpression) { | ||
return TO_REF_PATTERN.matcher(callExpression).replaceAll("$1"); | ||
} | ||
|
||
/** | ||
* Estimates the call depth of a function call expression. | ||
* | ||
* @param expression A function call expression (e.g., "call() == 1", "call(call()) == 2", etc). | ||
*/ | ||
private static int maxCallDepth(String expression) { | ||
int depth = 0; | ||
int maxDepth = 0; | ||
for (int i = 0; i < expression.length(); ++i) { | ||
char c = expression.charAt(i); | ||
if (c == '(') { | ||
depth += 1; | ||
if (depth > maxDepth) { | ||
maxDepth = depth; | ||
} | ||
continue; | ||
} | ||
if (c == ')') { | ||
depth -= 1; | ||
} | ||
} | ||
return maxDepth; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
.../java/software/amazon/smithy/aws/typescript/codegen/validation/UnaryFunctionCallTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package software.amazon.smithy.aws.typescript.codegen.validation; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class UnaryFunctionCallTest { | ||
@Test | ||
void check() { | ||
assertEquals(false, UnaryFunctionCall.check("")); | ||
assertEquals(false, UnaryFunctionCall.check("5")); | ||
assertEquals(false, UnaryFunctionCall.check("(param)")); | ||
assertEquals(false, UnaryFunctionCall.check("x[5]")); | ||
assertEquals(false, UnaryFunctionCall.check("new Date(timestamp)")); | ||
assertEquals(false, UnaryFunctionCall.check("x(y(_))")); | ||
assertEquals(false, UnaryFunctionCall.check("call(param).prop")); | ||
assertEquals(false, UnaryFunctionCall.check("call(param, param2)")); | ||
|
||
assertEquals(true, UnaryFunctionCall.check("_")); | ||
assertEquals(true, UnaryFunctionCall.check("x()")); | ||
assertEquals(true, UnaryFunctionCall.check("x(_)")); | ||
assertEquals(true, UnaryFunctionCall.check("long_function_name(long_parameter_name)")); | ||
assertEquals(true, UnaryFunctionCall.check("container.function(param)")); | ||
assertEquals(true, UnaryFunctionCall.check("factory(param)(param2)")); | ||
} | ||
|
||
@Test | ||
void toRef() { | ||
assertEquals("_", UnaryFunctionCall.toRef("_")); | ||
assertEquals("x", UnaryFunctionCall.toRef("x()")); | ||
assertEquals("x", UnaryFunctionCall.toRef("x(_)")); | ||
assertEquals("long_function_name", UnaryFunctionCall.toRef("long_function_name(long_parameter_name)")); | ||
assertEquals("container.function", UnaryFunctionCall.toRef("container.function(param)")); | ||
assertEquals("factory(param)", UnaryFunctionCall.toRef("factory(param)(param2)")); | ||
} | ||
} |