Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GremlinLang to Go driver #2965

Merged
merged 3 commits into from
Jan 28, 2025
Merged

Add GremlinLang to Go driver #2965

merged 3 commits into from
Jan 28, 2025

Conversation

xiazcy
Copy link
Contributor

@xiazcy xiazcy commented Jan 6, 2025

Base set of changes to add GremlinLang to Go driver, adapted from the originally translator. Datetime and strategies will be updated separately.
Additional clean-up and updates will be done once remote connection is fully set-up, tests will also remain disabled until then.

// submitGremlinLang submits GremlinLang to the server to execute and returns a ResultSet.
// TODO test and update when connection is set up
func (client *Client) submitGremlinLang(gremlinLang *GremlinLang) (ResultSet, error) {
client.logHandler.logf(Debug, submitStartedBytecode, *gremlinLang)
Copy link
Contributor

Choose a reason for hiding this comment

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

The log message should be changed to gremlin lang

// TODO test and update when connection is set up
func (driver *DriverRemoteConnection) submitGremlinLang(gremlinLang *GremlinLang) (ResultSet, error) {
if driver.isClosed {
return nil, newError(err0203SubmitBytecodeToClosedConnectionError)
Copy link
Contributor

Choose a reason for hiding this comment

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

Error message should be updated to reference gremlin lang.

@@ -922,7 +925,6 @@ func (t *Transaction) Begin() (*GraphTraversalSource, error) {

gts := &GraphTraversalSource{
graph: t.g.graph,
bytecode: t.g.bytecode,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be replaced by gremlinLang: t.g.gremlinLang,?

@@ -42,95 +42,88 @@ func NewGraphTraversalSource(graph *Graph, remoteConnection *DriverRemoteConnect
convertedArgs := convertStrategyVarargs(traversalStrategies)
bc := NewBytecode(nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should remove bc?

}
}
//TODO verify
var optionsStrategy TraversalStrategy = gts.gremlinLang.optionsStrategies[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible for optionStrategies to be empty and produce out of bounds error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's part of next step, which is to revisit how options is applied in go.

)

type GremlinLang struct {
emptyArray []interface{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be [0]interface{} so that it cannot be mutated to be non-empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll keep this in mind and revisit. Current I've set it up where the method requires[]interface{} to be returned.


func (gl *GremlinLang) addToGremlin(name string, args ...interface{}) {
flattenedArgs := gl.flattenArguments(args...)
//flattenedArgs := reflect.ValueOf(gl.flattenArguments(args...))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should remove commented out code

gl.gremlin.WriteString("Cardinality.")
//str0, _ := gl.argAsString(flattenedArgs.Index(0))
//str1, _ := gl.argAsString(flattenedArgs.Index(1))
str0, _ := gl.argAsString(flattenedArgs[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

Should there be error handling if the flattenedArgs are not an array of exactly 2 items as expected?

}
}

//var withOptionsMap map[any]string = map[any]string{
Copy link
Contributor

Choose a reason for hiding this comment

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

Should delete commented out code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll be revisiting this. I'll add a TODO to remind me

switch v := arg.(type) {
case string:
// TODO check escaping & formatting
return fmt.Sprintf("\"%s\"", html.EscapeString(v)), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

How about %q instead of %s + html.EscapeString(v) (since I don't think we want HTML syntax)?

https://pkg.go.dev/fmt#hdr-Printing

%q a double-quoted string safely escaped with Go syntax

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a deeper dive is warranted here. The requirement is for it to be properly escaped according to gremlin-lang syntax (cannot close out the string literal and start writing subsequent steps). It's not immediately clear that Go or HTML escaping meets this requirement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, I think since we are concerned mainly with single and double quotes I'll just create a string replacer

return "null", nil
}

switch v := arg.(type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to worry about a few types not included in this switch statement such as complex64, complex128, uintptr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we consider them, but I'll keep track of this and will come back to visit this point

Copy link
Contributor

Choose a reason for hiding this comment

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

Should the line t.Bytecode.AddStep("discard") be converted to gremlin lang?

return fmt.Sprintf("%s.%s", strings.ToUpper(name[:1])+name[1:], v), nil
case *Vertex:
id, _ := gl.argAsString(v.Id)
return fmt.Sprintf("new ReferenceVertex(%s,\"%s\")", id, v.Label), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

There may be a need to escape the id and label here.

return paramName
}

func (gl *GremlinLang) GetGremlin(arg ...string) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I wonder if we should limit using this vargargs pattern for psuedo-overloaded methods in favour of something more idiomatic in go. Feel free to ignore this request if you'd like as this pattern is already heavily used throughout gremlin-go.

@xiazcy
Copy link
Contributor Author

xiazcy commented Jan 27, 2025

VOTE +1

@xiazcy xiazcy changed the base branch from master to gremlin-go-http January 27, 2025 23:47
@Cole-Greer
Copy link
Contributor

VOTE +1

@xiazcy
Copy link
Contributor Author

xiazcy commented Jan 28, 2025

Merging to a feature branch instead. Will open PR to release branch for official review process once the go driver is fully complete.

@xiazcy xiazcy merged commit 980ec24 into gremlin-go-http Jan 28, 2025
36 checks passed
@xiazcy xiazcy deleted the go-gremlin-lang branch January 28, 2025 02:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants