Consider the following method which executes a gremlin query in Go and then interprets or parses the results.
func (n NeptuneGremlinGraph) Query(assetID string, version string, entityID string) ([]hz.Component, error) {
defer utils.TimeTracker(time.Now(), fmt.Sprintf("Graph Query"))
g := gremlin.Traversal_().WithRemote(n.connection)
anonT := gremlin.T__
results, err := g.V(makeId(assetID, version, entityID)).
Repeat(anonT.As("a").InE().OutV().SimplePath()).
Emit().Until(anonT.OutE().Count().Is(0)).
Filter(anonT.HasLabel("Component")).
Project("entity", "component").
By(anonT.Select("a").ElementMap()).
By(anonT.ElementMap()).
ToList()
if err != nil {
return nil, err
}
cnt := 0
for _, r := range results {
var entityID, componentID, value string
if m, ok := r.Data.(map[any]any); ok {
if entity, ok := m["entity"]; ok {
if entity, ok := entity.(map[any]any); ok {
if id, ok := entity["id"]; ok {
if id, ok := id.(string); ok {
_, _, entityID = splitId(id)
}
}
}
}
if component, ok := m["component"]; ok {
if component, ok := component.(map[any]any); ok {
if id, ok := component["component_id"]; ok {
if id, ok := id.(string); ok {
componentID = id
}
}
if v, ok := component["value"]; ok {
if v, ok := v.(string); ok {
value = v
}
}
}
}
log.Printf("%s, %s, %s\n", entityID, componentID, value)
} else {
log.Printf("not a map\n")
}
}
log.Printf("path cnt = %d\n", cnt)
return nil, nil
}
Obviously I could add helper methods to clean up the query processing code. But either way the query processing code has to deal with multiple layers of map[any]any and any values.
Am I missing some methods in the driver Result object that make this easier?