/
json.go
66 lines (58 loc) · 1.57 KB
/
json.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
package ui
import (
"github.com/anz-bank/sysl/pkg/sysl"
"github.com/anz-bank/sysl/pkg/syslutil"
)
const GRPC = "grpc"
const REST = "rest"
// WebService is the struct which will be translated to JSON via reflection.
type WebService struct {
Name string
Attributes map[string]string
Endpoints []Endpoint
Type string
Path string
}
// BuildWebService takes a sysl Application and returns a json-exportable representation of Sysl
func BuildWebService(a *sysl.Application) (*WebService, error) {
// TODO: Ensure there are no duplicate service names.
newService := &WebService{
Name: a.GetName().GetPart()[0],
Attributes: mapAttributes(a.GetAttrs()),
Endpoints: mapEndpoints(a.GetEndpoints()),
Type: serviceType(a),
Path: makePath(serviceType(a), "/", a.GetName().GetPart()[0]),
}
return newService, nil
}
func serviceType(app *sysl.Application) string {
if syslutil.HasPattern(app.GetAttrs(), GRPC) {
return GRPC
} else if syslutil.HasPattern(app.GetAttrs(), REST) {
return REST
}
return ""
}
func mapAttributes(attributes map[string]*sysl.Attribute) map[string]string {
var attr = make(map[string]string, 15)
for key, value := range attributes {
attr[key] = value.GetS()
}
return attr
}
type Endpoint struct {
Path string
Request string
Response string
}
func mapEndpoints(ep map[string]*sysl.Endpoint) []Endpoint {
var endpoints []Endpoint
for key, value := range ep {
endpoints = append(endpoints, Endpoint{
Path: key,
Request: value.GetName(),
Response: value.GetName(),
})
}
return endpoints
}