github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/example/echo/rpc/sample.rony.go (about) 1 // Code generated by Rony's protoc plugin; DO NOT EDIT. 2 // ProtoC ver. v3.17.3 3 // Rony ver. v0.16.24 4 // Source: sample.proto 5 6 package service 7 8 import ( 9 bytes "bytes" 10 context "context" 11 fmt "fmt" 12 rony "github.com/ronaksoft/rony" 13 config "github.com/ronaksoft/rony/config" 14 edge "github.com/ronaksoft/rony/edge" 15 edgec "github.com/ronaksoft/rony/edgec" 16 errors "github.com/ronaksoft/rony/errors" 17 pools "github.com/ronaksoft/rony/pools" 18 registry "github.com/ronaksoft/rony/registry" 19 tools "github.com/ronaksoft/rony/tools" 20 cobra "github.com/spf13/cobra" 21 protojson "google.golang.org/protobuf/encoding/protojson" 22 proto "google.golang.org/protobuf/proto" 23 http "net/http" 24 sync "sync" 25 ) 26 27 var _ = pools.Imported 28 29 const C_EchoRequest uint64 = 634453994073422796 30 31 type poolEchoRequest struct { 32 pool sync.Pool 33 } 34 35 func (p *poolEchoRequest) Get() *EchoRequest { 36 x, ok := p.pool.Get().(*EchoRequest) 37 if !ok { 38 x = &EchoRequest{} 39 } 40 41 return x 42 } 43 44 func (p *poolEchoRequest) Put(x *EchoRequest) { 45 if x == nil { 46 return 47 } 48 49 x.ID = 0 50 x.RandomText = "" 51 52 p.pool.Put(x) 53 } 54 55 var PoolEchoRequest = poolEchoRequest{} 56 57 func (x *EchoRequest) DeepCopy(z *EchoRequest) { 58 z.ID = x.ID 59 z.RandomText = x.RandomText 60 } 61 62 func (x *EchoRequest) Clone() *EchoRequest { 63 z := &EchoRequest{} 64 x.DeepCopy(z) 65 return z 66 } 67 68 func (x *EchoRequest) Unmarshal(b []byte) error { 69 return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x) 70 } 71 72 func (x *EchoRequest) Marshal() ([]byte, error) { 73 return proto.Marshal(x) 74 } 75 76 func (x *EchoRequest) UnmarshalJSON(b []byte) error { 77 return protojson.Unmarshal(b, x) 78 } 79 80 func (x *EchoRequest) MarshalJSON() ([]byte, error) { 81 return protojson.Marshal(x) 82 } 83 84 func factoryEchoRequest() registry.Message { 85 return &EchoRequest{} 86 } 87 88 func (x *EchoRequest) PushToContext(ctx *edge.RequestCtx) { 89 ctx.PushMessage(C_EchoRequest, x) 90 } 91 92 const C_EchoResponse uint64 = 10208763112635265787 93 94 type poolEchoResponse struct { 95 pool sync.Pool 96 } 97 98 func (p *poolEchoResponse) Get() *EchoResponse { 99 x, ok := p.pool.Get().(*EchoResponse) 100 if !ok { 101 x = &EchoResponse{} 102 } 103 104 return x 105 } 106 107 func (p *poolEchoResponse) Put(x *EchoResponse) { 108 if x == nil { 109 return 110 } 111 112 x.ReqID = 0 113 x.RandomText = "" 114 115 p.pool.Put(x) 116 } 117 118 var PoolEchoResponse = poolEchoResponse{} 119 120 func (x *EchoResponse) DeepCopy(z *EchoResponse) { 121 z.ReqID = x.ReqID 122 z.RandomText = x.RandomText 123 } 124 125 func (x *EchoResponse) Clone() *EchoResponse { 126 z := &EchoResponse{} 127 x.DeepCopy(z) 128 return z 129 } 130 131 func (x *EchoResponse) Unmarshal(b []byte) error { 132 return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x) 133 } 134 135 func (x *EchoResponse) Marshal() ([]byte, error) { 136 return proto.Marshal(x) 137 } 138 139 func (x *EchoResponse) UnmarshalJSON(b []byte) error { 140 return protojson.Unmarshal(b, x) 141 } 142 143 func (x *EchoResponse) MarshalJSON() ([]byte, error) { 144 return protojson.Marshal(x) 145 } 146 147 func factoryEchoResponse() registry.Message { 148 return &EchoResponse{} 149 } 150 151 func (x *EchoResponse) PushToContext(ctx *edge.RequestCtx) { 152 ctx.PushMessage(C_EchoResponse, x) 153 } 154 155 const C_SampleEcho uint64 = 5610266072904040111 156 157 // register constructors of the messages to the registry package 158 func init() { 159 registry.Register(634453994073422796, "EchoRequest", factoryEchoRequest) 160 registry.Register(10208763112635265787, "EchoResponse", factoryEchoResponse) 161 registry.Register(5610266072904040111, "SampleEcho", factoryEchoRequest) 162 163 } 164 165 var _ = tools.TimeUnix() 166 167 type ISample interface { 168 Echo(ctx *edge.RequestCtx, req *EchoRequest, res *EchoResponse) *rony.Error 169 } 170 171 func RegisterSample(h ISample, e *edge.Server, preHandlers ...edge.Handler) { 172 w := sampleWrapper{ 173 h: h, 174 } 175 w.Register(e, func(c uint64) []edge.Handler { 176 return preHandlers 177 }) 178 } 179 180 func RegisterSampleWithFunc(h ISample, e *edge.Server, handlerFunc func(c uint64) []edge.Handler) { 181 w := sampleWrapper{ 182 h: h, 183 } 184 w.Register(e, handlerFunc) 185 } 186 187 type sampleWrapper struct { 188 h ISample 189 } 190 191 func (sw *sampleWrapper) echoWrapper(ctx *edge.RequestCtx, in *rony.MessageEnvelope) { 192 req := &EchoRequest{} 193 res := &EchoResponse{} 194 195 var err error 196 if in.JsonEncoded { 197 err = protojson.Unmarshal(in.Message, req) 198 } else { 199 err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req) 200 } 201 if err != nil { 202 ctx.PushError(errors.ErrInvalidRequest) 203 return 204 } 205 206 rErr := sw.h.Echo(ctx, req, res) 207 if rErr != nil { 208 ctx.PushError(rErr) 209 return 210 } 211 if !ctx.Stopped() { 212 ctx.PushMessage(C_EchoResponse, res) 213 } 214 } 215 216 func (sw *sampleWrapper) Register(e *edge.Server, handlerFunc func(c uint64) []edge.Handler) { 217 if handlerFunc == nil { 218 handlerFunc = func(c uint64) []edge.Handler { 219 return nil 220 } 221 } 222 e.SetHandler( 223 edge.NewHandlerOptions(). 224 SetConstructor(C_SampleEcho). 225 SetServiceName("Sample"). 226 SetMethodName("Echo"). 227 SetHandler(handlerFunc(C_SampleEcho)...). 228 Append(sw.echoWrapper), 229 ) 230 e.SetRestProxy( 231 "get", "/echo/:ID/:Random", 232 edge.NewRestProxy(sw.echoRestClient, sw.echoRestServer), 233 ) 234 } 235 236 func TunnelRequestSampleEcho( 237 ctx *edge.RequestCtx, replicaSet uint64, 238 req *EchoRequest, res *EchoResponse, 239 kvs ...*rony.KeyValue, 240 ) error { 241 out := rony.PoolMessageEnvelope.Get() 242 defer rony.PoolMessageEnvelope.Put(out) 243 in := rony.PoolMessageEnvelope.Get() 244 defer rony.PoolMessageEnvelope.Put(in) 245 out.Fill(ctx.ReqID(), C_SampleEcho, req, kvs...) 246 err := ctx.TunnelRequest(replicaSet, out, in) 247 if err != nil { 248 return err 249 } 250 251 switch in.GetConstructor() { 252 case C_EchoResponse: 253 _ = res.Unmarshal(in.GetMessage()) 254 return nil 255 case rony.C_Error: 256 x := &rony.Error{} 257 _ = x.Unmarshal(in.GetMessage()) 258 return x 259 default: 260 return errors.ErrUnexpectedTunnelResponse 261 } 262 } 263 264 func (sw *sampleWrapper) echoRestClient(conn rony.RestConn, ctx *edge.DispatchCtx) error { 265 req := &EchoRequest{} 266 req.ID = tools.StrToInt64(tools.GetString(conn.Get("ID"), "0")) 267 req.RandomText = tools.GetString(conn.Get("Random"), "") 268 269 ctx.Fill(conn.ConnID(), C_SampleEcho, req) 270 return nil 271 } 272 func (sw *sampleWrapper) echoRestServer(conn rony.RestConn, ctx *edge.DispatchCtx) (err error) { 273 conn.WriteHeader("Content-Type", "application/json") 274 if !ctx.BufferPop(func(envelope *rony.MessageEnvelope) { 275 switch envelope.Constructor { 276 case C_EchoResponse: 277 x := &EchoResponse{} 278 _ = x.Unmarshal(envelope.Message) 279 var b []byte 280 b, err = x.MarshalJSON() 281 if err != nil { 282 return 283 } 284 err = conn.WriteBinary(ctx.StreamID(), b) 285 return 286 case rony.C_Error: 287 x := &rony.Error{} 288 _ = x.Unmarshal(envelope.Message) 289 err = x 290 return 291 case rony.C_Redirect: 292 x := &rony.Redirect{} 293 _ = x.Unmarshal(envelope.Message) 294 if len(x.Edges) == 0 || len(x.Edges[0].HostPorts) == 0 { 295 break 296 } 297 switch x.Reason { 298 case rony.RedirectReason_ReplicaSetSession: 299 conn.Redirect(http.StatusPermanentRedirect, x.Edges[0].HostPorts[0]) 300 case rony.RedirectReason_ReplicaSetRequest: 301 conn.Redirect(http.StatusTemporaryRedirect, x.Edges[0].HostPorts[0]) 302 } 303 return 304 } 305 err = errors.ErrUnexpectedResponse 306 }) { 307 err = errors.ErrInternalServer 308 } 309 310 return 311 } 312 313 type ISampleClient interface { 314 Echo(ctx context.Context, req *EchoRequest, kvs ...*rony.KeyValue) (*EchoResponse, error) 315 } 316 317 type SampleClient struct { 318 name string 319 c edgec.Client 320 } 321 322 func NewSampleClient(name string, ec edgec.Client) *SampleClient { 323 return &SampleClient{ 324 name: name, 325 c: ec, 326 } 327 } 328 func (c *SampleClient) Echo( 329 ctx context.Context, req *EchoRequest, kvs ...*rony.KeyValue, 330 ) (*EchoResponse, error) { 331 out := rony.PoolMessageEnvelope.Get() 332 defer rony.PoolMessageEnvelope.Put(out) 333 in := rony.PoolMessageEnvelope.Get() 334 defer rony.PoolMessageEnvelope.Put(in) 335 out.Fill(c.c.GetRequestID(), C_SampleEcho, req, kvs...) 336 err := c.c.Send(ctx, out, in) 337 if err != nil { 338 return nil, err 339 } 340 switch in.GetConstructor() { 341 case C_EchoResponse: 342 x := &EchoResponse{} 343 _ = proto.Unmarshal(in.Message, x) 344 return x, nil 345 case rony.C_Error: 346 x := &rony.Error{} 347 _ = x.Unmarshal(in.Message) 348 return nil, x 349 default: 350 return nil, fmt.Errorf("unknown message :%d", in.GetConstructor()) 351 } 352 } 353 354 func prepareSampleCommand(cmd *cobra.Command, c edgec.Client) (*SampleClient, error) { 355 // Bind current flags to the registered flags in config package 356 err := config.BindCmdFlags(cmd) 357 if err != nil { 358 return nil, err 359 } 360 361 return NewSampleClient("Sample", c), nil 362 } 363 364 var genSampleEchoCmd = func(h ISampleCli, c edgec.Client) *cobra.Command { 365 cmd := &cobra.Command{ 366 Use: "echo", 367 RunE: func(cmd *cobra.Command, args []string) error { 368 cli, err := prepareSampleCommand(cmd, c) 369 if err != nil { 370 return err 371 } 372 return h.Echo(cli, cmd, args) 373 }, 374 } 375 config.SetFlags(cmd, 376 config.Int64Flag("id", tools.StrToInt64(""), ""), 377 config.StringFlag("randomText", "", ""), 378 ) 379 return cmd 380 } 381 382 type ISampleCli interface { 383 Echo(cli *SampleClient, cmd *cobra.Command, args []string) error 384 } 385 386 func RegisterSampleCli(h ISampleCli, c edgec.Client, rootCmd *cobra.Command) { 387 subCommand := &cobra.Command{ 388 Use: "Sample", 389 } 390 subCommand.AddCommand( 391 genSampleEchoCmd(h, c), 392 ) 393 394 rootCmd.AddCommand(subCommand) 395 } 396 397 var _ = bytes.MinRead