Пример #1
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)
}
Пример #2
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
}
Пример #3
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>")
	}
}