Prepare gremlin for introduction of a worker type
This commit is contained in:
parent
a96f2ef6f3
commit
95170eb8ed
5 changed files with 141 additions and 160 deletions
|
|
@ -21,31 +21,25 @@ import (
|
|||
"github.com/robertkrimen/otto"
|
||||
)
|
||||
|
||||
func BuildEnviron(ses *Session) *otto.Otto {
|
||||
env := otto.New()
|
||||
setupGremlin(env, ses)
|
||||
return env
|
||||
}
|
||||
|
||||
func concatStringArgs(call otto.FunctionCall) *[]interface{} {
|
||||
outStrings := make([]interface{}, 0)
|
||||
func argsOf(call otto.FunctionCall) []string {
|
||||
var out []string
|
||||
for _, arg := range call.ArgumentList {
|
||||
if arg.IsString() {
|
||||
outStrings = append(outStrings, arg.String())
|
||||
out = append(out, arg.String())
|
||||
}
|
||||
if arg.IsObject() && arg.Class() == "Array" {
|
||||
obj, _ := arg.Export()
|
||||
for _, x := range obj.([]interface{}) {
|
||||
outStrings = append(outStrings, x.(string))
|
||||
out = append(out, x.(string))
|
||||
}
|
||||
}
|
||||
}
|
||||
return &outStrings
|
||||
return out
|
||||
}
|
||||
|
||||
func isVertexChain(obj *otto.Object) bool {
|
||||
val, _ := obj.Get("_gremlin_type")
|
||||
if x, _ := val.ToString(); x == "vertex" {
|
||||
if val.String() == "vertex" {
|
||||
return true
|
||||
}
|
||||
val, _ = obj.Get("_gremlin_prev")
|
||||
|
|
@ -55,8 +49,10 @@ func isVertexChain(obj *otto.Object) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func setupGremlin(env *otto.Otto, ses *Session) {
|
||||
func (s *Session) setup(env *otto.Otto) *otto.Otto {
|
||||
graph, _ := env.Object("graph = {}")
|
||||
env.Run("g = graph")
|
||||
|
||||
graph.Set("Vertex", func(call otto.FunctionCall) otto.Value {
|
||||
call.Otto.Run("var out = {}")
|
||||
out, err := call.Otto.Object("out")
|
||||
|
|
@ -65,31 +61,32 @@ func setupGremlin(env *otto.Otto, ses *Session) {
|
|||
return otto.TrueValue()
|
||||
}
|
||||
out.Set("_gremlin_type", "vertex")
|
||||
outStrings := concatStringArgs(call)
|
||||
if len(*outStrings) > 0 {
|
||||
out.Set("string_args", *outStrings)
|
||||
args := argsOf(call)
|
||||
if len(args) > 0 {
|
||||
out.Set("string_args", args)
|
||||
}
|
||||
embedTraversals(env, ses, out)
|
||||
embedFinals(env, ses, out)
|
||||
s.embedTraversals(env, out)
|
||||
s.embedFinals(env, out)
|
||||
return out.Value()
|
||||
})
|
||||
env.Run("graph.V = graph.Vertex")
|
||||
|
||||
graph.Set("Morphism", func(call otto.FunctionCall) otto.Value {
|
||||
call.Otto.Run("var out = {}")
|
||||
out, _ := call.Otto.Object("out")
|
||||
out.Set("_gremlin_type", "morphism")
|
||||
embedTraversals(env, ses, out)
|
||||
s.embedTraversals(env, out)
|
||||
return out.Value()
|
||||
})
|
||||
env.Run("graph.M = graph.Morphism")
|
||||
|
||||
graph.Set("Emit", func(call otto.FunctionCall) otto.Value {
|
||||
value := call.Argument(0)
|
||||
if value.IsDefined() {
|
||||
ses.SendResult(&Result{val: &value})
|
||||
s.SendResult(&Result{val: &value})
|
||||
}
|
||||
return otto.NullValue()
|
||||
})
|
||||
env.Run("graph.V = graph.Vertex")
|
||||
env.Run("graph.M = graph.Morphism")
|
||||
env.Run("g = graph")
|
||||
|
||||
return env
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue