-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add nodes to handle types #49785
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
Merged
Merged
Add nodes to handle types #49785
Changes from all commits
Commits
Show all changes
4 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 hidden or 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
81 changes: 81 additions & 0 deletions
81
modules/lang-painless/src/main/java/org/elasticsearch/painless/node/DResolvedType.java
This file contains hidden or 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,81 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.painless.node; | ||
|
||
import org.elasticsearch.painless.Location; | ||
import org.elasticsearch.painless.lookup.PainlessLookup; | ||
import org.elasticsearch.painless.lookup.PainlessLookupUtility; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Represents a Painless type as a {@link Class}. This may still | ||
* require resolution to ensure the type in the {@link PainlessLookup}. | ||
*/ | ||
public class DResolvedType extends DType { | ||
|
||
protected final Class<?> type; | ||
|
||
/** | ||
* If set to {@code true} ensures the type is in the {@link PainlessLookup}. | ||
* If set to {@code false} assumes the type is valid. | ||
*/ | ||
protected final boolean checkInLookup; | ||
|
||
public DResolvedType(Location location, Class<?> type) { | ||
this(location, type, true); | ||
} | ||
|
||
public DResolvedType(Location location, Class<?> type, boolean checkInLookup) { | ||
super(location); | ||
this.type = Objects.requireNonNull(type); | ||
this.checkInLookup = checkInLookup; | ||
} | ||
|
||
/** | ||
* If {@link #checkInLookup} is {@code true} checks if the type is in the | ||
* {@link PainlessLookup}, otherwise returns {@code this}. | ||
* @throws IllegalArgumentException if both checking the type is in the {@link PainlessLookup} | ||
* and the type cannot be resolved from the {@link PainlessLookup} | ||
* @return a {@link DResolvedType} where the resolved Painless type is retrievable | ||
*/ | ||
@Override | ||
public DResolvedType resolveType(PainlessLookup painlessLookup) { | ||
if (checkInLookup == false) { | ||
return this; | ||
} | ||
|
||
if (painlessLookup.getClasses().contains(type) == false) { | ||
throw location.createError(new IllegalArgumentException( | ||
"cannot resolve type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "]")); | ||
} | ||
|
||
return new DResolvedType(location, type, false); | ||
} | ||
|
||
public Class<?> getType() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return " (DResolvedType [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "])"; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
modules/lang-painless/src/main/java/org/elasticsearch/painless/node/DType.java
This file contains hidden or 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,46 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.painless.node; | ||
|
||
import org.elasticsearch.painless.Location; | ||
import org.elasticsearch.painless.lookup.PainlessLookup; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Represents an abstract Painless type. {@link DType} nodes must be | ||
* resolved using {@link #resolveType(PainlessLookup)} to retrieve the | ||
* actual Painless type. {@link DType} exists as a base class so consumers | ||
* may have either a {@link DUnresolvedType} representing a Painless | ||
* canonical type name or a {@link DResolvedType} representing a Painless | ||
* type as the Painless AST is constructed. This allows Painless types already | ||
* resolved at the time of Painless AST construction to not be forced to | ||
* convert back to a Painless canonical type name and then re-resolved. | ||
*/ | ||
public abstract class DType { | ||
|
||
protected final Location location; | ||
|
||
public DType(Location location) { | ||
this.location = Objects.requireNonNull(location); | ||
} | ||
|
||
public abstract DResolvedType resolveType(PainlessLookup painlessLookup); | ||
} |
61 changes: 61 additions & 0 deletions
61
modules/lang-painless/src/main/java/org/elasticsearch/painless/node/DUnresolvedType.java
This file contains hidden or 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 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.painless.node; | ||
|
||
import org.elasticsearch.painless.Location; | ||
import org.elasticsearch.painless.lookup.PainlessLookup; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Represents a canonical Painless type name as a {@link String} | ||
* that requires resolution. | ||
*/ | ||
public class DUnresolvedType extends DType { | ||
|
||
protected final String typeName; | ||
|
||
public DUnresolvedType(Location location, String typeName) { | ||
super(location); | ||
this.typeName = Objects.requireNonNull(typeName); | ||
} | ||
|
||
/** | ||
* Resolves the canonical Painless type name to a Painless type. | ||
* @throws IllegalArgumentException if the type cannot be resolved from the {@link PainlessLookup} | ||
* @return a {@link DResolvedType} where the resolved Painless type is retrievable | ||
*/ | ||
@Override | ||
public DResolvedType resolveType(PainlessLookup painlessLookup) { | ||
Class<?> type = painlessLookup.canonicalTypeNameToType(typeName); | ||
|
||
if (type == null) { | ||
throw location.createError(new IllegalArgumentException("cannot resolve type [" + typeName + "]")); | ||
} | ||
|
||
return new DResolvedType(location, type); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "(DUnresolvedType [" + typeName + "])"; | ||
} | ||
} | ||
|
This file contains hidden or 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
Oops, something went wrong.
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.
Please add javadoc.
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.
Done.