Beispiel #1
0
//GetInt64 gets int64 from otto value
func GetInt64(value otto.Value) (result int64, err error) {
	result, err = value.ToInteger()
	if err != nil {
		err = fmt.Errorf(wrongArgumentType, value, "int64")
	}
	return
}
Beispiel #2
0
//GetBool gets bool from otto value
func GetBool(value otto.Value) (bool, error) {
	rawBool, _ := value.Export()
	result, ok := rawBool.(bool)
	if !ok {
		return false, fmt.Errorf(wrongArgumentType, rawBool, "bool")
	}
	return result, nil
}
Beispiel #3
0
//GetAuthorization gets Transaction from otto value
func GetAuthorization(value otto.Value) (schema.Authorization, error) {
	rawAuthorization, _ := value.Export()
	result, ok := rawAuthorization.(schema.Authorization)
	if !ok {
		return nil, fmt.Errorf(wrongArgumentType, rawAuthorization, "Authorization")
	}
	return result, nil
}
Beispiel #4
0
//GetTransaction gets Transaction from otto value
func GetTransaction(value otto.Value) (transaction.Transaction, error) {
	rawTransaction, _ := value.Export()
	result, ok := rawTransaction.(transaction.Transaction)
	if !ok {
		return nil, fmt.Errorf(wrongArgumentType, rawTransaction, "Transaction")
	}
	return result, nil
}
Beispiel #5
0
//GetString gets string from otto value
func GetString(value otto.Value) (string, error) {
	rawString, _ := value.Export()
	result, ok := rawString.(string)
	if !ok {
		return "", fmt.Errorf(wrongArgumentType, rawString, "string")
	}
	return result, nil
}
Beispiel #6
0
//GetList gets []interface{} from otto value
func GetList(value otto.Value) ([]interface{}, error) {
	rawSlice, _ := value.Export()
	result, ok := rawSlice.([]interface{})
	if !ok {
		return []interface{}{}, fmt.Errorf(wrongArgumentType, rawSlice, "array")
	}
	for i, value := range result {
		result[i] = ConvertOttoToGo(value)
	}
	return result, nil
}
Beispiel #7
0
//GetMap gets map[string]interace{} from otto value
func GetMap(value otto.Value) (map[string]interface{}, error) {
	rawMap, _ := value.Export()
	result, ok := rawMap.(map[string]interface{})
	if !ok {
		return map[string]interface{}{}, fmt.Errorf(wrongArgumentType, rawMap, "Object")
	}
	for key, value := range result {
		result[key] = ConvertOttoToGo(value)
	}
	return result, nil
}
Beispiel #8
0
//GetOrCreateTransaction gets transaction from otto value or creates new is otto value is null
func (env *Environment) GetOrCreateTransaction(value otto.Value) (transaction.Transaction, bool, error) {
	if !value.IsNull() {
		tx, err := GetTransaction(value)
		return tx, false, err
	}
	dataStore := env.DataStore
	tx, err := dataStore.Begin()
	if err != nil {
		return nil, false, fmt.Errorf("Error creating transaction: %v", err.Error())
	}
	return tx, true, nil
}
Beispiel #9
0
//GetStringList gets []string  from otto value
func GetStringList(value otto.Value) ([]string, error) {
	rawSlice, _ := value.Export()
	errMessage := fmt.Errorf(wrongArgumentType, rawSlice, "array of strings")
	rawResult, ok := rawSlice.([]interface{})
	if !ok {
		return []string{}, errMessage
	}
	result := []string{}
	for _, rawItem := range rawResult {
		item, ok := rawItem.(string)
		if !ok {
			return []string{}, errMessage
		}
		result = append(result, item)
	}
	return result, nil
}