Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions gremlin-go/driver/gremlinlang.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions gremlin-go/driver/gremlinlang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\\\"\")",
Comment on lines +679 to +681
Copy link
Copy Markdown
Contributor

@Cole-Greer Cole-Greer Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Throw a single quote in there too

Suggested change
return g.V().Has("name", "\"marko\n\r\t\b\f\"")
},
equals: "g.V().has(\"name\",\"\\\"marko\\n\\r\\t\\b\\f\\\"\")",
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
Expand Down
Loading