forked from utrack/clay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swagcombo.go
43 lines (37 loc) · 940 Bytes
/
swagcombo.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
package transport
import (
"encoding/json"
"github.com/peterbourgon/mergemap"
"github.com/pkg/errors"
)
// swagJoiner glues up several Swagger definitions to one.
// This is one dirty hack...
type swagJoiner struct {
result map[string]interface{}
}
// AddDefinition adds another definition to the soup.
func (c *swagJoiner) AddDefinition(buf []byte) error {
def := map[string]interface{}{}
err := json.Unmarshal(buf, &def)
if err != nil {
return errors.Wrap(err, "couldn't unmarshal JSON def")
}
if c.result == nil {
c.result = def
return nil
}
c.result = mergemap.Merge(c.result, def)
return nil
}
// SumDefinitions returns a (hopefully) valid Swagger definition combined
// from everything that came up .AddDefinition().
func (c *swagJoiner) SumDefinitions() []byte {
if c.result == nil {
c.result = map[string]interface{}{}
}
ret, err := json.Marshal(c.result)
if err != nil {
panic(err)
}
return ret
}