I previously received helpful guidance from Kelvin in a Stack Overflow post (linked here: Gremin mergeV() Upsert: knowing whether the vertex was added or not(side-effect free)) regarding the successful execution of a Groovy query using Gremlin's mergeV syntax. This query allowed me to determine whether a vertex was added or obtained without any side effects. Now, I'm encountering issues when attempting to translate that Groovy code into Go code. The Groovy code, which functions correctly in my Gremlin console, is as follows:
g.mergeV([(label): 'principal', spiffe_id: '1']).
option(Merge.onCreate,[creat_at:'10/23/2023', update_at:'11/23/2023']).
option(Merge.onMatch,sideEffect(constant(true).store('found')).constant([:])).
option(Merge.onCreate,sideEffect(constant(false).store('found')).constant([:])).
project('spiffe_id','status'). by(id()). by(select('found').unfold())
However, when attempting to convert this code into Golang, I encounter an error:
code:244 message:class java.util.LinkedHashMap cannot be cast to
class org.apache.tinkerpop.gremlin.structure.Element
Here's the Golang code I'm using:
var __ = gremlingo.T__
steps := graph.g.MergeV(map[interface{}]interface{}{
gremlingo.T.Label: "principal", "spiffe_id": "1"}).
Option(gremlingo.Merge.OnCreate, map[interface{}]interface{}{
"created_at": "10/23/2023", "updated_at": "10/23/2023"}).
Option(gremlingo.Merge.OnCreate, __.SideEffect(__.Constant(false).Store("found")).Constant(map[interface{}]interface{}{})).
Option(gremlingo.Merge.OnMatch, __.SideEffect(__.Constant(true).Store("found")).Constant(map[interface{}]interface{}{})).
Project("principal", "status").By(__.Identity()).By(__.Select("found").Unfold())
queryResult, err := steps.ElementMap().ToList()
...
Upon attempting to comment out the section involving Constant(map[interface{}]interface{}{})
, I encountered a new error: java.lang.Boolean cannot be cast to class java.util.Map
. It seems that the issue arises from the constant([:])
operation in Groovy, which I attempted to replicate in Golang as Constant(map[interface{}]interface{}{})
.
It appears that during the serialization and deserialization process, there's a mismatch where the gremlin Element structure is attempting to utilize a Java LinkedHashMap. I'm uncertain whether this is due to a mistake in my Go code or if it is due to an incompatibility between the gremlin-go client and the Java server.
I'd greatly appreciate any insights or guidance on resolving this. Thank you for your help!
The main reason is that requests for Groovy and Golang are not equivalent.
Error
code:244 message:class java.util.LinkedHashMap cannot be cast to class org.apache.tinkerpop.gremlin.structure.Element
thrown by step ElementMap()
because it cannot work with input other than Element
, but receive Map
produced by previous Project
.
In Groovy example there is no ElementMap()
.
Therefore, a possible solution is to remove ElementMap()
. Or, if you need it, then move inside Project
Project("principal", "status").By(__.Identity().ElementMap()).By(__.Select("found").Unfold())
P.S. Tested on Gremlin Server 3.7.1