Skip to content

Commit

Permalink
revert changes to input file
Browse files Browse the repository at this point in the history
  • Loading branch information
hariso committed Nov 19, 2024
1 parent dabaf2e commit 1755bac
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 41 deletions.
56 changes: 27 additions & 29 deletions specgen/specgen/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions specgen/specgen/testdata/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions specgen/specgen/testdata/simple/existing_specs.yaml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions specgen/specgen/testdata/simple/want.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 1755bac

Please sign in to comment.