// pipelines in javascript are chainable, you take in a pipeline, and you return a pipeline
// we just generalize some of that logic here
func (js *JavascriptBuilder) setFunc(obj *otto.Object, token string, fn func(string, Node, otto.FunctionCall) (Node, error)) error {
	return obj.Set(token, func(call otto.FunctionCall) otto.Value {
		this, _ := call.This.Export()

		node, err := CreateNode(this)
		if err != nil {
			js.err = err
			return otto.NullValue()
		}

		node, err = fn(token, node, call)
		if err != nil {
			js.err = err
			return otto.NullValue()
		}

		o, err := node.Object()
		if err != nil {
			js.err = err
			return otto.NullValue()
		}

		js.setFunc(o, "transform", js.transform)
		js.setFunc(o, "save", js.save)

		return o.Value()
	})
}
Beispiel #2
0
func ResponseWriterToJso(o *otto.Object, w http.ResponseWriter) {
	o.Set("write", func(c otto.FunctionCall) otto.Value {
		text, _ := c.Argument(0).ToString()
		w.Write([]byte(text))
		return otto.TrueValue()
	})
	o.Set("writeHeader", func(c otto.FunctionCall) otto.Value {
		statusCode, _ := c.Argument(0).ToInteger()
		w.WriteHeader(int(statusCode))
		return otto.TrueValue()
	})
}
Beispiel #3
0
func embedFinals(env *otto.Otto, ses *Session, obj *otto.Object) {
	obj.Set("All", allFunc(env, ses, obj))
	obj.Set("GetLimit", limitFunc(env, ses, obj))
	obj.Set("ToArray", toArrayFunc(env, ses, obj, false))
	obj.Set("ToValue", toValueFunc(env, ses, obj, false))
	obj.Set("TagArray", toArrayFunc(env, ses, obj, true))
	obj.Set("TagValue", toValueFunc(env, ses, obj, true))
	obj.Set("Map", mapFunc(env, ses, obj))
	obj.Set("ForEach", mapFunc(env, ses, obj))
}
Beispiel #4
0
func (wk *worker) embedFinals(env *otto.Otto, obj *otto.Object) {
	obj.Set("All", wk.allFunc(env, obj))
	obj.Set("GetLimit", wk.limitFunc(env, obj))
	obj.Set("ToArray", wk.toArrayFunc(env, obj, false))
	obj.Set("ToValue", wk.toValueFunc(env, obj, false))
	obj.Set("TagArray", wk.toArrayFunc(env, obj, true))
	obj.Set("TagValue", wk.toValueFunc(env, obj, true))
	obj.Set("Map", wk.mapFunc(env, obj))
	obj.Set("ForEach", wk.mapFunc(env, obj))
}
Beispiel #5
0
func (wk *worker) embedTraversals(env *otto.Otto, obj *otto.Object) {
	for _, t := range traversals {
		obj.Set(t, wk.gremlinFunc(t, obj, env))
	}
}
Beispiel #6
0
func embedTraversals(env *otto.Otto, ses *GremlinSession, obj *otto.Object) {
	obj.Set("In", gremlinFunc("in", obj, env, ses))
	obj.Set("Out", gremlinFunc("out", obj, env, ses))
	obj.Set("Is", gremlinFunc("is", obj, env, ses))
	obj.Set("Both", gremlinFunc("both", obj, env, ses))
	obj.Set("Follow", gremlinFunc("follow", obj, env, ses))
	obj.Set("FollowR", gremlinFollowR("followr", obj, env, ses))
	obj.Set("And", gremlinFunc("and", obj, env, ses))
	obj.Set("Intersect", gremlinFunc("and", obj, env, ses))
	obj.Set("Union", gremlinFunc("or", obj, env, ses))
	obj.Set("Or", gremlinFunc("or", obj, env, ses))
	obj.Set("Back", gremlinBack("back", obj, env, ses))
	obj.Set("Tag", gremlinFunc("tag", obj, env, ses))
	obj.Set("As", gremlinFunc("tag", obj, env, ses))
	obj.Set("Has", gremlinFunc("has", obj, env, ses))
	obj.Set("Save", gremlinFunc("save", obj, env, ses))
	obj.Set("SaveR", gremlinFunc("saver", obj, env, ses))
}
Beispiel #7
0
func RequestToJso(o *otto.Object, r *http.Request) {
	o.Set("url", r.URL.String())
	o.Set("header", r.Header)
	o.Set("cookies", r.Cookies())
	o.Set("method", r.Method)
	o.Set("host", r.Host)
	o.Set("contentLength", r.ContentLength)
	o.Set("proto", r.Proto)
	o.Set("transferEncoding", r.TransferEncoding)
	r.ParseForm()
	o.Set("formValues", r.Form)
}
Beispiel #8
0
func ResponseToJso(o *otto.Object, w *http.Response) {
	o.Set("status", w.Status)
	o.Set("header", w.Header)
	o.Set("cookies", w.Cookies())
	o.Set("statusCode", w.StatusCode)
	o.Set("proto", w.Proto)
	c, _ := ioutil.ReadAll(w.Body)
	o.Set("body", string(c))
	if w.TLS != nil {
		o.Set("tlsServerName", w.TLS.ServerName)
		o.Set("tlsNegotiatedProtocol", w.TLS.NegotiatedProtocol)
		names := []string{}
		ips := []string{}
		for _, k := range w.TLS.PeerCertificates {
			for _, n := range k.DNSNames {
				names = append(names, n)
			}
			for _, n := range k.IPAddresses {
				ips = append(ips, n.String())
			}
		}
		o.Set("tlsDNSNames", names)
		o.Set("tlsIPs", ips)
	}
}
Beispiel #9
0
func (wk *worker) embedTraversals(env *otto.Otto, obj *otto.Object) {
	obj.Set("In", wk.gremlinFunc("in", obj, env))
	obj.Set("Out", wk.gremlinFunc("out", obj, env))
	obj.Set("Is", wk.gremlinFunc("is", obj, env))
	obj.Set("Both", wk.gremlinFunc("both", obj, env))
	obj.Set("Follow", wk.gremlinFunc("follow", obj, env))
	obj.Set("FollowR", wk.gremlinFollowR("followr", obj, env))
	obj.Set("And", wk.gremlinFunc("and", obj, env))
	obj.Set("Intersect", wk.gremlinFunc("and", obj, env))
	obj.Set("Union", wk.gremlinFunc("or", obj, env))
	obj.Set("Or", wk.gremlinFunc("or", obj, env))
	obj.Set("Back", wk.gremlinBack("back", obj, env))
	obj.Set("Tag", wk.gremlinFunc("tag", obj, env))
	obj.Set("As", wk.gremlinFunc("tag", obj, env))
	obj.Set("Has", wk.gremlinFunc("has", obj, env))
	obj.Set("Save", wk.gremlinFunc("save", obj, env))
	obj.Set("SaveR", wk.gremlinFunc("saver", obj, env))
	obj.Set("Except", wk.gremlinFunc("except", obj, env))
	obj.Set("Difference", wk.gremlinFunc("except", obj, env))
	obj.Set("InPredicates", wk.gremlinFunc("in_predicates", obj, env))
	obj.Set("OutPredicates", wk.gremlinFunc("out_predicates", obj, env))
}