-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathgrpc_service.go
113 lines (101 loc) Β· 2.84 KB
/
grpc_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
package expr
import (
"fmt"
"goa.design/goa/eval"
)
type (
// GRPCServiceExpr describes a gRPC service.
GRPCServiceExpr struct {
eval.DSLFunc
// ServiceExpr is the service expression that backs this service.
ServiceExpr *ServiceExpr
// Name of parent service if any
ParentName string
// GRPCEndpoints is the list of service endpoints.
GRPCEndpoints []*GRPCEndpointExpr
// GRPCErrors lists gRPC errors that apply to all endpoints.
GRPCErrors []*GRPCErrorExpr
// Meta is a set of key/value pairs with semantic that is
// specific to each generator.
Meta MetaExpr
}
)
// Name of service (service)
func (svc *GRPCServiceExpr) Name() string {
return svc.ServiceExpr.Name
}
// Description of service (service)
func (svc *GRPCServiceExpr) Description() string {
return svc.ServiceExpr.Description
}
// Endpoint returns the service endpoint with the given name or nil if there
// isn't one.
func (svc *GRPCServiceExpr) Endpoint(name string) *GRPCEndpointExpr {
for _, a := range svc.GRPCEndpoints {
if a.Name() == name {
return a
}
}
return nil
}
// EndpointFor builds the endpoint for the given method.
func (svc *GRPCServiceExpr) EndpointFor(name string, m *MethodExpr) *GRPCEndpointExpr {
if a := svc.Endpoint(name); a != nil {
return a
}
a := &GRPCEndpointExpr{
MethodExpr: m,
Service: svc,
}
svc.GRPCEndpoints = append(svc.GRPCEndpoints, a)
return a
}
// Error returns the error with the given name.
func (svc *GRPCServiceExpr) Error(name string) *ErrorExpr {
for _, erro := range svc.ServiceExpr.Errors {
if erro.Name == name {
return erro
}
}
return Root.Error(name)
}
// GRPCError returns the service gRPC error with given name if any.
func (svc *GRPCServiceExpr) GRPCError(name string) *GRPCErrorExpr {
for _, erro := range svc.GRPCErrors {
if erro.Name == name {
return erro
}
}
return nil
}
// EvalName returns the generic definition name used in error messages.
func (svc *GRPCServiceExpr) EvalName() string {
if svc.Name() == "" {
return "unnamed service"
}
return fmt.Sprintf("service %#v", svc.Name())
}
// Prepare initializes the error responses.
func (svc *GRPCServiceExpr) Prepare() {
for _, er := range svc.GRPCErrors {
er.Response.Prepare()
}
}
// Validate makes sure the service is valid.
func (svc *GRPCServiceExpr) Validate() error {
verr := new(eval.ValidationErrors)
// Validate errors
for _, er := range svc.GRPCErrors {
verr.Merge(er.Validate())
}
for _, er := range Root.API.GRPC.Errors {
// This may result in the same error being validated multiple
// times however service is the top level expression being
// walked and errors cannot be walked until all expressions have
// run. Another solution could be to append a new dynamically
// generated root that the eval engine would process after. Keep
// things simple for now.
verr.Merge(er.Validate())
}
return verr
}