From a6c45d5bf72c131e793e6c28944bbe5839bc20f3 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Tue, 10 Dec 2024 14:00:00 +0100 Subject: [PATCH] feat: Yaml styles for open api overrides --- bundle/internal/schema/annotations.go | 55 +++-- .../schema/annotations_openapi_overrides.yml | 215 +++++++++++------- bundle/internal/schema/parser.go | 3 +- 3 files changed, 162 insertions(+), 111 deletions(-) diff --git a/bundle/internal/schema/annotations.go b/bundle/internal/schema/annotations.go index 589f6f1d85..d6f953e0ec 100644 --- a/bundle/internal/schema/annotations.go +++ b/bundle/internal/schema/annotations.go @@ -110,32 +110,11 @@ func (d *annotationHandler) sync(outputPath string) error { return err } - existing, err := yamlloader.LoadYAML(outputPath, bytes.NewBuffer(existingFile)) + missingAnnotations, err := yaml.Marshal(d.empty) if err != nil { return err } - emptyB, err := yaml.Marshal(d.empty) - if err != nil { - return err - } - - empty, err := yamlloader.LoadYAML("", bytes.NewBuffer(emptyB)) - if err != nil { - return err - } - mergedFile, err := merge.Merge(existing, empty) - if err != nil { - return err - } - - style := map[string]yaml3.Style{} - file, _ := mergedFile.AsMap() - for _, v := range file.Keys() { - style[v.MustString()] = yaml3.LiteralStyle - } - - saver := yamlsaver.NewSaverWithStyle(style) - err = saver.SaveAsYAML(file, outputPath, true) + err = saveYamlWithStyle(outputPath, existingFile, missingAnnotations) if err != nil { return err } @@ -158,3 +137,33 @@ func assingAnnotation(s *jsonschema.Schema, a annotation) { s.Title = a.Title 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 + } + } + + style := map[string]yaml3.Style{} + file, _ := inputDyn.AsMap() + for _, v := range file.Keys() { + style[v.MustString()] = yaml3.LiteralStyle + } + + saver := yamlsaver.NewSaverWithStyle(style) + err = saver.SaveAsYAML(file, outputPath, true) + if err != nil { + return err + } + return nil +} diff --git a/bundle/internal/schema/annotations_openapi_overrides.yml b/bundle/internal/schema/annotations_openapi_overrides.yml index 73fa3b1eb9..e9e659e687 100644 --- a/bundle/internal/schema/annotations_openapi_overrides.yml +++ b/bundle/internal/schema/annotations_openapi_overrides.yml @@ -1,112 +1,155 @@ github.com/databricks/cli/bundle/config/resources.Cluster: - data_security_mode: - description: PLACEHOLDER - docker_image: - description: PLACEHOLDER - permissions: - description: PLACEHOLDER - runtime_engine: - description: PLACEHOLDER - workload_type: - description: PLACEHOLDER + "data_security_mode": + "description": |- + PLACEHOLDER + "docker_image": + "description": |- + PLACEHOLDER + "permissions": + "description": |- + PLACEHOLDER + "runtime_engine": + "description": |- + PLACEHOLDER + "workload_type": + "description": |- + PLACEHOLDER github.com/databricks/cli/bundle/config/resources.Dashboard: - embed_credentials: - description: PLACEHOLDER - file_path: - description: PLACEHOLDER - permissions: - description: PLACEHOLDER + "embed_credentials": + "description": |- + PLACEHOLDER + "file_path": + "description": |- + PLACEHOLDER + "permissions": + "description": |- + PLACEHOLDER github.com/databricks/cli/bundle/config/resources.Job: - health: - description: PLACEHOLDER - permissions: - description: PLACEHOLDER - run_as: - description: PLACEHOLDER + "health": + "description": |- + PLACEHOLDER + "permissions": + "description": |- + PLACEHOLDER + "run_as": + "description": |- + PLACEHOLDER github.com/databricks/cli/bundle/config/resources.MlflowExperiment: - permissions: - description: PLACEHOLDER + "permissions": + "description": |- + PLACEHOLDER github.com/databricks/cli/bundle/config/resources.MlflowModel: - permissions: - description: PLACEHOLDER + "permissions": + "description": |- + PLACEHOLDER github.com/databricks/cli/bundle/config/resources.ModelServingEndpoint: - permissions: - description: PLACEHOLDER + "permissions": + "description": |- + PLACEHOLDER github.com/databricks/cli/bundle/config/resources.Pipeline: - permissions: - description: PLACEHOLDER + "permissions": + "description": |- + PLACEHOLDER github.com/databricks/cli/bundle/config/resources.QualityMonitor: - table_name: - description: PLACEHOLDER + "table_name": + "description": |- + PLACEHOLDER github.com/databricks/cli/bundle/config/resources.RegisteredModel: - grants: - description: PLACEHOLDER + "grants": + "description": |- + PLACEHOLDER github.com/databricks/cli/bundle/config/resources.Schema: - grants: - description: PLACEHOLDER - properties: - description: PLACEHOLDER + "grants": + "description": |- + PLACEHOLDER + "properties": + "description": |- + PLACEHOLDER github.com/databricks/cli/bundle/config/resources.Volume: - grants: - description: PLACEHOLDER - volume_type: - description: PLACEHOLDER + "grants": + "description": |- + PLACEHOLDER + "volume_type": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/compute.AwsAttributes: - availability: - description: PLACEHOLDER - ebs_volume_type: - description: PLACEHOLDER + "availability": + "description": |- + PLACEHOLDER + "ebs_volume_type": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/compute.AzureAttributes: - availability: - description: PLACEHOLDER + "availability": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/compute.ClusterSpec: - data_security_mode: - description: PLACEHOLDER - docker_image: - description: PLACEHOLDER - runtime_engine: - description: PLACEHOLDER - workload_type: - description: PLACEHOLDER + "data_security_mode": + "description": |- + PLACEHOLDER + "docker_image": + "description": |- + PLACEHOLDER + "runtime_engine": + "description": |- + PLACEHOLDER + "workload_type": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/compute.DockerImage: - basic_auth: - description: PLACEHOLDER + "basic_auth": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/compute.GcpAttributes: - availability: - description: PLACEHOLDER + "availability": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/jobs.GitSource: - git_snapshot: - description: PLACEHOLDER + "git_snapshot": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/jobs.JobEnvironment: - spec: - description: PLACEHOLDER + "spec": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/jobs.JobsHealthRule: - metric: - description: PLACEHOLDER - op: - description: PLACEHOLDER + "metric": + "description": |- + PLACEHOLDER + "op": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/jobs.JobsHealthRules: - rules: - description: PLACEHOLDER + "rules": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/jobs.RunJobTask: - python_named_params: - description: PLACEHOLDER + "python_named_params": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/jobs.Task: - health: - description: PLACEHOLDER + "health": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/jobs.TriggerSettings: - table_update: - description: PLACEHOLDER + "table_update": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/jobs.Webhook: - id: - description: PLACEHOLDER + "id": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/pipelines.CronTrigger: - quartz_cron_schedule: - description: PLACEHOLDER - timezone_id: - description: PLACEHOLDER + "quartz_cron_schedule": + "description": |- + PLACEHOLDER + "timezone_id": + "description": |- + PLACEHOLDER github.com/databricks/databricks-sdk-go/service/pipelines.PipelineTrigger: - cron: - description: PLACEHOLDER - manual: - description: PLACEHOLDER + "cron": + "description": |- + PLACEHOLDER + "manual": + "description": |- + PLACEHOLDER diff --git a/bundle/internal/schema/parser.go b/bundle/internal/schema/parser.go index 262be73a24..962535a64e 100644 --- a/bundle/internal/schema/parser.go +++ b/bundle/internal/schema/parser.go @@ -147,11 +147,10 @@ func (p *openapiParser) extractAnnotations(typ reflect.Type, outputPath, overrid if err != nil { return err } - err = os.WriteFile(overridesPath, b, 0644) + err = saveYamlWithStyle(overridesPath, b, []byte{}) if err != nil { return err } - b, err = yaml.Marshal(annotations) if err != nil { return err