本文整理匯總了Golang中github.com/robertkrimen/otto.NullValue函數的典型用法代碼示例。如果您正苦於以下問題:Golang NullValue函數的具體用法?Golang NullValue怎麽用?Golang NullValue使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NullValue函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: addDiscoveryService
func (ctrl *JsController) addDiscoveryService(o *otto.Otto) {
o.Set("discover", func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) == 0 {
glog.Errorf("DISCOVER: Missing arguments")
return otto.NullValue()
}
url, _ := call.Argument(0).ToString()
upstreams, err := ctrl.DiscoveryService.Get(url)
if err != nil {
glog.Errorf("Failed to discover upstreams: %v", err)
return otto.NullValue()
}
glog.Infof("Discovered upstreams: %v", upstreams)
result, err := o.ToValue(upstreams)
if err != nil {
glog.Errorf("Failed to convert: %v", err)
return otto.NullValue()
}
return result
})
}
示例2: toValue
func (p *pathObject) toValue(call otto.FunctionCall, withTags bool) otto.Value {
it := p.buildIteratorTree()
it.Tagger().Add(TopResultTag)
const limit = 1
var (
val otto.Value
err error
)
if !withTags {
array := p.wk.runIteratorToArrayNoTags(it, limit)
if len(array) < 1 {
return otto.NullValue()
}
val, err = call.Otto.ToValue(array[0])
} else {
array := p.wk.runIteratorToArray(it, limit)
if len(array) < 1 {
return otto.NullValue()
}
val, err = call.Otto.ToValue(array[0])
}
if err != nil {
glog.Error(err)
return otto.NullValue()
}
return val
}
示例3: setFunc
// 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()
})
}
示例4: toValueFunc
func (wk *worker) toValueFunc(env *otto.Otto, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value {
it := buildIteratorTree(obj, wk.qs)
it.Tagger().Add(TopResultTag)
limit := 1
var val otto.Value
var err error
if !withTags {
array := wk.runIteratorToArrayNoTags(it, limit)
if len(array) < 1 {
return otto.NullValue()
}
val, err = call.Otto.ToValue(array[0])
} else {
array := wk.runIteratorToArray(it, limit)
if len(array) < 1 {
return otto.NullValue()
}
val, err = call.Otto.ToValue(array[0])
}
if err != nil {
clog.Errorf("%v", err)
return otto.NullValue()
}
return val
}
}
示例5: has
func (p *pathObject) has(call otto.FunctionCall, rev bool) otto.Value {
args := exportArgs(call.ArgumentList)
if len(args) == 0 {
return otto.NullValue()
}
via := args[0]
if vp, ok := via.(*pathObject); ok {
via = vp.path
} else {
via, ok = toQuadValue(via)
if !ok {
return otto.NullValue()
}
}
qv, err := toQuadValues(args[1:])
if err != nil {
//TODO(dennwc): pass error to caller
return otto.NullValue()
}
np := p.clonePath()
if rev {
np = np.HasReverse(via, qv...)
} else {
np = np.Has(via, qv...)
}
return outObj(call, p.new(np))
}
示例6: save
func (p *pathObject) save(call otto.FunctionCall, rev bool) otto.Value {
args := exportArgs(call.ArgumentList)
if len(args) > 2 || len(args) == 0 {
return otto.NullValue()
}
vtag := args[0]
if len(args) == 2 {
vtag = args[1]
}
tag, ok := vtag.(string)
if !ok {
return otto.NullValue()
}
via := args[0]
if vp, ok := via.(*pathObject); ok {
via = vp.path
} else {
via, ok = toQuadValue(via)
if !ok {
return otto.NullValue()
}
}
np := p.clonePath()
if rev {
np = np.SaveReverse(via, tag)
} else {
np = np.Save(via, tag)
}
return outObj(call, p.new(np))
}
示例7: convertNilsToNulls
func convertNilsToNulls(object interface{}) {
switch object := object.(type) {
case map[string]interface{}:
for key, obj := range object {
switch obj := obj.(type) {
case map[string]interface{}:
convertNilsToNulls(obj)
case []interface{}:
convertNilsToNulls(obj)
case nil:
object[key] = otto.NullValue()
}
}
case []interface{}:
for key, obj := range object {
switch obj := obj.(type) {
case map[string]interface{}:
convertNilsToNulls(obj)
case []interface{}:
convertNilsToNulls(obj)
case nil:
object[key] = otto.NullValue()
}
}
}
}
示例8: toArray
func (p *pathObject) toArray(call otto.FunctionCall, withTags bool) otto.Value {
args := exportArgs(call.ArgumentList)
if len(args) != 1 {
return otto.NullValue()
}
limit := -1
if len(args) > 0 {
limit = toInt(args[0])
}
it := p.buildIteratorTree()
it.Tagger().Add(TopResultTag)
var (
val otto.Value
err error
)
if !withTags {
array := p.wk.runIteratorToArrayNoTags(it, limit)
val, err = call.Otto.ToValue(array)
} else {
array := p.wk.runIteratorToArray(it, limit)
val, err = call.Otto.ToValue(array)
}
if err != nil {
glog.Error(err)
return otto.NullValue()
}
return val
}
示例9: toValueFunc
func toValueFunc(env *otto.Otto, ses *Session, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value {
it := buildIteratorTree(obj, ses.ts)
it.Tagger().Add(TopResultTag)
limit := 1
var val otto.Value
var err error
if !withTags {
array := runIteratorToArrayNoTags(it, ses, limit)
if len(array) < 1 {
return otto.NullValue()
}
val, err = call.Otto.ToValue(array[0])
} else {
array := runIteratorToArray(it, ses, limit)
if len(array) < 1 {
return otto.NullValue()
}
val, err = call.Otto.ToValue(array[0])
}
if err != nil {
glog.Error(err)
return otto.NullValue()
} else {
return val
}
}
}
示例10: cmpOpType
func cmpOpType(op iterator.Operator) func(call otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value {
args := exportArgs(call.ArgumentList)
if len(args) != 1 {
return otto.NullValue()
}
qv, ok := toQuadValue(args[0])
if !ok {
return otto.NullValue()
}
return outObj(call, cmpOperator{op: op, val: qv})
}
}
示例11: runCompScript
func (j *job) runCompScript(irs [][]*gnr.InteractionResult) {
vm := otto.New()
injectRuntime(vm)
if err := vm.Set("interactions", irs); err != nil {
panic(err)
}
vm.Set("width", Width)
vm.Set("height", Height)
compositions := make([]composition, 0, 5)
vm.Set("composition", func(call otto.FunctionCall) otto.Value {
name, err := call.Argument(0).ToString()
if err != nil {
panic("First argument to composition() must be a string")
}
cb := call.Argument(1)
if !cb.IsFunction() {
panic("Second argument to composition() must be a function(x,y)")
}
compositions = append(compositions, composition{name, cb})
return otto.NullValue()
})
if _, err := vm.Run(j.Composite); err != nil {
panic(err)
}
for _, comp := range compositions {
img := image.NewRGBA(image.Rect(0, 0, Width, Height))
for y := 0; y < Height; y++ {
for x := 0; x < Width; x++ {
v, err := comp.Func.Call(otto.NullValue(), x, y)
if err != nil {
panic(err)
}
c, err := v.Export()
if err != nil {
panic(err)
}
img.Set(x, y, mapToColor(c.(map[string]interface{})))
}
}
buf := &bytes.Buffer{}
if err := png.Encode(buf, img); err != nil {
panic(err)
}
j.Compositions[comp.Name] = buf.Bytes()
}
}
示例12: reverseGremlinChainTo
func reverseGremlinChainTo(env *otto.Otto, prevObj *otto.Object, tag string) (*otto.Object, *otto.Object) {
env.Run("var _base_object = {}")
base, err := env.Object("_base_object")
if err != nil {
glog.Error(err)
return otto.NullValue().Object(), otto.NullValue().Object()
}
if isVertexChain(prevObj) {
base.Set("_gremlin_type", "vertex")
} else {
base.Set("_gremlin_type", "morphism")
}
return reverseGremlinChainHelper(env, prevObj, base, tag)
}
示例13: ForEach
func (p *pathObject) ForEach(call otto.FunctionCall) otto.Value {
it := p.buildIteratorTree()
it.Tagger().Add(TopResultTag)
limit := -1
if len(call.ArgumentList) == 0 {
return otto.NullValue()
}
callback := call.Argument(len(call.ArgumentList) - 1)
args := exportArgs(call.ArgumentList[:len(call.ArgumentList)-1])
if len(args) > 1 {
limit = toInt(args[0])
}
p.wk.runIteratorWithCallback(it, callback, call, limit)
return otto.NullValue()
}
示例14: Filter
func (p *pathObject) Filter(call otto.FunctionCall) otto.Value {
args := exportArgs(call.ArgumentList)
if len(args) == 0 {
return otto.NullValue()
}
np := p.clonePath()
for _, arg := range args {
op, ok := arg.(cmpOperator)
if !ok {
return otto.NullValue()
}
np = np.Filter(op.op, op.val)
}
return outObj(call, p.new(np))
}
示例15: DirectCallFunction
// Invokes the JS function. Not thread-safe! This is exposed for use by unit tests.
func (server *JSServer) DirectCallFunction(inputs []string) (interface{}, error) {
if server.Before != nil {
server.Before()
}
var result otto.Value
var err error
if server.fn.IsUndefined() {
result = otto.UndefinedValue()
} else {
inputJS := make([]interface{}, len(inputs))
for i, inputStr := range inputs {
if inputStr == "" {
inputJS[i] = otto.NullValue()
} else {
var err error
inputJS[i], err = server.js.Object("x = " + inputStr)
if err != nil {
return nil, fmt.Errorf("Unparseable input %q: %s", inputStr, err)
}
}
}
result, err = server.fn.Call(server.fn, inputJS...)
}
if server.After != nil {
return server.After(result, err)
}
return nil, err
}