From fcdccb359a566190ed205ad4dfc39c62be0f6c82 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 27 Aug 2024 12:47:49 +0200 Subject: [PATCH] cleanu unused code --- bundle/internal/schema/main.go | 3 +- cmd/bundle/schema.go | 50 +++++++++++++----------------- libs/jsonschema/schema.go | 45 +-------------------------- libs/jsonschema/schema_test.go | 56 ---------------------------------- libs/template/config_test.go | 2 +- 5 files changed, 26 insertions(+), 130 deletions(-) diff --git a/bundle/internal/schema/main.go b/bundle/internal/schema/main.go index 33cfbbe7f..e59db91e0 100644 --- a/bundle/internal/schema/main.go +++ b/bundle/internal/schema/main.go @@ -52,7 +52,8 @@ func addInterpolationPatterns(_ reflect.Type, s jsonschema.Schema) jsonschema.Sc // as well. Make sure to pull those in. // TODO: Add unit tests for all permutations of structs, maps and slices for the FromType // method. -// TODO: Note the minor regression of losing the bundle descriptions +// TODO: Note the minor regression of losing the bundle descriptions. +// TODO: Ensure descriptions work for target resources section. func main() { if len(os.Args) != 2 { diff --git a/cmd/bundle/schema.go b/cmd/bundle/schema.go index 813aebbae..9938ee14f 100644 --- a/cmd/bundle/schema.go +++ b/cmd/bundle/schema.go @@ -1,13 +1,7 @@ package bundle import ( - "encoding/json" - "reflect" - - "github.com/databricks/cli/bundle/config" - "github.com/databricks/cli/bundle/schema" "github.com/databricks/cli/cmd/root" - "github.com/databricks/cli/libs/jsonschema" "github.com/spf13/cobra" ) @@ -19,31 +13,31 @@ func newSchemaCommand() *cobra.Command { } cmd.RunE = func(cmd *cobra.Command, args []string) error { - // Load embedded schema descriptions. - docs, err := schema.LoadBundleDescriptions() - if err != nil { - return err - } + // // Load embedded schema descriptions. + // docs, err := schema.LoadBundleDescriptions() + // if err != nil { + // return err + // } - // Generate the JSON schema from the bundle configuration struct in Go. - schema, err := schema.New(reflect.TypeOf(config.Root{}), docs) - if err != nil { - return err - } + // // Generate the JSON schema from the bundle configuration struct in Go. + // schema, err := schema.New(reflect.TypeOf(config.Root{}), docs) + // if err != nil { + // return err + // } - // Target variable value overrides can be primitives, maps or sequences. - // Set an empty schema for them. - err = schema.SetByPath("targets.*.variables.*", jsonschema.Schema{}) - if err != nil { - return err - } + // // Target variable value overrides can be primitives, maps or sequences. + // // Set an empty schema for them. + // err = schema.SetByPath("targets.*.variables.*", jsonschema.Schema{}) + // if err != nil { + // return err + // } - // Print the JSON schema to stdout. - result, err := json.MarshalIndent(schema, "", " ") - if err != nil { - return err - } - cmd.OutOrStdout().Write(result) + // // Print the JSON schema to stdout. + // result, err := json.MarshalIndent(schema, "", " ") + // if err != nil { + // return err + // } + // cmd.OutOrStdout().Write(result) return nil } diff --git a/libs/jsonschema/schema.go b/libs/jsonschema/schema.go index 44cc3b9c8..b364c8276 100644 --- a/libs/jsonschema/schema.go +++ b/libs/jsonschema/schema.go @@ -6,20 +6,12 @@ import ( "os" "regexp" "slices" - "strings" "github.com/databricks/cli/internal/build" "golang.org/x/mod/semver" ) // defines schema for a json object -// TODO: Remove pointers from properties and AnyOf. -// TODO: Can / should we emulate dyn.V here in having a readonly model for the data -// structure? Makes it easier to reason about. -// -// Any performance issues can be addressed by storing the schema -// -// as an embedded file. type Schema struct { // TODO: Comments for this field Definitions any `json:"$defs,omitempty"` @@ -38,6 +30,7 @@ type Schema struct { // Schemas for the fields of an struct. The keys are the first json tag. // The values are the schema for the type of the field + // TODO: Followup: Make this a map[string]Schema Properties map[string]*Schema `json:"properties,omitempty"` // The schema for all values of an array @@ -95,48 +88,12 @@ func (s *Schema) ParseString(v string) (any, error) { return fromString(v, s.Type) } -func (s *Schema) getByPath(path string) (*Schema, error) { - p := strings.Split(path, ".") - - res := s - for _, node := range p { - if node == "*" { - res = res.AdditionalProperties.(*Schema) - continue - } - var ok bool - res, ok = res.Properties[node] - if !ok { - return nil, fmt.Errorf("property %q not found in schema. Query path: %s", node, path) - } - } - return res, nil -} - -func (s *Schema) GetByPath(path string) (Schema, error) { - v, err := s.getByPath(path) - if err != nil { - return Schema{}, err - } - return *v, nil -} - -func (s *Schema) SetByPath(path string, v Schema) error { - dst, err := s.getByPath(path) - if err != nil { - return err - } - *dst = v - return nil -} - type Type string const ( // Default zero value of a schema. This does not correspond to a type in the // JSON schema spec and is an internal type defined for convenience. InvalidType Type = "invalid" - NullType Type = "null" BooleanType Type = "boolean" StringType Type = "string" NumberType Type = "number" diff --git a/libs/jsonschema/schema_test.go b/libs/jsonschema/schema_test.go index c365cf235..46f4db6b1 100644 --- a/libs/jsonschema/schema_test.go +++ b/libs/jsonschema/schema_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestSchemaValidateTypeNames(t *testing.T) { @@ -340,58 +339,3 @@ func testSchema() *Schema { } } - -func TestSchemaGetByPath(t *testing.T) { - s := testSchema() - - ss, err := s.GetByPath("int_val") - require.NoError(t, err) - assert.Equal(t, Schema{ - Type: IntegerType, - Default: int64(123), - }, ss) - - ss, err = s.GetByPath("string_val") - require.NoError(t, err) - assert.Equal(t, Schema{ - Type: StringType, - }, ss) - - ss, err = s.GetByPath("object_val.bar") - require.NoError(t, err) - assert.Equal(t, Schema{ - Type: StringType, - Default: "baz", - }, ss) - - ss, err = s.GetByPath("object_val.*.foo") - require.NoError(t, err) - assert.Equal(t, Schema{ - Type: StringType, - Default: "zab", - }, ss) -} - -func TestSchemaSetByPath(t *testing.T) { - s := testSchema() - - err := s.SetByPath("int_val", Schema{ - Type: IntegerType, - Default: int64(456), - }) - require.NoError(t, err) - assert.Equal(t, int64(456), s.Properties["int_val"].Default) - - err = s.SetByPath("object_val.*.foo", Schema{ - Type: StringType, - Default: "zooby", - }) - require.NoError(t, err) - - ns, err := s.GetByPath("object_val.*.foo") - require.NoError(t, err) - assert.Equal(t, Schema{ - Type: StringType, - Default: "zooby", - }, ns) -} diff --git a/libs/template/config_test.go b/libs/template/config_test.go index 73b47f289..ab9dbeb5f 100644 --- a/libs/template/config_test.go +++ b/libs/template/config_test.go @@ -461,7 +461,7 @@ func TestPromptIsSkippedAnyOf(t *testing.T) { Default: "hello-world", Extension: jsonschema.Extension{ SkipPromptIf: &jsonschema.Schema{ - AnyOf: []*jsonschema.Schema{ + AnyOf: []jsonschema.Schema{ { Properties: map[string]*jsonschema.Schema{ "abc": {