Exemple #1
0
func convertValToMysqlConnInfo(v *otto.Value) *MysqlConnInfo {
	connInfo := &MysqlConnInfo{}

	if v.IsDefined() && v.IsObject() {
		obj := v.Object()
		// get username
		if u, err := obj.Get("username"); err == nil && u.IsString() {
			connInfo.Username, _ = u.ToString()
		}

		// get password
		if p, err := obj.Get("password"); err == nil && p.IsString() {
			connInfo.Password, _ = p.ToString()
		}

		// get host
		if h, err := obj.Get("host"); err == nil && h.IsString() {
			connInfo.Host, _ = h.ToString()
		}

		// get port
		if p, err := obj.Get("port"); err == nil && p.IsNumber() {
			connInfo.Port, _ = p.ToInteger()
		}
	}

	return connInfo
}
Exemple #2
0
// Execute Javascript
func (sh *Shell) execScript(editor bool) stateFn {
	var out string
	var value otto.Value
	var err error

	if !editor {
		tmp := strings.TrimPrefix(sh.input, RunJavascript) // remove script meta-command
		value, err = sh.jsengine.Run(tmp)
	} else {
		value, err = sh.jsengine.Run(sh.input)
	}

	if err != nil {
		out = err.Error()
	} else if value.IsDefined() {
		out = value.String()
	}

	sh.write(out)
	if !editor {
		sh.line.AppendHistory(sh.input)
	}

	return bqlPrompt
}
Exemple #3
0
func convertValToMap(v *otto.Value) map[string]interface{} {
	if v.IsDefined() && v.IsObject() {
		options, _ := v.Export()
		return options.(map[string]interface{})
	}
	return make(map[string]interface{})
}
// 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
}