-
Notifications
You must be signed in to change notification settings - Fork 2
/
docs.go
75 lines (60 loc) · 1.45 KB
/
docs.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
package gocqrs
import (
"encoding/json"
"log"
"net/url"
)
type APPDocs struct {
Name string `json:"name"`
Version string `json:"version"`
Entities map[string][]string `json:"entities"`
Endpoints []Endpoint `json:"endpoints"`
}
type Endpoint struct {
Path string `json:"path"`
Method string `json:"method"`
Description string `json:"description,omitempty"`
ExampleBody string `json:"exampleBody,omitempty"`
}
func NewEndpoint(path, method string, exampleBody interface{}) Endpoint {
var e Endpoint
_, err := url.Parse(path)
if err != nil {
log.Fatal("Invalid path for doc endpoint:", path)
}
e.Path = path
e.Method = method
if exampleBody != nil {
b, _ := json.Marshal(exampleBody)
e.ExampleBody = string(b)
}
return e
}
func (ad APPDocs) GetEvents(e string) []string {
for entity, events := range ad.Entities {
if entity == e {
return events
}
}
return []string{}
}
func GenerateDocs(app *App) APPDocs {
return app.GenDocs()
}
func (app *App) AddEndpoint(e Endpoint) {
app.Endpoints = append(app.Endpoints, e)
}
func (app *App) GenDocs() APPDocs {
var docs APPDocs
docs.Entities = make(map[string][]string)
docs.Name = app.Name
docs.Version = app.Version
for e, c := range app.Entities {
docs.Entities[e] = []string{}
for event, _ := range c.EventHandlers {
docs.Entities[e] = append(docs.Entities[e], event)
}
}
docs.Endpoints = app.Endpoints
return docs
}