本文整理汇总了Golang中github.com/robertkrimen/otto.Value.Class方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Class方法的具体用法?Golang Value.Class怎么用?Golang Value.Class使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/robertkrimen/otto.Value
的用法示例。
在下文中一共展示了Value.Class方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: formatOneLine
func formatOneLine(v otto.Value, limit int, seen map[otto.Value]bool) (string, error) {
if limit == 0 {
return "...", nil
}
switch {
case v.IsBoolean(), v.IsNull(), v.IsNumber(), v.IsUndefined():
return v.String(), nil
case v.IsString():
return fmt.Sprintf("%q", v.String()), nil
case v.IsFunction():
n, err := v.Object().Get("name")
if err != nil {
return "", err
}
if n.IsUndefined() {
return "function", nil
}
return fmt.Sprintf("function %s", n.String()), nil
case v.IsObject():
switch v.Class() {
case "Array":
return formatArrayOneLine(v, limit, seen)
default:
return formatObjectOneLine(v, limit, seen)
}
default:
return "", fmt.Errorf("couldn't format type %s", v.Class())
}
}
示例2: ottoToGo
// Converts an Otto value to a Go value. Handles all JSON-compatible types.
func ottoToGo(value otto.Value) (interface{}, error) {
if value.IsBoolean() {
return value.ToBoolean()
} else if value.IsNull() || value.IsUndefined() {
return nil, nil
} else if value.IsNumber() {
return value.ToFloat()
} else if value.IsString() {
return value.ToString()
} else {
switch value.Class() {
case "Array":
return ottoToGoArray(value.Object())
}
}
return nil, fmt.Errorf("Unsupported Otto value: %v", value)
}
示例3: buildIteratorFromValue
func buildIteratorFromValue(val otto.Value, ts graph.TripleStore) graph.Iterator {
if val.IsNull() || val.IsUndefined() {
return ts.GetNodesAllIterator()
}
if val.IsPrimitive() {
thing, _ := val.Export()
switch v := thing.(type) {
case string:
it := ts.MakeFixed()
it.AddValue(ts.GetIdFor(v))
return it
default:
glog.Errorln("Trying to build unknown primitive value.")
}
}
switch val.Class() {
case "Object":
return buildIteratorTree(val.Object(), ts)
case "Array":
// Had better be an array of strings
strings := makeListOfStringsFromArrayValue(val.Object())
it := ts.MakeFixed()
for _, x := range strings {
it.AddValue(ts.GetIdFor(x))
}
return it
case "Number":
fallthrough
case "Boolean":
fallthrough
case "Date":
fallthrough
case "String":
it := ts.MakeFixed()
str, _ := val.ToString()
it.AddValue(ts.GetIdFor(str))
return it
default:
glog.Errorln("Trying to handle unsupported Javascript value.")
return graph.NewNullIterator()
}
}
示例4: buildIteratorFromValue
func buildIteratorFromValue(val otto.Value, qs graph.QuadStore) graph.Iterator {
if val.IsNull() || val.IsUndefined() {
return qs.NodesAllIterator()
}
if val.IsPrimitive() {
thing, _ := val.Export()
switch v := thing.(type) {
case string:
it := qs.FixedIterator()
it.Add(qs.ValueOf(v))
return it
default:
glog.Errorln("Trying to build unknown primitive value.")
}
}
switch val.Class() {
case "Object":
return buildIteratorTree(val.Object(), qs)
case "Array":
// Had better be an array of strings
strings := stringsFrom(val.Object())
it := qs.FixedIterator()
for _, x := range strings {
it.Add(qs.ValueOf(x))
}
return it
case "Number":
fallthrough
case "Boolean":
fallthrough
case "Date":
fallthrough
case "String":
it := qs.FixedIterator()
it.Add(qs.ValueOf(val.String()))
return it
default:
glog.Errorln("Trying to handle unsupported Javascript value.")
return iterator.NewNull()
}
}
示例5: buildPathFromValue
func buildPathFromValue(val otto.Value) (out []interface{}) {
if val.IsNull() || val.IsUndefined() {
return nil
}
if val.IsPrimitive() {
thing, _ := val.Export()
switch v := thing.(type) {
case string:
out = append(out, v)
return
default:
clog.Errorf("Trying to build unknown primitive value.")
}
}
switch val.Class() {
case "Object":
out = append(out, buildPathFromObject(val.Object()))
return
case "Array":
// Had better be an array of strings
for _, x := range stringsFrom(val.Object()) {
out = append(out, x)
}
return
case "Number":
fallthrough
case "Boolean":
fallthrough
case "Date":
fallthrough
case "String":
out = append(out, val.String())
return
default:
clog.Errorf("Trying to handle unsupported Javascript value.")
return nil
}
}
示例6: formatIndent
func formatIndent(v otto.Value, width, indent, limit, level, additional int, seen map[otto.Value]bool) (string, error) {
if limit == 0 {
return "...", nil
}
switch {
case v.IsBoolean(), v.IsNull(), v.IsNumber(), v.IsUndefined():
return v.String(), nil
case v.IsString():
return fmt.Sprintf("%q", v.String()), nil
case v.IsFunction():
n, err := v.Object().Get("name")
if err != nil {
return "", err
}
if n.IsUndefined() {
return "function", nil
}
return fmt.Sprintf("function %s", n.String()), nil
case v.IsObject():
if d, err := formatOneLine(v, limit, seen); err != nil {
return "", err
} else if level*indent+additional+len(d) <= width {
return d, nil
}
switch v.Class() {
case "Array":
return formatArray(v, width, indent, limit, level, seen)
default:
return formatObject(v, width, indent, limit, level, seen)
}
default:
return "", fmt.Errorf("couldn't format type %s", v.Class())
}
}
示例7: isOttoArray
func isOttoArray(value otto.Value) bool {
return value.Class() == "Array" || value.Class() == "GoArray"
}