From a579b1cd03a39790e53175aa811a91b6648ef82c Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Tue, 10 Dec 2024 15:51:13 +0100 Subject: [PATCH] fix: Use `convert.FromTyped` to generate dyn.Value --- bundle/internal/schema/annotations.go | 37 +++++++++++---------------- bundle/internal/schema/parser.go | 7 ++++- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/bundle/internal/schema/annotations.go b/bundle/internal/schema/annotations.go index d6f953e0e..9f67e1bbe 100644 --- a/bundle/internal/schema/annotations.go +++ b/bundle/internal/schema/annotations.go @@ -6,7 +6,6 @@ import ( "reflect" "strings" - "github.com/ghodss/yaml" yaml3 "gopkg.in/yaml.v3" "github.com/databricks/cli/libs/dyn" @@ -109,12 +108,21 @@ func (d *annotationHandler) sync(outputPath string) error { if err != nil { return err } - - missingAnnotations, err := yaml.Marshal(d.empty) + existing, err := yamlloader.LoadYAML("", bytes.NewBuffer(existingFile)) if err != nil { return err } - err = saveYamlWithStyle(outputPath, existingFile, missingAnnotations) + missingAnnotations, err := convert.FromTyped(&d.empty, dyn.NilValue) + if err != nil { + return err + } + + output, err := merge.Merge(existing, missingAnnotations) + if err != nil { + return err + } + + err = saveYamlWithStyle(outputPath, output) if err != nil { return err } @@ -138,30 +146,15 @@ func assingAnnotation(s *jsonschema.Schema, a annotation) { s.Enum = a.Enum } -func saveYamlWithStyle(outputPath string, input []byte, overrides []byte) error { - inputDyn, err := yamlloader.LoadYAML("", bytes.NewBuffer(input)) - if err != nil { - return err - } - if len(overrides) != 0 { - overrideDyn, err := yamlloader.LoadYAML("", bytes.NewBuffer(overrides)) - if err != nil { - return err - } - inputDyn, err = merge.Merge(inputDyn, overrideDyn) - if err != nil { - return err - } - } - +func saveYamlWithStyle(outputPath string, input dyn.Value) error { style := map[string]yaml3.Style{} - file, _ := inputDyn.AsMap() + file, _ := input.AsMap() for _, v := range file.Keys() { style[v.MustString()] = yaml3.LiteralStyle } saver := yamlsaver.NewSaverWithStyle(style) - err = saver.SaveAsYAML(file, outputPath, true) + err := saver.SaveAsYAML(file, outputPath, true) if err != nil { return err } diff --git a/bundle/internal/schema/parser.go b/bundle/internal/schema/parser.go index 962535a64..0775ec68f 100644 --- a/bundle/internal/schema/parser.go +++ b/bundle/internal/schema/parser.go @@ -11,6 +11,7 @@ import ( "github.com/ghodss/yaml" + "github.com/databricks/cli/libs/dyn/yamlloader" "github.com/databricks/cli/libs/jsonschema" ) @@ -147,7 +148,11 @@ func (p *openapiParser) extractAnnotations(typ reflect.Type, outputPath, overrid if err != nil { return err } - err = saveYamlWithStyle(overridesPath, b, []byte{}) + o, err := yamlloader.LoadYAML("", bytes.NewBuffer(b)) + if err != nil { + return err + } + err = saveYamlWithStyle(overridesPath, o) if err != nil { return err }