/
weaver_gen.go
220 lines (180 loc) · 6.75 KB
/
weaver_gen.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Code generated by "weaver generate". DO NOT EDIT.
//go:build !ignoreWeaverGen
package ledgerwriter
import (
"context"
"errors"
"github.com/ServiceWeaver/weaver"
"github.com/ServiceWeaver/weaver/examples/bankofanthos/model"
"github.com/ServiceWeaver/weaver/runtime/codegen"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"reflect"
)
func init() {
codegen.Register(codegen.Registration{
Name: "github.com/ServiceWeaver/weaver/examples/bankofanthos/ledgerwriter/T",
Iface: reflect.TypeOf((*T)(nil)).Elem(),
Impl: reflect.TypeOf(impl{}),
LocalStubFn: func(impl any, caller string, tracer trace.Tracer) any {
return t_local_stub{impl: impl.(T), tracer: tracer, addTransactionMetrics: codegen.MethodMetricsFor(codegen.MethodLabels{Caller: caller, Component: "github.com/ServiceWeaver/weaver/examples/bankofanthos/ledgerwriter/T", Method: "AddTransaction", Remote: false})}
},
ClientStubFn: func(stub codegen.Stub, caller string) any {
return t_client_stub{stub: stub, addTransactionMetrics: codegen.MethodMetricsFor(codegen.MethodLabels{Caller: caller, Component: "github.com/ServiceWeaver/weaver/examples/bankofanthos/ledgerwriter/T", Method: "AddTransaction", Remote: true})}
},
ServerStubFn: func(impl any, addLoad func(uint64, float64)) codegen.Server {
return t_server_stub{impl: impl.(T), addLoad: addLoad}
},
ReflectStubFn: func(caller func(string, context.Context, []any, []any) error) any {
return t_reflect_stub{caller: caller}
},
RefData: "⟦7237a6f4:wEaVeReDgE:github.com/ServiceWeaver/weaver/examples/bankofanthos/ledgerwriter/T→github.com/ServiceWeaver/weaver/examples/bankofanthos/balancereader/T⟧\n",
})
}
// weaver.InstanceOf checks.
var _ weaver.InstanceOf[T] = (*impl)(nil)
// weaver.Router checks.
var _ weaver.Unrouted = (*impl)(nil)
// Local stub implementations.
type t_local_stub struct {
impl T
tracer trace.Tracer
addTransactionMetrics *codegen.MethodMetrics
}
// Check that t_local_stub implements the T interface.
var _ T = (*t_local_stub)(nil)
func (s t_local_stub) AddTransaction(ctx context.Context, a0 string, a1 string, a2 model.Transaction) (err error) {
// Update metrics.
begin := s.addTransactionMetrics.Begin()
defer func() { s.addTransactionMetrics.End(begin, err != nil, 0, 0) }()
span := trace.SpanFromContext(ctx)
if span.SpanContext().IsValid() {
// Create a child span for this method.
ctx, span = s.tracer.Start(ctx, "ledgerwriter.T.AddTransaction", trace.WithSpanKind(trace.SpanKindInternal))
defer func() {
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
span.End()
}()
}
return s.impl.AddTransaction(ctx, a0, a1, a2)
}
// Client stub implementations.
type t_client_stub struct {
stub codegen.Stub
addTransactionMetrics *codegen.MethodMetrics
}
// Check that t_client_stub implements the T interface.
var _ T = (*t_client_stub)(nil)
func (s t_client_stub) AddTransaction(ctx context.Context, a0 string, a1 string, a2 model.Transaction) (err error) {
// Update metrics.
var requestBytes, replyBytes int
begin := s.addTransactionMetrics.Begin()
defer func() { s.addTransactionMetrics.End(begin, err != nil, requestBytes, replyBytes) }()
span := trace.SpanFromContext(ctx)
if span.SpanContext().IsValid() {
// Create a child span for this method.
ctx, span = s.stub.Tracer().Start(ctx, "ledgerwriter.T.AddTransaction", trace.WithSpanKind(trace.SpanKindClient))
}
defer func() {
// Catch and return any panics detected during encoding/decoding/rpc.
if err == nil {
err = codegen.CatchPanics(recover())
if err != nil {
err = errors.Join(weaver.RemoteCallError, err)
}
}
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
span.End()
}()
// Encode arguments.
enc := codegen.NewEncoder()
enc.String(a0)
enc.String(a1)
(a2).WeaverMarshal(enc)
var shardKey uint64
// Call the remote method.
requestBytes = len(enc.Data())
var results []byte
results, err = s.stub.Run(ctx, 0, enc.Data(), shardKey)
replyBytes = len(results)
if err != nil {
err = errors.Join(weaver.RemoteCallError, err)
return
}
// Decode the results.
dec := codegen.NewDecoder(results)
err = dec.Error()
return
}
// Note that "weaver generate" will always generate the error message below.
// Everything is okay. The error message is only relevant if you see it when
// you run "go build" or "go run".
var _ codegen.LatestVersion = codegen.Version[[0][20]struct{}](`
ERROR: You generated this file with 'weaver generate' (devel) (codegen
version v0.20.0). The generated code is incompatible with the version of the
github.com/ServiceWeaver/weaver module that you're using. The weaver module
version can be found in your go.mod file or by running the following command.
go list -m github.com/ServiceWeaver/weaver
We recommend updating the weaver module and the 'weaver generate' command by
running the following.
go get github.com/ServiceWeaver/weaver@latest
go install github.com/ServiceWeaver/weaver/cmd/weaver@latest
Then, re-run 'weaver generate' and re-build your code. If the problem persists,
please file an issue at https://github.com/ServiceWeaver/weaver/issues.
`)
// Server stub implementations.
type t_server_stub struct {
impl T
addLoad func(key uint64, load float64)
}
// Check that t_server_stub implements the codegen.Server interface.
var _ codegen.Server = (*t_server_stub)(nil)
// GetStubFn implements the codegen.Server interface.
func (s t_server_stub) GetStubFn(method string) func(ctx context.Context, args []byte) ([]byte, error) {
switch method {
case "AddTransaction":
return s.addTransaction
default:
return nil
}
}
func (s t_server_stub) addTransaction(ctx context.Context, args []byte) (res []byte, err error) {
// Catch and return any panics detected during encoding/decoding/rpc.
defer func() {
if err == nil {
err = codegen.CatchPanics(recover())
}
}()
// Decode arguments.
dec := codegen.NewDecoder(args)
var a0 string
a0 = dec.String()
var a1 string
a1 = dec.String()
var a2 model.Transaction
(&a2).WeaverUnmarshal(dec)
// TODO(rgrandl): The deferred function above will recover from panics in the
// user code: fix this.
// Call the local method.
appErr := s.impl.AddTransaction(ctx, a0, a1, a2)
// Encode the results.
enc := codegen.NewEncoder()
enc.Error(appErr)
return enc.Data(), nil
}
// Reflect stub implementations.
type t_reflect_stub struct {
caller func(string, context.Context, []any, []any) error
}
// Check that t_reflect_stub implements the T interface.
var _ T = (*t_reflect_stub)(nil)
func (s t_reflect_stub) AddTransaction(ctx context.Context, a0 string, a1 string, a2 model.Transaction) (err error) {
err = s.caller("AddTransaction", ctx, []any{a0, a1, a2}, []any{})
return
}