-
Notifications
You must be signed in to change notification settings - Fork 586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use serde elision index #4712
Merged
Merged
Use serde elision index #4712
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this repo and smithy-ts have different static analysis requirements. It might've needed to be abstract there when there were no instance level methods or properties. That check made sense to me. |
||
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)")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interface change is ok, right? if any external consumers upgrade, they change their calls at the same time?