Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a public method to read the value of an EnvironmentItemModel with original type #205

Merged
merged 2 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions models/models_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
// DefaultSkipIfEmpty ...
DefaultSkipIfEmpty = false

//DefaultIsRequired ...
// DefaultIsRequired ...
DefaultIsRequired = false
// DefaultIsDontChangeValue ...
DefaultIsDontChangeValue = false
Expand All @@ -42,11 +42,11 @@ func NewEnvJSONList(jsonStr string) (EnvsJSONListModel, error) {
return list, nil
}

// GetKeyValuePair ...
func (env EnvironmentItemModel) GetKeyValuePair() (string, string, error) {
// GetKeyValuePairWithType ...
func (env EnvironmentItemModel) GetKeyValuePairWithType() (string, interface{}, error) {
// Collect keys and values
keys := []string{}
values := []interface{}{}
var keys []string
var values []interface{}

for key, value := range env {
keys = append(keys, key)
Expand Down Expand Up @@ -84,6 +84,16 @@ func (env EnvironmentItemModel) GetKeyValuePair() (string, string, error) {
return "", "", fmt.Errorf("more than 1 environment key specified: %v", keys)
}

return key, value, nil
}

// GetKeyValuePair ...
func (env EnvironmentItemModel) GetKeyValuePair() (string, string, error) {
key, value, err := env.GetKeyValuePairWithType()
if err != nil {
return "", "", err
}

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

Expand Down
68 changes: 67 additions & 1 deletion models/models_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package models
import (
"testing"

yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"

"encoding/json"

Expand Down Expand Up @@ -77,6 +77,72 @@ func TestGetKeyValuePair(t *testing.T) {
require.EqualError(t, err, "no environment key found, keys: [opts]")
}

func TestGetKeyValuePairWithType(t *testing.T) {
// Filled env
env := EnvironmentItemModel{
"bool_key": false,
OptionsKey: EnvironmentItemOptionsModel{
Title: pointers.NewStringPtr("test_title"),
Description: pointers.NewStringPtr("test_description"),
Summary: pointers.NewStringPtr("test_summary"),
Category: pointers.NewStringPtr("category"),
ValueOptions: []string{"test_key2", "test_value2"},
IsRequired: pointers.NewBoolPtr(true),
IsExpand: pointers.NewBoolPtr(false),
IsSensitive: pointers.NewBoolPtr(false),
IsDontChangeValue: pointers.NewBoolPtr(true),
IsTemplate: pointers.NewBoolPtr(false),
SkipIfEmpty: pointers.NewBoolPtr(false),
},
}

key, value, err := env.GetKeyValuePairWithType()
require.NoError(t, err)

require.Equal(t, "bool_key", key)
require.Equal(t, false, value)

// More then 2 fields
env = EnvironmentItemModel{
"test_key": "test_value",
"test_key1": "test_value1",
OptionsKey: EnvironmentItemOptionsModel{Title: pointers.NewStringPtr("test_title")},
}

key, value, err = env.GetKeyValuePairWithType()
require.EqualError(t, err, `more than 2 keys specified: [opts test_key test_key1]`)

// 2 key-value fields
env = EnvironmentItemModel{
"test_key": "test_value",
"test_key1": "test_value1",
}

key, value, err = env.GetKeyValuePairWithType()
require.EqualError(t, err, `more than 1 environment key specified: [test_key test_key1]`)

// String value
env = EnvironmentItemModel{"test_key": "test_value"}

key, value, err = env.GetKeyValuePairWithType()
require.NoError(t, err)

require.Equal(t, "test_key", key)
require.Equal(t, "test_value", value)

// Empty key
env = EnvironmentItemModel{"": "test_value"}

key, value, err = env.GetKeyValuePairWithType()
require.EqualError(t, err, "no environment key found, keys: []")

// Missing key-value
env = EnvironmentItemModel{OptionsKey: EnvironmentItemOptionsModel{Title: pointers.NewStringPtr("test_title")}}

key, value, err = env.GetKeyValuePairWithType()
require.EqualError(t, err, "no environment key found, keys: [opts]")
}

func TestParseFromInterfaceMap(t *testing.T) {
envOptions := EnvironmentItemOptionsModel{}
model := map[string]interface{}{}
Expand Down