-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
424 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func patchDict[T map[string]any](dest, patch T) error { | ||
for k, v := range patch { | ||
result, ok := dest[k] | ||
switch { | ||
case !ok: | ||
result = v | ||
case reflect.TypeOf(result) != reflect.TypeOf(v): | ||
return fmt.Errorf("type missmatch for key %s", k) | ||
case reflect.TypeOf(result).Kind() == reflect.Map: | ||
err := patchDict(result.(T), v.(T)) //nolint:forcetypeassert | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
result = v | ||
} | ||
|
||
dest[k] = result | ||
} | ||
return nil | ||
} | ||
|
||
// readOpenAPIPatched Reads OpenAPI file (JSON) and applies the patch (YAML) | ||
func readOpenAPIPatched(oaFile, patchFile string) ([]byte, error) { | ||
dest, err := os.ReadFile(filepath.Clean(oaFile)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
patch, err := os.ReadFile(filepath.Clean(patchFile)) | ||
if errors.Is(err, os.ErrExist) { | ||
// No patch found, exits | ||
return dest, nil | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var d, p map[string]any | ||
err = json.Unmarshal(dest, &d) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = yaml.Unmarshal(patch, &p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = patchDict(d, p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return json.Marshal(&d) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.