-
Notifications
You must be signed in to change notification settings - Fork 28
/
intersection.go
80 lines (69 loc) · 1.88 KB
/
intersection.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
package openapi3edit
import (
"sort"
"github.com/grokify/mogo/type/stringsutil"
"github.com/grokify/spectrum/openapi3"
)
type SpecMetadata struct {
Endpoints []string
OperationIDs []string
SchemaNames []string
}
func NewSpecMetadata(spec *openapi3.Spec) SpecMetadata {
md := SpecMetadata{
OperationIDs: []string{},
Endpoints: []string{}}
if spec != nil {
mapOpIDs := SpecOperationIds(spec)
for key := range mapOpIDs {
md.OperationIDs = append(md.OperationIDs, key)
}
md.Endpoints = SpecEndpoints(spec, true)
sm := openapi3.SpecMore{Spec: spec}
md.SchemaNames = sm.SchemaNames()
}
return md
}
func (md *SpecMetadata) Intersection(md2 SpecMetadata) SpecMetadata {
idata := SpecMetadata{
Endpoints: stringsutil.SliceIntersection(md.Endpoints, md2.Endpoints),
OperationIDs: stringsutil.SliceIntersection(md.OperationIDs, md2.OperationIDs),
SchemaNames: stringsutil.SliceIntersection(md.SchemaNames, md2.SchemaNames)}
return idata
}
func (md *SpecMetadata) IsEmpty() bool {
if len(md.Endpoints) == 0 &&
len(md.OperationIDs) == 0 &&
len(md.SchemaNames) == 0 {
return true
}
return false
}
func (md *SpecMetadata) Sort() {
sort.Strings(md.Endpoints)
sort.Strings(md.OperationIDs)
sort.Strings(md.SchemaNames)
}
type IntersectionData struct {
Spec1 SpecMetadata
Spec2 SpecMetadata
Intersection SpecMetadata
}
func (idata *IntersectionData) Sort() {
idata.Spec1.Sort()
idata.Spec2.Sort()
idata.Intersection.Sort()
}
func NewIntersectionData() IntersectionData {
return IntersectionData{
Spec1: NewSpecMetadata(nil),
Spec2: NewSpecMetadata(nil),
Intersection: NewSpecMetadata(nil)}
}
func SpecsIntersection(spec1, spec2 *openapi3.Spec) IntersectionData {
idata := IntersectionData{
Spec1: NewSpecMetadata(spec1),
Spec2: NewSpecMetadata(spec2)}
idata.Intersection = idata.Spec1.Intersection(idata.Spec2)
return idata
}