Exemplo n.º 1
0
// GetKeyValuePair ...
func (env EnvironmentItemModel) GetKeyValuePair() (string, string, error) {
	// Collect keys and values
	keys := []string{}
	values := []interface{}{}

	for key, value := range env {
		keys = append(keys, key)
		values = append(values, value)
	}

	if len(keys) == 0 {
		return "", "", errors.New("no environment key specified")
	} else if len(keys) > 2 {
		sort.Strings(keys)
		return "", "", fmt.Errorf("more than 2 keys specified: %v", keys)
	}

	// Collect env key and value
	key := ""
	var value interface{}
	optionsFound := false

	for i := 0; i < len(keys); i++ {
		k := keys[i]
		if k != OptionsKey {
			key = k
			value = values[i]
		} else {
			optionsFound = true
		}
	}

	if key == "" {
		sort.Strings(keys)
		return "", "", fmt.Errorf("no environment key found, keys: %v", keys)
	}
	if len(keys) > 1 && !optionsFound {
		sort.Strings(keys)
		return "", "", fmt.Errorf("more than 1 environment key specified: %v", keys)
	}

	// Cast env value to string
	valueStr := ""

	if value != nil {
		if str, ok := value.(string); ok {
			valueStr = str
		} else if str := parseutil.CastToString(value); str != "" {
			valueStr = str
		} else {
			return "", "", fmt.Errorf("value (%#v) is not a string for key (%s)", value, key)
		}
	}

	return key, valueStr, nil
}
Exemplo n.º 2
0
// GetKeyValuePair ...
func (env EnvironmentItemModel) GetKeyValuePair() (string, string, error) {
	if len(env) > 2 {
		return "", "", fmt.Errorf("Invalid env: more than 2 fields: %#v", env)
	}

	retKey := ""
	retValue := ""

	for key, value := range env {
		if key != OptionsKey {
			if retKey != "" {
				return "", "", fmt.Errorf("Invalid env: more than 1 key-value field found: %#v", env)
			}

			valueStr, ok := value.(string)
			if !ok {
				if value == nil {
					valueStr = ""
				} else {
					valueStr = parseutil.CastToString(value)
					if valueStr == "" {
						return "", "", fmt.Errorf("Invalid value, not a string (key:%#v) (value:%#v)", key, value)
					}
				}
			}

			retKey = key
			retValue = valueStr
		}
	}

	if retKey == "" {
		return "", "", errors.New("Invalid env: no envKey specified!")
	}

	return retKey, retValue, nil
}
Exemplo n.º 3
0
// ParseFromInterfaceMap ...
func (envSerModel *EnvironmentItemOptionsModel) ParseFromInterfaceMap(input map[string]interface{}) error {
	for keyStr, value := range input {

		switch keyStr {
		case "title":
			envSerModel.Title = parseutil.CastToStringPtr(value)
		case "description":
			envSerModel.Description = parseutil.CastToStringPtr(value)
		case "summary":
			envSerModel.Summary = parseutil.CastToStringPtr(value)
		case "category":
			envSerModel.Category = parseutil.CastToStringPtr(value)
		case "value_options":
			castedValue, ok := value.([]string)
			if !ok {
				// try with []interface{} instead and cast the
				//  items to string
				castedValue = []string{}
				interfArr, ok := value.([]interface{})
				if !ok {
					return fmt.Errorf("invalid value type (%#v) for key: %s", value, keyStr)
				}
				for _, interfItm := range interfArr {
					castedItm, ok := interfItm.(string)
					if !ok {
						castedItm = parseutil.CastToString(interfItm)
						if castedItm == "" {
							return fmt.Errorf("not a string value (%#v) in value_options", interfItm)
						}
					}
					castedValue = append(castedValue, castedItm)
				}
			}
			envSerModel.ValueOptions = castedValue
		case "is_required":
			castedBoolPtr, ok := parseutil.CastToBoolPtr(value)
			if !ok {
				return fmt.Errorf("failed to parse bool value (%#v) for key (%s)", value, keyStr)
			}
			envSerModel.IsRequired = castedBoolPtr
		case "is_expand":
			castedBoolPtr, ok := parseutil.CastToBoolPtr(value)
			if !ok {
				return fmt.Errorf("failed to parse bool value (%#v) for key (%s)", value, keyStr)
			}
			envSerModel.IsExpand = castedBoolPtr
		case "is_dont_change_value":
			castedBoolPtr, ok := parseutil.CastToBoolPtr(value)
			if !ok {
				return fmt.Errorf("failed to parse bool value (%#v) for key (%s)", value, keyStr)
			}
			envSerModel.IsDontChangeValue = castedBoolPtr
		case "is_template":
			castedBoolPtr, ok := parseutil.CastToBoolPtr(value)
			if !ok {
				return fmt.Errorf("failed to parse bool value (%#v) for key (%s)", value, keyStr)
			}
			envSerModel.IsTemplate = castedBoolPtr
		case "skip_if_empty":
			castedBoolPtr, ok := parseutil.CastToBoolPtr(value)
			if !ok {
				return fmt.Errorf("failed to parse bool value (%#v) for key (%s)", value, keyStr)
			}
			envSerModel.SkipIfEmpty = castedBoolPtr
		default:
			return fmt.Errorf("not supported key found in options: %s", keyStr)
		}
	}
	return nil
}
Exemplo n.º 4
0
// ParseFromInterfaceMap ...
func (envSerModel *EnvironmentItemOptionsModel) ParseFromInterfaceMap(input map[string]interface{}) error {
	for keyStr, value := range input {
		log.Debugf("  ** processing (key:%#v) (value:%#v) (envSerModel:%#v)", keyStr, value, envSerModel)
		switch keyStr {
		case "title":
			castedValue, ok := value.(string)
			if !ok {
				return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
			}
			envSerModel.Title = pointers.NewStringPtr(castedValue)
		case "description":
			castedValue, ok := value.(string)
			if !ok {
				return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
			}
			envSerModel.Description = pointers.NewStringPtr(castedValue)
		case "summary":
			castedValue, ok := value.(string)
			if !ok {
				return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
			}
			envSerModel.Summary = pointers.NewStringPtr(castedValue)
		case "value_options":
			castedValue, ok := value.([]string)
			if !ok {
				// try with []interface{} instead and cast the
				//  items to string
				castedValue = []string{}
				interfArr, ok := value.([]interface{})
				if !ok {
					return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
				}
				for _, interfItm := range interfArr {
					castedItm, ok := interfItm.(string)
					if !ok {
						castedItm = parseutil.CastToString(interfItm)
						if castedItm == "" {
							return fmt.Errorf("Invalid value in value_options (%#v), not a string: %#v", interfArr, interfItm)
						}
					}
					castedValue = append(castedValue, castedItm)
				}
			}
			envSerModel.ValueOptions = castedValue
		case "is_required":
			castedValue, ok := value.(bool)
			if !ok {
				castedStr := parseutil.CastToString(value)
				if castedStr != "" {
					casted, err := parseutil.ParseBool(castedStr)
					if err != nil {
						return fmt.Errorf("Faild to parse bool (key:%s): %#v, err: %s", keyStr, value, err)
					}
					castedValue = casted
				} else {
					return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
				}
			}
			envSerModel.IsRequired = pointers.NewBoolPtr(castedValue)
		case "is_expand":
			castedValue, ok := value.(bool)
			if !ok {
				castedStr := parseutil.CastToString(value)
				if castedStr != "" {
					casted, err := parseutil.ParseBool(castedStr)
					if err != nil {
						return fmt.Errorf("Faild to parse bool (key:%s): %#v, err: %s", keyStr, value, err)
					}
					castedValue = casted
				} else {
					return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
				}
			}
			envSerModel.IsExpand = pointers.NewBoolPtr(castedValue)
		case "is_dont_change_value":
			castedValue, ok := value.(bool)
			if !ok {
				castedStr := parseutil.CastToString(value)
				if castedStr != "" {
					casted, err := parseutil.ParseBool(castedStr)
					if err != nil {
						return fmt.Errorf("Faild to parse bool (key:%s): %#v, err: %s", keyStr, value, err)
					}
					castedValue = casted
				} else {
					return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
				}
			}
			envSerModel.IsDontChangeValue = pointers.NewBoolPtr(castedValue)
		case "is_template":
			castedValue, ok := value.(bool)
			if !ok {
				castedStr := parseutil.CastToString(value)
				if castedStr != "" {
					casted, err := parseutil.ParseBool(castedStr)
					if err != nil {
						return fmt.Errorf("Faild to parse bool (key:%s): %#v, err: %s", keyStr, value, err)
					}
					castedValue = casted
				} else {
					return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
				}
			}
			envSerModel.IsTemplate = pointers.NewBoolPtr(castedValue)
		default:
			return fmt.Errorf("Not supported key found in options: %#v", keyStr)
		}
	}
	return nil
}