本文整理汇总了Golang中github.com/robertkrimen/otto.Value.ToString方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.ToString方法的具体用法?Golang Value.ToString怎么用?Golang Value.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/robertkrimen/otto.Value
的用法示例。
在下文中一共展示了Value.ToString方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: fetchConfig
func fetchConfig(funcname string, name string, config interface{}) bool {
var ret otto.Value
var err error
if name == "" {
ret, err = jsVM.Call(funcname, nil)
} else {
ret, err = jsVM.Call(funcname, nil, name)
}
if err != nil {
log.Printf("call failed, in '%s', %v", funcname, err)
return false
}
jsonConfig, err := ret.ToString()
if err != nil {
log.Println(err)
return false
}
if err := json.Unmarshal([]byte(jsonConfig), config); err != nil {
log.Printf("json.Unmarshal failed, in '%s', %v", funcname, err)
return false
}
return true
}
示例2: MustResolve
func (rm refMap) MustResolve(ref otto.Value) interface{} {
key, err := ref.ToString()
if err != nil {
panic(err)
}
v, ok := rm[key]
if !ok {
panic(fmt.Sprintf("Invalid reference: %s", key))
}
return v
}
示例3: ottoRetString
func (p *PacSandbox) ottoRetString(v otto.Value, rte error) (string, error) {
if rte != nil {
return "", rte
}
ret, err := v.ToString()
if err != nil {
return "", err
}
return ret, nil
}
示例4: 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)
}
示例5: 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()
}
}
示例6: convertToPrimitive
func convertToPrimitive(v otto.Value) interface{} {
if v.IsBoolean() {
v_b, err := v.ToBoolean()
if err != nil {
log.Printf("Error converting to boolean")
}
return v_b
} else if v.IsNumber() {
v_f, err := v.ToFloat()
if err != nil {
log.Printf("Error converting to float")
}
return v_f
} else {
v_s, err := v.ToString()
if err != nil {
log.Printf("Error converting to boolean")
}
return v_s
}
return nil
}
示例7: printValue
func (ctx ppctx) printValue(v otto.Value, level int, inArray bool) {
switch {
case v.IsObject():
ctx.printObject(v.Object(), level, inArray)
case v.IsNull():
specialColor.Print("null")
case v.IsUndefined():
specialColor.Print("undefined")
case v.IsString():
s, _ := v.ToString()
stringColor.Printf("%q", s)
case v.IsBoolean():
b, _ := v.ToBoolean()
specialColor.Printf("%t", b)
case v.IsNaN():
numberColor.Printf("NaN")
case v.IsNumber():
s, _ := v.ToString()
numberColor.Printf("%s", s)
default:
fmt.Printf("<unprintable>")
}
}
示例8: getSample
// Helper function to populate a sample structure with a single sample
func (sr *SampleReader) getSample() (*Sample, error) {
var err error
var v otto.Value
s := new(Sample)
// Extract date field from javascript runtime
v, err = sr.vm.Get("date")
if err != nil {
return nil, err
}
if !v.IsDefined() {
return nil, errors.New("date not defined")
}
ds, err := v.ToString()
if err != nil {
return nil, err
}
s.Date, err = time.Parse("2006-01-02T15:04:05", ds)
if err != nil {
return nil, err
}
// Extract latitude field from javascript runtime
v, err = sr.vm.Get("latitude")
if err != nil {
return nil, err
}
if !v.IsDefined() {
return nil, errors.New("latitude not defined")
}
s.Latitude, err = v.ToFloat()
if err != nil {
return nil, err
}
// Extract longitude field from javascript runtime
v, err = sr.vm.Get("longitude")
if err != nil {
return nil, err
}
if !v.IsDefined() {
return nil, errors.New("longitude not defined")
}
s.Longitude, err = v.ToFloat()
if err != nil {
return nil, err
}
// Extract altitude field from javascript runtime
v, err = sr.vm.Get("altitude")
if err != nil {
return nil, err
}
if !v.IsDefined() {
return nil, errors.New("altitude not defined")
}
s.Altitude, err = v.ToFloat()
if err != nil {
return nil, err
}
// Extract value field from javascript runtime
v, err = sr.vm.Get("value")
if err != nil {
return nil, err
}
if !v.IsDefined() {
return nil, errors.New("value not defined")
}
s.Value, err = v.ToFloat()
if err != nil {
return nil, err
}
// Extract unit field from javascript runtime
v, err = sr.vm.Get("unit")
if err != nil {
return nil, err
}
if !v.IsDefined() {
return nil, errors.New("unit not defined")
}
s.Unit, err = v.ToString()
if err != nil {
//.........这里部分代码省略.........