[ https://issues.apache.org/jira/browse/TINKERPOP-2234?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18019917#comment-18019917 ]
ASF GitHub Bot commented on TINKERPOP-2234: ------------------------------------------- spmallette commented on code in PR #3207: URL: https://github.com/apache/tinkerpop/pull/3207#discussion_r2344984883 ########## docs/src/dev/future/proposal-type-predicate-8.asciidoc: ########## @@ -0,0 +1,264 @@ +//// +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF 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. +//// + +image::apache-tinkerpop-logo.png[width=500,link="https://tinkerpop.apache.org"] + +*x.y.z - Proposal 8* + +== Type Predicate + +=== Motivation + +Following up on link:https://issues.apache.org/jira/browse/TINKERPOP-2234[TINKERPOP-2234], filtering of traversers based on types would be a very convenient addition to data manipulation in Gremlin. For example, when we perform union of traversals and only want vertices or edges, currently there is no simple way to assert the type and pass it through. + +=== Type Token Definition & Implementation + +To facilitate type assertion across GLVs, using a unified set of enum tokens is the most convenient, especially given we do not want to leak Java types into other languages. The proposal here is to use a defined set of Gremlin Type tokens (see Appendix), and use an interface structure to allow providers to implement and register their own type tokens to be used with the Gremlin Language. + +==== Gremlin Type Enum + +We will define a `GType` enum consiste of core types in Gremlin be used with the type predicate (for full set of types, see Appendix), for type safety and maintainability across GLVs. This will also be the singel set of type enum used in Gremlin steps (e.g. replacing `N` in `asNumber()`). + +This enum itself will not be extensable, however it will contain a GlobalCache which providers can use to register their own types with the Grammar to be used as string arguments. + +[source,java] +---- +public enum GType { + BYTE(Byte.class), + DOUBLE(Double.class), + FLOAT(Float.class), + INT(Integer.class), + LIST(List.class), + LONG(Long.class), + MAP(Map.class), + NULL(OffsetDateTime.class),; + + private final Class<?> javaType; + + GType(Class<?> javaType) { + this.javaType = javaType; + } + + public Class<?> getType() { return javaType; } + + public GType fromName(String name) { return GType.valueOf(name.toUpperCase()); } + + static final class GlobalTypeCache { + + private GlobalTypeCache() { + throw new IllegalStateException("Utility class"); + } + + private static final Map<String, Class<?>> GLOBAL_TYPE_REGISTRY = new ConcurrentHashMap<>() { + }; + } +} +---- + +=== Addition of `P.typeOf(token)` + +Given `P` is widely used by filter steps and has existing evaluation structure, we would also introduce a new predicate `Type` for type assertions. + +[source,java] +---- +public enum Type implements PBiPredicate<Object, Object> { + typeOf { + @Override + public boolean test(final Object first, final Object second) { + // implementations + }} +} +---- + +There are several accepted overloads for the token. + +* `P.typeOf(GType)` - default case, accepts core Gremlin type enum. +* `P.typeOf(String)` - string for extensions: +** Can be the string key used in the Global Cache (e.g. `"GType.NUMBER"`), for providers +** Can be canonical Java class names for which we can derive the class from (e.g. `"java.lang.Number"`) +* `P.typeOf(Class)` - Java-specific for convenience, can be used as sugar syntax in Groovy + +Any other input, while accepted, will return `false` and lead to result being filtered out (alternative is to throw exception). + +For negation, use `not(P.typeOf(token))`. + +==== Examples + +---- +// Use of GType +gremlin> g.V().is(P.of(GType.VERTEX)).fold() +==> [v[1],v[2],[v3],[v4],[v5],[v6]] +gremlin> g.inject(1.0,2,3,'hello',false).is(P.typeOf(GType.NUMBER)).fold() +==> [1.0,2,3] +// for negation, use not() +gremlin> g.inject(1.0,2,3,'hello',false).is(not(P.typeOf(GType.NUMBER))).fold() +==> [hello,false] + +// Use of String key in GlobalTypeCache +gremlin> g.inject(1.0,2,3,'hello',false).is(P.typeOf("GType.NUMBER")).fold() // in this case +==> [1.0,2,3] +// Use of canonical Java name Review Comment: i think it should only be if its registered. it reduces the chance of someone doing something foul from a security perspective. i don't know what foul would be but no reason to leave a hole where there doesnt need to be one. please make sure that's clear in the proposal. thanks. > Introduce Type Predicate > ------------------------ > > Key: TINKERPOP-2234 > URL: https://issues.apache.org/jira/browse/TINKERPOP-2234 > Project: TinkerPop > Issue Type: Improvement > Components: process > Affects Versions: 3.4.2 > Reporter: Stephen Mallette > Priority: Major > > Provide for a {{typeOf()}} predicate that allows for testing the type of an > object which would enable neat things like: > {code} > g.V().outE().has('weight',gt(0.1)).inV().path().unfold().is(typeOf(VERTEX)) > {code} > See the linked DISCUSS thread for more information. -- This message was sent by Atlassian Jira (v8.20.10#820010)