From 1755bacf7e59eec5fda8a1fd0b32e7c4c8c1c21c Mon Sep 17 00:00:00 2001 From: Haris Osmanagic Date: Tue, 19 Nov 2024 12:37:09 +0100 Subject: [PATCH] revert changes to input file --- specgen/specgen/specgen.go | 56 +++++++++---------- specgen/specgen/testdata/integration_test.go | 13 ++++- .../testdata/simple/existing_specs.yaml | 10 ++-- specgen/specgen/testdata/simple/want.yaml | 10 ++-- 4 files changed, 48 insertions(+), 41 deletions(-) diff --git a/specgen/specgen/specgen.go b/specgen/specgen/specgen.go index a322ad1..42b3118 100644 --- a/specgen/specgen/specgen.go +++ b/specgen/specgen/specgen.go @@ -82,45 +82,51 @@ func WriteAndCombine(yamlBytes []byte, path string) error { return fmt.Errorf("failed to read existing file: %w", err) } - out := struct { + type specWithUnknowns struct { Version string `yaml:"version"` Specification struct { v1.ConnectorSpecification `yaml:",inline"` UnknownFields map[string]any `yaml:",inline"` } `yaml:"specification"` UnknownFields map[string]any `yaml:",inline"` - }{} + } - err = yaml.Unmarshal(yamlBytes, &out) + generatedYAML := specWithUnknowns{} + err = yaml.Unmarshal(yamlBytes, &generatedYAML) if err != nil { // This shouldn't happen, since we produced the YAML in this program. - return fmt.Errorf("failed to unmarshal specifications YAML: %w", err) + return fmt.Errorf("failed to unmarshal generated specifications YAML: %w", err) } - var unknownFields map[string]any + existingSpecs := specWithUnknowns{} // Unmarshal the existing YAML file and check for unknown fields. - if err := yaml.Unmarshal(existingRaw, &unknownFields); err != nil { + err = yaml.Unmarshal(existingRaw, &existingSpecs) + if err != nil { // This shouldn't happen, since we produced the YAML in this program. - return fmt.Errorf("failed to unmarshal specifications YAML: %w", err) + return fmt.Errorf("failed to unmarshal existing specifications YAML: %w", err) } - // Merge the new map into the existing map, preserving existing fields - connectorUnknownFields, _ := unknownFields["specification"].(map[string]any) - connTyp := reflect.TypeFor[v1.ConnectorSpecification]() - for i := range connTyp.NumField() { - f := connTyp.Field(i) - fieldName := getYAMLFieldName(f) - delete(connectorUnknownFields, fieldName) + existingSpecs.Version = generatedYAML.Version + existingSpecs.Specification.Source = generatedYAML.Specification.Source + existingSpecs.Specification.Destination = generatedYAML.Specification.Destination + if existingSpecs.Specification.Name == "" { + existingSpecs.Specification.Name = generatedYAML.Specification.Name + } + if existingSpecs.Specification.Summary == "" { + existingSpecs.Specification.Summary = generatedYAML.Specification.Summary + } + if existingSpecs.Specification.Description == "" { + existingSpecs.Specification.Description = generatedYAML.Specification.Description + } + if existingSpecs.Specification.Version == "" { + existingSpecs.Specification.Version = generatedYAML.Specification.Version + } + if existingSpecs.Specification.Author == "" { + existingSpecs.Specification.Author = generatedYAML.Specification.Author } - - delete(unknownFields, "version") - delete(unknownFields, "specification") - - out.UnknownFields = unknownFields - out.Specification.UnknownFields = connectorUnknownFields // Marshal the merged map back to YAML bytes - mergedYAML, err := yamlMarshal(out) + mergedYAML, err := yamlMarshal(existingSpecs) if err != nil { return fmt.Errorf("failed to marshal merged YAML: %w", err) } @@ -133,14 +139,6 @@ func WriteAndCombine(yamlBytes []byte, path string) error { return nil } -func getYAMLFieldName(field reflect.StructField) string { - tag := field.Tag.Get("yaml") - if tag == "" { - tag = field.Tag.Get("json") - } - return strings.Split(tag, ",")[0] -} - func yamlMarshal(obj any) ([]byte, error) { var buf bytes.Buffer enc := yaml.NewEncoder(&buf) diff --git a/specgen/specgen/testdata/integration_test.go b/specgen/specgen/testdata/integration_test.go index a8db966..1a7696f 100644 --- a/specgen/specgen/testdata/integration_test.go +++ b/specgen/specgen/testdata/integration_test.go @@ -96,16 +96,25 @@ func TestParseSpecification(t *testing.T) { func TestWriteAndCombine(t *testing.T) { is := is.New(t) + existingSpecsPath := "./simple/existing_specs.yaml" + oldExisting, err := os.ReadFile(existingSpecsPath) + is.NoErr(err) + t.Cleanup(func() { + err := os.WriteFile(existingSpecsPath, oldExisting, 0o644) + if err != nil { + t.Logf("failed reverting changes to existing specs file: %v", err) + } + }) specs, err := specgen.ExtractSpecification(context.Background(), simple.Connector) is.NoErr(err) specBytes, err := specgen.SpecificationToYaml(specs) is.NoErr(err) - err = specgen.WriteAndCombine(specBytes, "./simple/existing_specs.yaml") + err = specgen.WriteAndCombine(specBytes, existingSpecsPath) is.NoErr(err) - got, err := os.ReadFile("./simple/existing_specs.yaml") + got, err := os.ReadFile(existingSpecsPath) is.NoErr(err) want, err := os.ReadFile("./simple/want.yaml") diff --git a/specgen/specgen/testdata/simple/existing_specs.yaml b/specgen/specgen/testdata/simple/existing_specs.yaml index 692080d..543fcef 100644 --- a/specgen/specgen/testdata/simple/existing_specs.yaml +++ b/specgen/specgen/testdata/simple/existing_specs.yaml @@ -1,7 +1,7 @@ version: "1.0" specification: - name: "test connector name" - summary: "test connector summary" - description: "test connector description" - version: "v12.34.56" - author: "Acme" + name: test connector name + summary: test connector summary + description: test connector description + version: v12.34.56 + author: Acme diff --git a/specgen/specgen/testdata/simple/want.yaml b/specgen/specgen/testdata/simple/want.yaml index c4d9c43..c4ee5a8 100644 --- a/specgen/specgen/testdata/simple/want.yaml +++ b/specgen/specgen/testdata/simple/want.yaml @@ -1,10 +1,10 @@ version: "1.0" specification: - name: "test connector name" - summary: "test connector summary" - description: "test connector description" - version: "v12.34.56" - author: "Acme" + name: test connector name + summary: test connector summary + description: test connector description + version: v12.34.56 + author: Acme source: parameters: - name: myInt