valueMap and Multivalues

I was going to use the recipe from @KelvinL 's book to return lists only when the property has multiple values:
l, err := g.G.V(id).HasLabel("geoname").
ValueMap().With(gremlingo.WithOptions.Tokens).
Map(__.Unfold()).
Group().By(Keys).
By(__.Choose(__.Select(Values).Count(Local).Is(1),
__.Select(Values).Unfold(),
__.Select(Values))).
Unfold().
ToList()
l, err := g.G.V(id).HasLabel("geoname").
ValueMap().With(gremlingo.WithOptions.Tokens).
Map(__.Unfold()).
Group().By(Keys).
By(__.Choose(__.Select(Values).Count(Local).Is(1),
__.Select(Values).Unfold(),
__.Select(Values))).
Unfold().
ToList()
However, what I want is for the properites in Neptune that have been specified as multi-valued to always be lists even if they only currently have one value. This is because I want to populate my go structures from the return properties. I don't immediately see any way to do that. It's fine, I can code around it and just return all properties as lists, but I wondered if there was a way to specify that behavior? We have the two extremes of all lists with ValueMap and all single (even if Multivalued) with ElementMap(), and nothing in between. BUt maybe I am overlooking some WithOption?
6 Replies
spmallette
spmallette15mo ago
Given TinkerPop's limited understanding of a graph's schema it's hard for Gremlin to automatically take a particular action given a property's type/cardinality. I'm afraid that there's really no option that works the way you want at this time and I think that the only way you could have something like that is for you to explicitly writ your Gremlin to do that. Since you have knowledge of your schema you could obviously write your Gremlin to format the data however you like. The downside of course is that you have to build explicit if/then sort of logic into your code to check for keys and then format appropriately. It will lead to more complicated looking Gremlin. I've often though that a DSL would help here. You could mask that complexity behind a one or more steps that would convert graph elements to a Map where you could encode all the logic related to schema in a single place.
Jim Idle
Jim Idle15mo ago
DSLs and language design are my thing, I was thinking about such a thing for my own specialized data to make it easier for our own people to come up with a traversal. I suppose a more general DSL could be written too. I wonder though if such a thing perhaps adds complexity that isn't need?
spmallette
spmallette15mo ago
our DSL implementation patterns are pretty easy to implement typically, so there's usually not much complexity in that part. generally speaking i think that the complexity they hide can be incredibly helpful to Gremlin readability and learnability. i think this example of a zip() step from a talk i gave on DSLs is an extreme representation of that but it's fun to look at:
No description
spmallette
spmallette15mo ago
the neat thing about zip() was that this step itself was composed of individual DSL steps which even made it easy to read. so i think if you get the composability right for your steps a DSL can be a really powerful tool in Gremlin writing
Jim Idle
Jim Idle15mo ago
I agree with this. A DSL can also help to enforce the use of known best patterns and so on
kelvinl2816
kelvinl281615mo ago
It's amazing how useful index ended up being. So many things can be implemented using it. Reversing a list being another.
Want results from more Discord servers?
Add your server