Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions common/src/main/java/dev/cel/common/CelContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ public Builder addAbbreviations(ImmutableSet<String> qualifiedNames) {
* useful for remapping poorly chosen protobuf message / package names.
*
* <p>Note: all the rules that apply to abbreviations also apply to aliasing.
*
* <p>Note: It is also possible to alias a top-level package or a name that does not contain a
* period. When resolving an identifier, CEL checks for variables and functions before
* attempting to expand aliases for type resolution. Therefore, if an expression consists solely
* of an identifier that matches both an alias and a declared variable (e.g., {@code
* short_alias}), the variable will take precedence and the compilation will succeed. The alias
* expansion will only be used when the alias is a prefix to a longer name (e.g., {@code
* short_alias.TestRequest}) or if no variable with the same name exists, in which case using
* the alias as a standalone identifier will likely result in a compilation error.
*
* @param alias Simple name to be expanded. Must be a valid identifier.
* @param qualifiedName The fully qualified name to expand to. This may be a simple name (e.g. a
* package name) but it must be a valid identifier.
*/
@CanIgnoreReturnValue
public Builder addAlias(String alias, String qualifiedName) {
Expand All @@ -172,12 +185,6 @@ private void validateAliasOrThrow(AliasKind kind, String qualifiedName, String a
String.format("qualified name must not begin with a leading '.': %s", qualifiedName));
}

int index = qualifiedName.lastIndexOf(".");
if (index <= 0 || index == qualifiedName.length() - 1) {
throw new IllegalArgumentException(
String.format("%s must refer to a valid qualified name: %s", kind, qualifiedName));
}

String aliasRef = aliases.get(alias);
if (aliasRef != null) {
throw new IllegalArgumentException(
Expand Down
4 changes: 0 additions & 4 deletions common/src/test/java/dev/cel/common/CelContainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ public void containerBuilder_addAliasError_throws(@TestParameter AliasingErrorTe

private enum AliasingErrorTestCase {
BAD_QUALIFIED_NAME(
"foo",
"invalid_qualified_name",
"alias must refer to a valid qualified name: invalid_qualified_name"),
BAD_QUALIFIED_NAME_2(
"foo", ".bad.name", "qualified name must not begin with a leading '.': .bad.name"),
BAD_ALIAS_NAME_1(
"bad.alias", "b.c", "alias must be non-empty and simple (not qualified): alias=bad.alias"),
Expand Down