From f5384fc5ed482503064b1d8525b9f0b9211993ad Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Fri, 19 May 2023 15:39:55 +0200 Subject: [PATCH] added a map for validators, and moved init under bundle --- cmd/{init => bundle}/init.go | 5 ++-- internal/init_test.go | 6 ++--- libs/template/schema.go | 26 +++---------------- libs/template/validators.go | 48 ++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 28 deletions(-) rename cmd/{init => bundle}/init.go (95%) create mode 100644 libs/template/validators.go diff --git a/cmd/init/init.go b/cmd/bundle/init.go similarity index 95% rename from cmd/init/init.go rename to cmd/bundle/init.go index 07d9ec7b9..498fad8d1 100644 --- a/cmd/init/init.go +++ b/cmd/bundle/init.go @@ -1,11 +1,10 @@ -package init +package bundle import ( "encoding/json" "os" "path/filepath" - "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/template" "github.com/spf13/cobra" ) @@ -66,5 +65,5 @@ var targetDir string func init() { initCmd.Flags().StringVar(&targetDir, "target-dir", ".", "path to directory template will be initialized in") - root.RootCmd.AddCommand(initCmd) + AddCommand(initCmd) } diff --git a/internal/init_test.go b/internal/init_test.go index 793ee3a5e..5e679ea6f 100644 --- a/internal/init_test.go +++ b/internal/init_test.go @@ -6,7 +6,7 @@ import ( "path/filepath" "testing" - _ "github.com/databricks/cli/cmd/init" + _ "github.com/databricks/cli/cmd/bundle" "github.com/databricks/cli/cmd/root" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -36,7 +36,7 @@ func TestTemplateInitializationForDevConfig(t *testing.T) { // materialize the template cmd := root.RootCmd - cmd.SetArgs([]string{"init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp}) + cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp}) err = cmd.Execute() require.NoError(t, err) @@ -68,7 +68,7 @@ func TestTemplateInitializationForProdConfig(t *testing.T) { cmd := root.RootCmd childCommands := cmd.Commands() fmt.Println(childCommands) - cmd.SetArgs([]string{"init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp}) + cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp}) err = cmd.Execute() require.NoError(t, err) diff --git a/libs/template/schema.go b/libs/template/schema.go index b536dc335..9a9f2fe31 100644 --- a/libs/template/schema.go +++ b/libs/template/schema.go @@ -3,8 +3,6 @@ package template import ( "fmt" "reflect" - - "golang.org/x/exp/slices" ) type Schema map[string]FieldInfo @@ -67,27 +65,11 @@ func (schema Schema) CastFloatToInt(config map[string]any) error { } func validateType(v any, fieldType FieldType) error { - switch fieldType { - case FieldTypeString: - if _, ok := v.(string); !ok { - return fmt.Errorf("expected type string, but value is %#v", v) - } - case FieldTypeInt: - if !slices.Contains([]reflect.Kind{reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64}, - reflect.TypeOf(v).Kind()) { - return fmt.Errorf("expected type integer, but value is %#v", v) - } - case FieldTypeFloat: - if !slices.Contains([]reflect.Kind{reflect.Float32, reflect.Float64}, - reflect.TypeOf(v).Kind()) { - return fmt.Errorf("expected type float, but value is %#v", v) - } - case FieldTypeBoolean: - if _, ok := v.(bool); !ok { - return fmt.Errorf("expected type boolean, but value is %#v", v) - } + validateFunc, ok := validators[fieldType] + if !ok { + return nil } - return nil + return validateFunc(v) } // TODO: add validation check for regex for string types diff --git a/libs/template/validators.go b/libs/template/validators.go new file mode 100644 index 000000000..39e7fe9c4 --- /dev/null +++ b/libs/template/validators.go @@ -0,0 +1,48 @@ +package template + +import ( + "fmt" + "reflect" + + "golang.org/x/exp/slices" +) + +type Validator func(v any) error + +// TODO: refactor tests into individual tests for individual validators +func validateString(v any) error { + if _, ok := v.(string); !ok { + return fmt.Errorf("expected type string, but value is %#v", v) + } + return nil +} + +func validateBoolean(v any) error { + if _, ok := v.(bool); !ok { + return fmt.Errorf("expected type boolean, but value is %#v", v) + } + return nil +} + +func validateFloat(v any) error { + if !slices.Contains([]reflect.Kind{reflect.Float32, reflect.Float64}, + reflect.TypeOf(v).Kind()) { + return fmt.Errorf("expected type float, but value is %#v", v) + } + return nil +} + +func validateInteger(v any) error { + if !slices.Contains([]reflect.Kind{reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64}, + reflect.TypeOf(v).Kind()) { + return fmt.Errorf("expected type integer, but value is %#v", v) + } + return nil +} + +var validators map[FieldType]Validator = map[FieldType]Validator{ + FieldTypeString: validateString, + FieldTypeBoolean: validateBoolean, + FieldTypeInt: validateInteger, + FieldTypeFloat: validateFloat, +}