-
Notifications
You must be signed in to change notification settings - Fork 28
/
merge.go
105 lines (94 loc) · 2.64 KB
/
merge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package openapi2
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/grokify/mogo/errors/errorsutil"
"github.com/grokify/mogo/os/osutil"
)
var jsonFileRx = regexp.MustCompile(`(?i)\.json\s*$`)
func MergeDirectory(dir string) (Specification, error) {
entries, err := osutil.ReadDirMore(dir, jsonFileRx, false, true, false)
if err != nil {
return Specification{}, err
}
if len(entries) == 0 {
return Specification{}, fmt.Errorf("no JSON files found in directory [%s]", dir)
}
var specMaster Specification
for i, entry := range entries {
thisSpecFilepath := filepath.Join(dir, entry.Name())
thisSpec, err := ReadOpenAPI2SpecFileDirect(thisSpecFilepath)
if err != nil {
return specMaster, err
}
if i == 0 {
specMaster = thisSpec
} else {
specMaster = Merge(specMaster, thisSpec)
}
}
return specMaster, nil
}
func MergeFilepaths(filepaths []string) (Specification, error) {
var specMaster Specification
for i, fpath := range filepaths {
fmt.Printf("[%v][%v]\n", i, fpath)
thisSpec, err := ReadOpenAPI2SpecFileDirect(fpath)
if err != nil {
return specMaster, errorsutil.Wrap(err, fmt.Sprintf("E_READ_SPEC [%v]", fpath))
}
if i == 0 {
specMaster = thisSpec
} else {
specMaster = Merge(specMaster, thisSpec)
}
}
return specMaster, nil
}
func Merge(specMaster, specExtra Specification) Specification {
specMaster = MergeTags(specMaster, specExtra)
specMaster = MergePaths(specMaster, specExtra)
return MergeDefinitions(specMaster, specExtra)
}
func MergeTags(specMaster, specExtra Specification) Specification {
tagsMap := map[string]int{}
for _, tag := range specMaster.Tags {
tagsMap[tag.Name] = 1
}
for _, tag := range specExtra.Tags {
tag.Name = strings.TrimSpace(tag.Name)
if _, ok := tagsMap[tag.Name]; !ok {
specMaster.Tags = append(specMaster.Tags, tag)
}
}
return specMaster
}
func MergePaths(specMaster, specExtra Specification) Specification {
for url, path := range specExtra.Paths {
specMaster.Paths[url] = path
}
return specMaster
}
func MergeDefinitions(specMaster, specExtra Specification) Specification {
for definitionName, def := range specExtra.Definitions {
if specMaster.Definitions == nil {
specMaster.Definitions = map[string]Definition{}
}
specMaster.Definitions[definitionName] = def
}
return specMaster
}
func WriteFileDirMerge(outfile, inputDir string, perm os.FileMode) error {
spec, err := MergeDirectory(inputDir)
if err != nil {
return errorsutil.Wrap(err, "E_OPENAPI3_MERGE_DIRECTORY_FAILED")
}
err = osutil.WriteFileJSON(outfile, spec, perm, "", " ")
if err != nil {
return errorsutil.Wrap(err, "E_OPENAPI3_WRITE_FAILED")
}
return nil
}