-
Notifications
You must be signed in to change notification settings - Fork 46
/
service.go
124 lines (100 loc) · 3.22 KB
/
service.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package schema
import (
"fmt"
"strings"
)
type Service struct {
Name string `json:"name"`
Methods []*Method `json:"methods"`
Comments []string `json:"comments"`
Schema *WebRPCSchema `json:"-"` // denormalize/back-reference
}
type Method struct {
Name string `json:"name"`
Comments []string `json:"comments"`
StreamInput bool `json:"streamInput,omitempty"`
StreamOutput bool `json:"streamOutput,omitempty"`
Proxy bool `json:"-"` // TODO: actual implementation
Inputs []*MethodArgument `json:"inputs"`
Outputs []*MethodArgument `json:"outputs"`
Service *Service `json:"-"` // denormalize/back-reference
}
type MethodArgument struct {
Name string `json:"name"`
Type *VarType `json:"type"`
Optional bool `json:"optional"`
InputArg bool `json:"-"` // denormalize/back-reference
OutputArg bool `json:"-"` // denormalize/back-reference
TypeExtra `json:",omitempty"`
}
func (s *Service) Parse(schema *WebRPCSchema) error {
s.Schema = schema // back-ref
// Service name
serviceName := string(s.Name)
if string(s.Name) == "" {
return fmt.Errorf("schema error: service name cannot be empty")
}
// Ensure we don't have dupe service names (w/ normalization)
name := strings.ToLower(string(s.Name))
for _, svc := range schema.Services {
if svc != s && name == strings.ToLower(string(svc.Name)) {
return fmt.Errorf("schema error: duplicate service name detected in service '%s'", serviceName)
}
}
// Ensure methods is defined
if len(s.Methods) == 0 {
return fmt.Errorf("schema error: methods cannot be empty for service '%s'", serviceName)
}
// Verify method names and ensure we don't have any duplicate method names
methodList := map[string]string{}
for _, method := range s.Methods {
if string(method.Name) == "" {
return fmt.Errorf("schema error: detected empty method name in service '%s", serviceName)
}
methodName := string(method.Name)
methodNameLower := strings.ToLower(methodName)
if _, ok := methodList[methodNameLower]; ok {
return fmt.Errorf("schema error: detected duplicate method name of '%s' in service '%s'", methodName, serviceName)
}
methodList[methodNameLower] = methodName
}
// Parse+validate methods
for _, method := range s.Methods {
err := method.Parse(schema, s)
if err != nil {
return err
}
}
return nil
}
func (m *Method) Parse(schema *WebRPCSchema, service *Service) error {
if service == nil {
return fmt.Errorf("parse error, service arg cannot be nil")
}
m.Service = service // back-ref
serviceName := string(service.Name)
// Parse+validate inputs
for _, input := range m.Inputs {
input.InputArg = true // back-ref
if input.Name == "" {
return fmt.Errorf("schema error: detected empty input argument name for method '%s' in service '%s'", m.Name, serviceName)
}
err := input.Type.Parse(schema)
if err != nil {
return err
}
}
// Parse+validate outputs
for _, output := range m.Outputs {
output.OutputArg = true // back-ref
if output.Name == "" {
return fmt.Errorf("schema error: detected empty output name for method '%s' in service '%s'", m.Name, serviceName)
}
err := output.Type.Parse(schema)
if err != nil {
return err
}
}
// Note, we allow zero inputs and zero outputs
return nil
}