fix: Use `convert.FromTyped` to generate dyn.Value

This commit is contained in:
Ilya Kuznetsov 2024-12-10 15:51:13 +01:00
parent 00164a8dbf
commit a579b1cd03
No known key found for this signature in database
GPG Key ID: 91F3DDCF5D21CDDF
2 changed files with 21 additions and 23 deletions

View File

@ -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
}

View File

@ -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
}