/
caller_template.go
126 lines (107 loc) · 3.15 KB
/
caller_template.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
125
126
package qrpc
const callerTemplate = `
{{- "" -}}
{{- $contextPkg := .ContextPkg -}}
{{- $qrpcPkg := .QrpcPkg -}}
{{- $uuidPkg := .UUIDPkg -}}
{{- range .Services }}
{{- $service := .Name -}}
{{- $clientInt := ($service | sprintf "Caller%sClient") -}}
{{- $clientType := ($service | sprintf "caller%sClient") -}}
{{- $serverInt := ($service | sprintf "Caller%sServer") }}
// Caller API description
{{- if (.ForwardMethods | concat .BidirectionalMethods) }}
// {{ $clientInt }} is the caller client API for {{ $service }} service.
{{- if .IsDeprecated }}
//
// Deprecated: Do not use
{{- end }}
type {{ $clientInt }} interface {
{{- range (.ForwardMethods | concat .BidirectionalMethods) }}
{{ .Comment }}
{{- if .IsDeprecated }}
//
// Deprecated: Do not use.
{{- end }}
{{ .Name }}(ctx {{ $contextPkg }}.Context, in *{{ .InType }}) (string, error)
{{- end }}
}
type {{ $clientType }} struct {
cc *{{ $qrpcPkg }}.ClientConn
}
{{- if .IsDeprecated }}
//
// Deprecated: Do not use.
{{- end }}
func New{{ $clientInt }}(cc *{{ $qrpcPkg }}.ClientConn) {{ $clientInt }} {
cc.SetService("qrpc.test.api.{{ $service }}.in")
return &{{ $clientType }}{cc}
}
{{- range (.ForwardMethods | concat .BidirectionalMethods) }}
{{ .Comment }}
{{- if .IsDeprecated }}
//
// Deprecated: Do not use.
{{- end }}
func (c *{{ $clientType }}) {{ .Name }}(ctx {{ $contextPkg }}.Context, in *{{ .InType }}) (string, error) {
data, err := proto.Marshal(in)
if err != nil {
return "", err
}
msg := {{ $qrpcPkg }}.Message{
Method: "{{ .Name }}",
RequestID: {{ $uuidPkg }}.New().String(),
Data: data,
}
return msg.RequestID, c.cc.Invoke(ctx, msg)
}
{{- end }}
{{- end }}
{{- if (.BackwardMethods | concat .BidirectionalMethods) }}
// {{ $serverInt }} is the caller server API for {{ $service }} service.
// It should be used to handle responses from backward methods and bidirectional methods.
{{- if .IsDeprecated }}
//
// Deprecated: Do not use.
{{- end }}
type {{ $serverInt }} interface {
{{- range (.BackwardMethods | concat .BidirectionalMethods) }}
{{ .Comment }}
{{- if .IsDeprecated }}
//
// Deprecated: Do not use.
{{- end }}
{{ .Name }}(ctx {{ $contextPkg }}.Context, reqID string, out *{{ .OutType }}) error
{{- end }}
}
{{- if .IsDeprecated }}
//
// Deprecated: Do not use.
{{- end }}
func Register{{ $serverInt }}(s *{{ $qrpcPkg }}.Server, srv {{ $serverInt }}) {
s.RegisterService(&_Caller{{ $service }}_serviceDesc, srv)
}
{{- range (.BackwardMethods | concat .BidirectionalMethods) }}
func _Caller{{ $service }}_{{ .Name }}_Handler(srv interface{}, ctx {{ $contextPkg }}.Context, reqID string, msg []byte) error {
out := new({{ .OutType }})
if err := proto.Unmarshal(msg, out); err != nil {
return err
}
return srv.({{ $serverInt }}).{{ .Name }}(ctx, reqID, out)
}
{{- end }}
var _Caller{{ $service }}_serviceDesc = {{ $qrpcPkg }}.ServiceDesc{
ServiceName: "qrpc.test.api.{{ $service }}.out",
HandlerType: (*{{ $serverInt }})(nil),
Methods: []{{ $qrpcPkg }}.MethodDesc{
{{- range (.BackwardMethods | concat .BidirectionalMethods) }}
{
MethodName: "{{ .Name }}",
Handler: _Caller{{ $service }}_{{ .Name }}_Handler,
},
{{- end }}
},
}
{{- end }}
{{- end }}
`