Exemple #1
0
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
}
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
}
Exemple #3
0
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
}
Exemple #4
0
// 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)
}
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()
	}
}
Exemple #6
0
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
}
Exemple #7
0
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>")
	}
}
// 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 {
		return nil, err
	}

	return s, nil
}