Skip to content

Commit

Permalink
fix: the runtime value store now supports starlark.Bool value types (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
leoporoli committed Sep 6, 2023
1 parent 725cf43 commit 825f7cd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Up @@ -24,7 +24,8 @@ const (
)

var (
starlarkIntValue = starlark.MakeInt(30)
starlarkIntValue = starlark.MakeInt(30)
starlarkBoolValue = starlark.Bool(true)
)

func TestRecipeResultSaveKey_Success(t *testing.T) {
Expand All @@ -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)
Expand All @@ -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)

Expand Down
Expand Up @@ -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 {
Expand Down

0 comments on commit 825f7cd

Please sign in to comment.