From d7a5e51950d00a7ad566e1d7d3a983bf4be7d096 Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Mon, 18 Dec 2023 21:34:46 -0500 Subject: [PATCH] fix validation func --- .../add_service/add_service.go | 22 +++++++------------ .../add_service/add_services.go | 12 +++------- .../service_config/service_config.go | 14 ++++++------ 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_service.go b/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_service.go index aaf521a4fc..353353c458 100644 --- a/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_service.go +++ b/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_service.go @@ -52,20 +52,14 @@ func NewAddService( Name: ServiceConfigArgName, IsOptional: false, ZeroValueProvider: builtin_argument.ZeroValueProvider[*service_config.ServiceConfig], - Validator: nil, - //Validator: func(value starlark.Value) *startosis_errors.InterpretationError { - // // we just try to convert the configs here to validate their shape, to avoid code duplication with Interpret - // if _, _, err := validateAndConvertConfigAndReadyCondition( - // serviceNetwork, - // value, - // packageId, // use package id for root locator - // packageId, - // packageContentProvider, - // packageReplaceOptions); err != nil { - // return err - // } - // return nil - //}, + Validator: func(value starlark.Value) *startosis_errors.InterpretationError { + // we just try to convert the configs here to validate their shape, to avoid code duplication with Interpret + _, ok := value.(*service_config.ServiceConfig) + if !ok { + return startosis_errors.NewInterpretationError("The '%s' argument is not a ServiceConfig (was '%s').", ConfigsArgName, reflect.TypeOf(value)) + } + return nil + }, }, }, }, diff --git a/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_services.go b/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_services.go index 4fae9c601d..25bc9ac016 100644 --- a/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_services.go +++ b/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_services.go @@ -45,17 +45,11 @@ func NewAddServices( Name: ConfigsArgName, IsOptional: false, ZeroValueProvider: builtin_argument.ZeroValueProvider[*starlark.Dict], - // TODO: add a validator Validator: func(value starlark.Value) *startosis_errors.InterpretationError { // we just try to convert the configs here to validate their shape, to avoid code duplication with Interpret - if _, _, err := validateAndConvertConfigsAndReadyConditions( - serviceNetwork, - value, - "", - packageId, - packageContentProvider, - packageReplaceOptions); err != nil { - return err + _, ok := value.(*starlark.Dict) + if !ok { + return startosis_errors.NewInterpretationError("The '%s' argument is not a ServiceConfig (was '%s').", ConfigsArgName, reflect.TypeOf(value)) } return nil }, diff --git a/core/server/api_container/server/startosis_engine/kurtosis_types/service_config/service_config.go b/core/server/api_container/server/startosis_engine/kurtosis_types/service_config/service_config.go index be9bcd7902..ab4ed98fed 100644 --- a/core/server/api_container/server/startosis_engine/kurtosis_types/service_config/service_config.go +++ b/core/server/api_container/server/startosis_engine/kurtosis_types/service_config/service_config.go @@ -230,7 +230,7 @@ func (config *ServiceConfig) ToKurtosisType( if !found { return nil, startosis_errors.NewInterpretationError("Required attribute '%s' could not be found on type '%s'", ImageAttr, ServiceConfigTypeName) } - imageName, imageBuildSpec, interpretationErr = convertImageAttr( + imageName, imageBuildSpec, interpretationErr = convertImage( rawImageAttrValue, locatorOfModuleInWhichThisBuiltInIsBeingCalled, packageId, @@ -580,15 +580,15 @@ func convertFilesArguments(attrNameForLogging string, filesDict *starlark.Dict) return filesArtifacts, persistentDirectories, nil } -// If [rawImageAttrValue] is an ImageBuildSpec type, returns name for the image to build and ImageBuildSpec converted to KurtosisType -// If [rawImageAttrValue] is a string, returns the image name with no image build spec (image will be fetched from local cache or remote) -func convertImageAttr( - rawImageAttrValue starlark.Value, +// If [image] is an ImageBuildSpec type, returns name for the image to build and ImageBuildSpec converted to KurtosisType +// If [image] is a string, returns the image name with no image build spec (image will be fetched from local cache or remote) +func convertImage( + image starlark.Value, locatorOfModuleInWhichThisBuiltInIsBeingCalled string, packageId string, packageContentProvider startosis_packages.PackageContentProvider, packageReplaceOptions map[string]string) (string, *image_build_spec.ImageBuildSpec, *startosis_errors.InterpretationError) { - imageBuildSpecStarlarkType, isImageBuildSpecStarlarkType := rawImageAttrValue.(*ImageBuildSpec) + imageBuildSpecStarlarkType, isImageBuildSpecStarlarkType := image.(*ImageBuildSpec) if isImageBuildSpecStarlarkType { imageBuildSpec, interpretationErr := imageBuildSpecStarlarkType.ToKurtosisType(locatorOfModuleInWhichThisBuiltInIsBeingCalled, packageId, packageContentProvider, packageReplaceOptions) if interpretationErr != nil { @@ -601,7 +601,7 @@ func convertImageAttr( logrus.Debugf("CONVERT IMAGE ATTRIBUTE: %v %v", imageName, imageBuildSpec) return imageName, imageBuildSpec, nil } else { - imageName, interpretationErr := kurtosis_types.SafeCastToString(rawImageAttrValue, ImageAttr) + imageName, interpretationErr := kurtosis_types.SafeCastToString(image, ImageAttr) if interpretationErr != nil { return "", nil, interpretationErr }