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

colegreer pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git


The following commit(s) were added to refs/heads/master by this push:
     new 72d6881362 GO support for escaped characters (#3381)
72d6881362 is described below

commit 72d688136205d7bef24606f58985f0647dc4d22c
Author: Guian Gumpac <[email protected]>
AuthorDate: Wed Apr 15 12:22:27 2026 -0700

    GO support for escaped characters (#3381)
---
 gremlin-go/driver/gremlinlang.go      | 30 +++++++++++++++++++++++++++---
 gremlin-go/driver/gremlinlang_test.go |  6 ++++++
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/gremlin-go/driver/gremlinlang.go b/gremlin-go/driver/gremlinlang.go
index baec7e6547..2983fbc559 100644
--- a/gremlin-go/driver/gremlinlang.go
+++ b/gremlin-go/driver/gremlinlang.go
@@ -110,16 +110,40 @@ func (gl *GremlinLang) addToGremlin(name string, args 
...interface{}) error {
        return nil
 }
 
+// escapeString escapes a string value for safe embedding in a GremlinLang 
script.
+func escapeString(s string) string {
+       var sb strings.Builder
+       for _, c := range s {
+               switch c {
+               case '\\':
+                       sb.WriteString(`\\`)
+               case '"':
+                       sb.WriteString(`\"`)
+               case '\n':
+                       sb.WriteString(`\n`)
+               case '\r':
+                       sb.WriteString(`\r`)
+               case '\t':
+                       sb.WriteString(`\t`)
+               case '\b':
+                       sb.WriteString(`\b`)
+               case '\f':
+                       sb.WriteString(`\f`)
+               default:
+                       sb.WriteRune(c)
+               }
+       }
+       return sb.String()
+}
+
 func (gl *GremlinLang) argAsString(arg interface{}) (string, error) {
        if arg == nil {
                return "null", nil
        }
-       // we are concerned with both single and double quotes and %q in fmt 
only escapes double quotes
-       escapeQuotes := strings.NewReplacer(`'`, `\'`, `"`, `\"`)
 
        switch v := arg.(type) {
        case string:
-               return fmt.Sprintf("\"%s\"", escapeQuotes.Replace(v)), nil
+               return fmt.Sprintf("\"%s\"", escapeString(v)), nil
        case bool:
                return strconv.FormatBool(v), nil
        case int8, uint8:
diff --git a/gremlin-go/driver/gremlinlang_test.go 
b/gremlin-go/driver/gremlinlang_test.go
index 7160056f24..e9cb5e6d1c 100644
--- a/gremlin-go/driver/gremlinlang_test.go
+++ b/gremlin-go/driver/gremlinlang_test.go
@@ -674,6 +674,12 @@ func Test_GremlinLang(t *testing.T) {
                        },
                        equals: "g.inject(NaN).is(eq(NaN))",
                },
+               {
+                       assert: func(g *GraphTraversalSource) *GraphTraversal {
+                               return g.V().Has("name", "\"marko\n\r\t\b\f\"")
+                       },
+                       equals: 
"g.V().has(\"name\",\"\\\"marko\\n\\r\\t\\b\\f\\\"\")",
+               },
        }
 
        var testsToRun []test

Reply via email to