From 825f7cdb7b77bfb3a88d658b839141f965ca4fb6 Mon Sep 17 00:00:00 2001 From: leoporoli Date: Wed, 6 Sep 2023 15:56:54 -0300 Subject: [PATCH] fix: the runtime value store now supports `starlark.Bool` value types (#1249) ## Description: the runtime value store now supports `starlark.Bool` value types ## Is this change user facing? NO ## References (if applicable): to fix this issue: https://app.circleci.com/pipelines/github/kurtosis-tech/awesome-kurtosis/1069/workflows/5a87b29f-1ab0-4ed1-94fa-1640ec5b329d/jobs/3297 --- .../recipe_result_repository.go | 30 ++++++++++++------- .../recipe_result_repository_test.go | 16 ++++------ .../runtime_value_store.go | 2 +- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/core/server/api_container/server/startosis_engine/runtime_value_store/recipe_result_repository.go b/core/server/api_container/server/startosis_engine/runtime_value_store/recipe_result_repository.go index 9dba0e6233..150a303fbf 100644 --- a/core/server/api_container/server/startosis_engine/runtime_value_store/recipe_result_repository.go +++ b/core/server/api_container/server/startosis_engine/runtime_value_store/recipe_result_repository.go @@ -60,7 +60,7 @@ func (repository *recipeResultRepository) SaveKey( } // Save store recipe result values into the repository, and it only accepts comparables of -// starlark.String and starlark.Int so far +// starlark.String, starlark.Int and starlark.Bool so far func (repository *recipeResultRepository) Save( uuid string, value map[string]starlark.Comparable, @@ -75,9 +75,9 @@ func (repository *recipeResultRepository) Save( for key, comparableValue := range value { //TODO add more kind of comparable types if we want to extend the support - //TODO now starlark.Int and starlark.String are enough so far + //TODO now starlark.Int, starlark.String and starlark.Bool are enough so far switch valueType := comparableValue.(type) { - case starlark.Int: + case starlark.Int, starlark.Bool: stringifiedValue[key] = comparableValue.String() case starlark.String: comparableStr, ok := comparableValue.(starlark.String) @@ -140,13 +140,7 @@ func (repository *recipeResultRepository) Get( } for key, stringifiedComparable := range stringifiedValue { - var comparableValue starlark.Comparable - comparableInt, err := strconv.Atoi(stringifiedComparable) - if err != nil { - comparableValue = starlark.String(stringifiedComparable) - } else { - comparableValue = starlark.MakeInt(comparableInt) - } + comparableValue := getComparableFromValueString(stringifiedComparable) value[key] = comparableValue } @@ -179,3 +173,19 @@ func (repository *recipeResultRepository) Delete(uuid string) error { func getUuidKey(uuid string) []byte { return []byte(uuid) } + +func getComparableFromValueString(stringifiedComparable string) starlark.Comparable { + var comparableValue starlark.Comparable + comparableInt, err := strconv.Atoi(stringifiedComparable) + if err != nil { + comparableBool, err := strconv.ParseBool(stringifiedComparable) + if err != nil { + comparableValue = starlark.String(stringifiedComparable) + } else { + comparableValue = starlark.Bool(comparableBool) + } + } else { + comparableValue = starlark.MakeInt(comparableInt) + } + return comparableValue +} diff --git a/core/server/api_container/server/startosis_engine/runtime_value_store/recipe_result_repository_test.go b/core/server/api_container/server/startosis_engine/runtime_value_store/recipe_result_repository_test.go index b6942add35..24ef65d773 100644 --- a/core/server/api_container/server/startosis_engine/runtime_value_store/recipe_result_repository_test.go +++ b/core/server/api_container/server/startosis_engine/runtime_value_store/recipe_result_repository_test.go @@ -24,7 +24,8 @@ const ( ) var ( - starlarkIntValue = starlark.MakeInt(30) + starlarkIntValue = starlark.MakeInt(30) + starlarkBoolValue = starlark.Bool(true) ) func TestRecipeResultSaveKey_Success(t *testing.T) { @@ -44,6 +45,7 @@ func TestRecipeResultSaveAndGet_Success(t *testing.T) { resultValue := map[string]starlark.Comparable{ firstKey: starlarkStringValue, secondKey: starlarkIntValue, + thirdKey: starlarkBoolValue, } err := repository.Save(randomUuid, resultValue) @@ -65,22 +67,14 @@ func TestRecipeResultGet_DoesNotExist(t *testing.T) { require.Empty(t, value) } -func TestRecipeResultSave_ErrorWhenUsingNotStarlarkStringOrInt(t *testing.T) { +func TestRecipeResultSave_ErrorWhenUsingNotStarlarkStringIntorBool(t *testing.T) { repository := getRecipeResultRepositoryForTest(t) - resultValue := map[string]starlark.Comparable{ - firstKey: starlark.Bool(true), - } - - err := repository.Save(randomUuid, resultValue) - require.Error(t, err) - require.ErrorContains(t, err, notAcceptedComparableTypeErrorMsg) - resultValue2 := map[string]starlark.Comparable{ secondKey: directory.Directory{}, // nolint: exhaustruct } - err = repository.Save(randomUuid, resultValue2) + err := repository.Save(randomUuid, resultValue2) require.Error(t, err) require.ErrorContains(t, err, notAcceptedComparableTypeErrorMsg) diff --git a/core/server/api_container/server/startosis_engine/runtime_value_store/runtime_value_store.go b/core/server/api_container/server/startosis_engine/runtime_value_store/runtime_value_store.go index 3d2b72b5e4..63de7ca0b3 100644 --- a/core/server/api_container/server/startosis_engine/runtime_value_store/runtime_value_store.go +++ b/core/server/api_container/server/startosis_engine/runtime_value_store/runtime_value_store.go @@ -73,7 +73,7 @@ func (re *RuntimeValueStore) GetOrCreateValueAssociatedWithService(serviceName s } // SetValue store recipe result values into the runtime value store, and it only accepts comparables of -// starlark.String and starlark.Int so far, make sure to upgrade the recipe result repository if you +// starlark.String, starlark.Int, and starlark.Bool so far, make sure to upgrade the recipe result repository if you // want to extend this capability supporting more comparable types func (re *RuntimeValueStore) SetValue(uuid string, value map[string]starlark.Comparable) error { if err := re.recipeResultRepository.Save(uuid, value); err != nil {