We've long had the issue of how to deal with string better. Typically the
concern lies with concatenation, but there are other use cases that have
come up along the way as well. I started playing around with a format()
step to try to capture all the odds and ends I have notes on in relation to
this:

Quickly hacked together I have something that allows:

gremlin> g.V().hasLabel('person').format("%s is %s years
old").by('name').by('age')
==>marko is 29 years old
==>vadas is 27 years old
==>josh is 32 years old
==>peter is 35 years old

The engine behind the string formatting is the standard Java Formatter. I
just wanted to see what it could look like so Formatter was an easy choice.
Of course, Formatter might not be best - part of me would prefer a more
non-JVM centric sort of templating, perhaps something like:

g.V().hasLabel('person').format("{} is {} years old").by('name').by('age')

which is fairly commonplace across languages (even used in Java in
libraries like slf4j). That of course made me realize that it wouldn't be
hard to overload format() to take a formatting engine as an argument so
that it's extensible:

g.V().hasLabel('person').format("{} is {} years old").by('name').by('age')

g.V().hasLabel('person').format(JAVA, "%s is %s years
old").by('name').by('age')

The notion of a formatting engine argument made me think about another
thing folks tend to want in relation to strings - clean output to JSON (not
GraphSON exactly with all the embedded types - like think back to GraphSON
1 format) and other string formats:

g.V().hasLabel('person').format(JSON)

or perhaps it is just GraphSON??

g.V().hasLabel('person').format(GraphSON_1)

Providers who require special serializers could easily just override the
FormatStep to configure the engines as necessary.

I think format() helps solve a lot of the common issues with strings and
Gremlin. Even with the basic Formatter you can do a poor man's sort of
substring:

gremlin> g.V().hasLabel('person').format("%1.1s").by('name')
==>m
==>v
==>j
==>p

I'd imagine that with a more advanced engine we could get something more
full featured if we wanted to cover even wider general function use cases.
Not sure if things like substring should be more like first class citizens
in Gremlin or not though. Anyway, happy to hear any thoughts on the idea of
format() and what it might mean to Gremlin.

Reply via email to