[
https://issues.apache.org/jira/browse/TINKERPOP-3166?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18004319#comment-18004319
]
ASF GitHub Bot commented on TINKERPOP-3166:
-------------------------------------------
Cole-Greer commented on code in PR #3153:
URL: https://github.com/apache/tinkerpop/pull/3153#discussion_r2196362757
##########
gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/AsNumberStepTest.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.
+ */
+package org.apache.tinkerpop.gremlin.process.traversal.step.map;
+
+import org.apache.tinkerpop.gremlin.process.traversal.N;
+import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
+import org.apache.tinkerpop.gremlin.process.traversal.step.StepTest;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.UUID;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class AsNumberStepTest extends StepTest {
+
+ @Override
+ protected List<Traversal> getTraversals() {
+ return Collections.singletonList(__.asNumber());
+ }
+
+ @Test
+ public void testReturnTypes() {
+ assertEquals(1, __.__(1).asNumber().next());
+ assertEquals((byte) 1, __.__(1).asNumber(N.nbyte).next());
+ assertEquals(1, __.__(1.8).asNumber(N.nint).next());
+ assertEquals(1, __.__(1L).asNumber(N.nint).next());
+ assertEquals(1L, __.__(1L).asNumber().next());
+ assertEquals(1, __.__("1").asNumber(N.nint).next());
+ assertEquals(1, __.__("1").asNumber().next());
+ assertEquals((byte) 1, __.__("1").asNumber(N.nbyte).next());
Review Comment:
Would be good to have cases covering all types (short, long, float, double,
bigint, bigdecimal)
> Add number conversion step asNumber()
> -------------------------------------
>
> Key: TINKERPOP-3166
> URL: https://issues.apache.org/jira/browse/TINKERPOP-3166
> Project: TinkerPop
> Issue Type: Improvement
> Components: language
> Affects Versions: 3.8.0
> Reporter: Yang Xia
> Priority: Major
>
> Given the addition of the {{asString()}} and {{asDate()}} steps in the 3.7
> line, it should also be helpful to add an {{asNumber()}} step that does
> numerical casting/conversions.
> The current idea is for the {{asNumber()}} step to convert the incoming
> traverser to the nearest parsable type (e.g. int or double) if no argument is
> provided, or to the desired numerical type, based on a number token
> ({{{}N{}}}) provided. Like the {{asDate()}} step, it will not be scoped (for
> now, scopes can be added in the future).
> Some conjured examples:
> {code:java}
> gremlin> g.inject(5).asNumber()
> ==> 5 // parses to int
> gremlin> g.inject(5.123f).asNumber()
> ==> 5.123
> gremlin> g.inject(5.43).asNumber(N.int)
> ==> 5 {code}
> More details can be found in the [proposal
> doc|https://github.com/apache/tinkerpop/blob/master/docs/src/dev/future/proposal-asnumber-step-6.asciidoc].
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)