This is an automated email from the ASF dual-hosted git repository.

xiazcy pushed a commit to branch 3.5-dev
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git


The following commit(s) were added to refs/heads/3.5-dev by this push:
     new 2c63a41e22 [TINKERPOP-2849] fix for GraphTraversalSource.With (#1917)
2c63a41e22 is described below

commit 2c63a41e2279e00ece670d19108749eb3d890007
Author: Valentyn Kahamlyk <[email protected]>
AuthorDate: Tue Jan 3 12:00:25 2023 -0800

    [TINKERPOP-2849] fix for GraphTraversalSource.With (#1917)
    
    * fix for GraphTraversalSource.With
    * update changelog
    
    Co-authored-by: valentynk <[email protected]>
    Co-authored-by: Yang Xia <[email protected]>
---
 CHANGELOG.asciidoc                             |  1 +
 gremlin-go/driver/graphTraversalSource.go      | 18 ++++++-
 gremlin-go/driver/graphTraversalSource_test.go | 69 ++++++++++++++++++++++++++
 3 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index aa6681bb9f..cfdbd55939 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -58,6 +58,7 @@ 
image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
 * Removed the delay for reconnecting to a potentially unhealthy `Host` only 
marking it as unavailable after that initial retry fails.
 * Prevented fast `NoHostAvailableException` in favor of more direct exceptions 
when borrowing connections from the `ConnectionPool`.
 * Fixed an issue in Go and Python GLVs where modifying per request settings to 
override request_id's was not working correctly.
+* Fixed incorrect implementation for `GraphTraversalSource.With` in 
`gremlin-go`.
 
 ==== Bugs
 
diff --git a/gremlin-go/driver/graphTraversalSource.go 
b/gremlin-go/driver/graphTraversalSource.go
index 2cf4470fe1..fa762fdf2c 100644
--- a/gremlin-go/driver/graphTraversalSource.go
+++ b/gremlin-go/driver/graphTraversalSource.go
@@ -122,7 +122,23 @@ func (gts *GraphTraversalSource) WithoutStrategies(args 
...TraversalStrategy) *G
 // With provides a configuration to a traversal in the form of a key value 
pair.
 func (gts *GraphTraversalSource) With(key interface{}, value interface{}) 
*GraphTraversalSource {
        source := gts.clone()
-       source.bytecode.AddSource("with", key, value)
+
+       var optionsStrategy TraversalStrategy = nil
+       for _, v := range gts.bytecode.sourceInstructions {
+               if v.operator == "withStrategies" &&
+                       v.arguments[0].(*traversalStrategy).name == 
decorationNamespace+"OptionsStrategy" {
+                       optionsStrategy = v.arguments[0]
+                       break
+               }
+       }
+
+       if optionsStrategy == nil {
+               optionsStrategy = 
OptionsStrategy(map[string]interface{}{key.(string): value})
+               return source.WithStrategies(optionsStrategy)
+       }
+
+       options := optionsStrategy.(*traversalStrategy)
+       options.configuration[key.(string)] = value
        return source
 }
 
diff --git a/gremlin-go/driver/graphTraversalSource_test.go 
b/gremlin-go/driver/graphTraversalSource_test.go
new file mode 100644
index 0000000000..ce1ad9aac6
--- /dev/null
+++ b/gremlin-go/driver/graphTraversalSource_test.go
@@ -0,0 +1,69 @@
+/*
+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 gremlingo
+
+import (
+       "github.com/stretchr/testify/assert"
+       "testing"
+)
+
+func TestGraphTraversalSource(t *testing.T) {
+
+       t.Run("GraphTraversalSource.With tests", func(t *testing.T) {
+               t.Run("Test for single param", func(t *testing.T) {
+                       g := &GraphTraversalSource{graph: &Graph{}, bytecode: 
NewBytecode(nil), remoteConnection: nil}
+                       traversal := g.With("foo", "bar")
+                       assert.NotNil(t, traversal)
+                       assert.Equal(t, 1, 
len(traversal.bytecode.sourceInstructions))
+                       instruction := traversal.bytecode.sourceInstructions[0]
+                       assert.Equal(t, "withStrategies", instruction.operator)
+                       assert.Equal(t, 
"org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.OptionsStrategy",
+                               
instruction.arguments[0].(*traversalStrategy).name)
+                       config := 
instruction.arguments[0].(*traversalStrategy).configuration
+                       assert.Equal(t, map[string]interface{}{"foo": "bar"}, 
config)
+               })
+
+               t.Run("Test for multiple param", func(t *testing.T) {
+                       g := &GraphTraversalSource{graph: &Graph{}, bytecode: 
NewBytecode(nil), remoteConnection: nil}
+                       traversal := g.With("foo", "bar").With("foo2", "bar2")
+                       assert.NotNil(t, traversal)
+                       assert.Equal(t, 1, 
len(traversal.bytecode.sourceInstructions))
+                       instruction := traversal.bytecode.sourceInstructions[0]
+                       assert.Equal(t, "withStrategies", instruction.operator)
+                       assert.Equal(t, 
"org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.OptionsStrategy",
+                               
instruction.arguments[0].(*traversalStrategy).name)
+                       config := 
instruction.arguments[0].(*traversalStrategy).configuration
+                       assert.Equal(t, map[string]interface{}{"foo": "bar", 
"foo2": "bar2"}, config)
+               })
+
+               t.Run("Test for param replacement", func(t *testing.T) {
+                       g := &GraphTraversalSource{graph: &Graph{}, bytecode: 
NewBytecode(nil), remoteConnection: nil}
+                       traversal := g.With("foo", "bar").With("foo", "not bar")
+                       assert.NotNil(t, traversal)
+                       assert.Equal(t, 1, 
len(traversal.bytecode.sourceInstructions))
+                       instruction := traversal.bytecode.sourceInstructions[0]
+                       assert.Equal(t, "withStrategies", instruction.operator)
+                       assert.Equal(t, 
"org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.OptionsStrategy",
+                               
instruction.arguments[0].(*traversalStrategy).name)
+                       config := 
instruction.arguments[0].(*traversalStrategy).configuration
+                       assert.Equal(t, map[string]interface{}{"foo": "not 
bar"}, config)
+               })
+       })
+}

Reply via email to