github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/example/redirect/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 rpc
     7  
     8  import (
     9  	bytes "bytes"
    10  	context "context"
    11  	fmt "fmt"
    12  	rony "github.com/ronaksoft/rony"
    13  	edge "github.com/ronaksoft/rony/edge"
    14  	edgec "github.com/ronaksoft/rony/edgec"
    15  	errors "github.com/ronaksoft/rony/errors"
    16  	pools "github.com/ronaksoft/rony/pools"
    17  	registry "github.com/ronaksoft/rony/registry"
    18  	tools "github.com/ronaksoft/rony/tools"
    19  	protojson "google.golang.org/protobuf/encoding/protojson"
    20  	proto "google.golang.org/protobuf/proto"
    21  	http "net/http"
    22  	sync "sync"
    23  )
    24  
    25  var _ = pools.Imported
    26  
    27  const C_InfoRequest uint64 = 393641345359739852
    28  
    29  type poolInfoRequest struct {
    30  	pool sync.Pool
    31  }
    32  
    33  func (p *poolInfoRequest) Get() *InfoRequest {
    34  	x, ok := p.pool.Get().(*InfoRequest)
    35  	if !ok {
    36  		x = &InfoRequest{}
    37  	}
    38  
    39  	return x
    40  }
    41  
    42  func (p *poolInfoRequest) Put(x *InfoRequest) {
    43  	if x == nil {
    44  		return
    45  	}
    46  
    47  	x.ReplicaSet = 0
    48  	x.RandomText = ""
    49  
    50  	p.pool.Put(x)
    51  }
    52  
    53  var PoolInfoRequest = poolInfoRequest{}
    54  
    55  func (x *InfoRequest) DeepCopy(z *InfoRequest) {
    56  	z.ReplicaSet = x.ReplicaSet
    57  	z.RandomText = x.RandomText
    58  }
    59  
    60  func (x *InfoRequest) Clone() *InfoRequest {
    61  	z := &InfoRequest{}
    62  	x.DeepCopy(z)
    63  	return z
    64  }
    65  
    66  func (x *InfoRequest) Unmarshal(b []byte) error {
    67  	return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x)
    68  }
    69  
    70  func (x *InfoRequest) Marshal() ([]byte, error) {
    71  	return proto.Marshal(x)
    72  }
    73  
    74  func (x *InfoRequest) UnmarshalJSON(b []byte) error {
    75  	return protojson.Unmarshal(b, x)
    76  }
    77  
    78  func (x *InfoRequest) MarshalJSON() ([]byte, error) {
    79  	return protojson.Marshal(x)
    80  }
    81  
    82  func factoryInfoRequest() registry.Message {
    83  	return &InfoRequest{}
    84  }
    85  
    86  func (x *InfoRequest) PushToContext(ctx *edge.RequestCtx) {
    87  	ctx.PushMessage(C_InfoRequest, x)
    88  }
    89  
    90  const C_InfoResponse uint64 = 10205569623592301307
    91  
    92  type poolInfoResponse struct {
    93  	pool sync.Pool
    94  }
    95  
    96  func (p *poolInfoResponse) Get() *InfoResponse {
    97  	x, ok := p.pool.Get().(*InfoResponse)
    98  	if !ok {
    99  		x = &InfoResponse{}
   100  	}
   101  
   102  	return x
   103  }
   104  
   105  func (p *poolInfoResponse) Put(x *InfoResponse) {
   106  	if x == nil {
   107  		return
   108  	}
   109  
   110  	x.ServerID = ""
   111  	x.RandomText = ""
   112  
   113  	p.pool.Put(x)
   114  }
   115  
   116  var PoolInfoResponse = poolInfoResponse{}
   117  
   118  func (x *InfoResponse) DeepCopy(z *InfoResponse) {
   119  	z.ServerID = x.ServerID
   120  	z.RandomText = x.RandomText
   121  }
   122  
   123  func (x *InfoResponse) Clone() *InfoResponse {
   124  	z := &InfoResponse{}
   125  	x.DeepCopy(z)
   126  	return z
   127  }
   128  
   129  func (x *InfoResponse) Unmarshal(b []byte) error {
   130  	return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x)
   131  }
   132  
   133  func (x *InfoResponse) Marshal() ([]byte, error) {
   134  	return proto.Marshal(x)
   135  }
   136  
   137  func (x *InfoResponse) UnmarshalJSON(b []byte) error {
   138  	return protojson.Unmarshal(b, x)
   139  }
   140  
   141  func (x *InfoResponse) MarshalJSON() ([]byte, error) {
   142  	return protojson.Marshal(x)
   143  }
   144  
   145  func factoryInfoResponse() registry.Message {
   146  	return &InfoResponse{}
   147  }
   148  
   149  func (x *InfoResponse) PushToContext(ctx *edge.RequestCtx) {
   150  	ctx.PushMessage(C_InfoResponse, x)
   151  }
   152  
   153  const C_SampleInfoWithClientRedirect uint64 = 11847639677132253675
   154  const C_SampleInfoWithServerRedirect uint64 = 11816518540128942571
   155  
   156  // register constructors of the messages to the registry package
   157  func init() {
   158  	registry.Register(393641345359739852, "InfoRequest", factoryInfoRequest)
   159  	registry.Register(10205569623592301307, "InfoResponse", factoryInfoResponse)
   160  	registry.Register(11847639677132253675, "SampleInfoWithClientRedirect", factoryInfoRequest)
   161  	registry.Register(11816518540128942571, "SampleInfoWithServerRedirect", factoryInfoRequest)
   162  
   163  }
   164  
   165  var _ = tools.TimeUnix()
   166  
   167  type ISample interface {
   168  	InfoWithClientRedirect(ctx *edge.RequestCtx, req *InfoRequest, res *InfoResponse) *rony.Error
   169  	InfoWithServerRedirect(ctx *edge.RequestCtx, req *InfoRequest, res *InfoResponse) *rony.Error
   170  }
   171  
   172  func RegisterSample(h ISample, e *edge.Server, preHandlers ...edge.Handler) {
   173  	w := sampleWrapper{
   174  		h: h,
   175  	}
   176  	w.Register(e, func(c uint64) []edge.Handler {
   177  		return preHandlers
   178  	})
   179  }
   180  
   181  func RegisterSampleWithFunc(h ISample, e *edge.Server, handlerFunc func(c uint64) []edge.Handler) {
   182  	w := sampleWrapper{
   183  		h: h,
   184  	}
   185  	w.Register(e, handlerFunc)
   186  }
   187  
   188  type sampleWrapper struct {
   189  	h ISample
   190  }
   191  
   192  func (sw *sampleWrapper) infoWithClientRedirectWrapper(ctx *edge.RequestCtx, in *rony.MessageEnvelope) {
   193  	req := &InfoRequest{}
   194  	res := &InfoResponse{}
   195  
   196  	var err error
   197  	if in.JsonEncoded {
   198  		err = protojson.Unmarshal(in.Message, req)
   199  	} else {
   200  		err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req)
   201  	}
   202  	if err != nil {
   203  		ctx.PushError(errors.ErrInvalidRequest)
   204  		return
   205  	}
   206  
   207  	rErr := sw.h.InfoWithClientRedirect(ctx, req, res)
   208  	if rErr != nil {
   209  		ctx.PushError(rErr)
   210  		return
   211  	}
   212  	if !ctx.Stopped() {
   213  		ctx.PushMessage(C_InfoResponse, res)
   214  	}
   215  }
   216  func (sw *sampleWrapper) infoWithServerRedirectWrapper(ctx *edge.RequestCtx, in *rony.MessageEnvelope) {
   217  	req := &InfoRequest{}
   218  	res := &InfoResponse{}
   219  
   220  	var err error
   221  	if in.JsonEncoded {
   222  		err = protojson.Unmarshal(in.Message, req)
   223  	} else {
   224  		err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req)
   225  	}
   226  	if err != nil {
   227  		ctx.PushError(errors.ErrInvalidRequest)
   228  		return
   229  	}
   230  
   231  	rErr := sw.h.InfoWithServerRedirect(ctx, req, res)
   232  	if rErr != nil {
   233  		ctx.PushError(rErr)
   234  		return
   235  	}
   236  	if !ctx.Stopped() {
   237  		ctx.PushMessage(C_InfoResponse, res)
   238  	}
   239  }
   240  
   241  func (sw *sampleWrapper) Register(e *edge.Server, handlerFunc func(c uint64) []edge.Handler) {
   242  	if handlerFunc == nil {
   243  		handlerFunc = func(c uint64) []edge.Handler {
   244  			return nil
   245  		}
   246  	}
   247  	e.SetHandler(
   248  		edge.NewHandlerOptions().
   249  			SetConstructor(C_SampleInfoWithClientRedirect).
   250  			SetServiceName("Sample").
   251  			SetMethodName("InfoWithClientRedirect").
   252  			SetHandler(handlerFunc(C_SampleInfoWithClientRedirect)...).
   253  			Append(sw.infoWithClientRedirectWrapper),
   254  	)
   255  	e.SetRestProxy(
   256  		"get", "/info/client-redirect/:ReplicaSet/:RandomText",
   257  		edge.NewRestProxy(sw.infoWithClientRedirectRestClient, sw.infoWithClientRedirectRestServer),
   258  	)
   259  	e.SetHandler(
   260  		edge.NewHandlerOptions().
   261  			SetConstructor(C_SampleInfoWithServerRedirect).
   262  			SetServiceName("Sample").
   263  			SetMethodName("InfoWithServerRedirect").
   264  			SetHandler(handlerFunc(C_SampleInfoWithServerRedirect)...).
   265  			Append(sw.infoWithServerRedirectWrapper),
   266  	)
   267  	e.SetRestProxy(
   268  		"get", "/info/server-redirect/:ReplicaSet/:RandomText",
   269  		edge.NewRestProxy(sw.infoWithServerRedirectRestClient, sw.infoWithServerRedirectRestServer),
   270  	)
   271  }
   272  
   273  func TunnelRequestSampleInfoWithClientRedirect(
   274  	ctx *edge.RequestCtx, replicaSet uint64,
   275  	req *InfoRequest, res *InfoResponse,
   276  	kvs ...*rony.KeyValue,
   277  ) error {
   278  	out := rony.PoolMessageEnvelope.Get()
   279  	defer rony.PoolMessageEnvelope.Put(out)
   280  	in := rony.PoolMessageEnvelope.Get()
   281  	defer rony.PoolMessageEnvelope.Put(in)
   282  	out.Fill(ctx.ReqID(), C_SampleInfoWithClientRedirect, req, kvs...)
   283  	err := ctx.TunnelRequest(replicaSet, out, in)
   284  	if err != nil {
   285  		return err
   286  	}
   287  
   288  	switch in.GetConstructor() {
   289  	case C_InfoResponse:
   290  		_ = res.Unmarshal(in.GetMessage())
   291  		return nil
   292  	case rony.C_Error:
   293  		x := &rony.Error{}
   294  		_ = x.Unmarshal(in.GetMessage())
   295  		return x
   296  	default:
   297  		return errors.ErrUnexpectedTunnelResponse
   298  	}
   299  }
   300  func TunnelRequestSampleInfoWithServerRedirect(
   301  	ctx *edge.RequestCtx, replicaSet uint64,
   302  	req *InfoRequest, res *InfoResponse,
   303  	kvs ...*rony.KeyValue,
   304  ) error {
   305  	out := rony.PoolMessageEnvelope.Get()
   306  	defer rony.PoolMessageEnvelope.Put(out)
   307  	in := rony.PoolMessageEnvelope.Get()
   308  	defer rony.PoolMessageEnvelope.Put(in)
   309  	out.Fill(ctx.ReqID(), C_SampleInfoWithServerRedirect, req, kvs...)
   310  	err := ctx.TunnelRequest(replicaSet, out, in)
   311  	if err != nil {
   312  		return err
   313  	}
   314  
   315  	switch in.GetConstructor() {
   316  	case C_InfoResponse:
   317  		_ = res.Unmarshal(in.GetMessage())
   318  		return nil
   319  	case rony.C_Error:
   320  		x := &rony.Error{}
   321  		_ = x.Unmarshal(in.GetMessage())
   322  		return x
   323  	default:
   324  		return errors.ErrUnexpectedTunnelResponse
   325  	}
   326  }
   327  
   328  func (sw *sampleWrapper) infoWithClientRedirectRestClient(conn rony.RestConn, ctx *edge.DispatchCtx) error {
   329  	req := &InfoRequest{}
   330  	req.ReplicaSet = tools.StrToUInt64(tools.GetString(conn.Get("ReplicaSet"), "0"))
   331  	req.RandomText = tools.GetString(conn.Get("RandomText"), "")
   332  
   333  	ctx.Fill(conn.ConnID(), C_SampleInfoWithClientRedirect, req)
   334  	return nil
   335  }
   336  func (sw *sampleWrapper) infoWithClientRedirectRestServer(conn rony.RestConn, ctx *edge.DispatchCtx) (err error) {
   337  	conn.WriteHeader("Content-Type", "application/json")
   338  	if !ctx.BufferPop(func(envelope *rony.MessageEnvelope) {
   339  		switch envelope.Constructor {
   340  		case C_InfoResponse:
   341  			x := &InfoResponse{}
   342  			_ = x.Unmarshal(envelope.Message)
   343  			var b []byte
   344  			b, err = x.MarshalJSON()
   345  			if err != nil {
   346  				return
   347  			}
   348  			err = conn.WriteBinary(ctx.StreamID(), b)
   349  			return
   350  		case rony.C_Error:
   351  			x := &rony.Error{}
   352  			_ = x.Unmarshal(envelope.Message)
   353  			err = x
   354  			return
   355  		case rony.C_Redirect:
   356  			x := &rony.Redirect{}
   357  			_ = x.Unmarshal(envelope.Message)
   358  			if len(x.Edges) == 0 || len(x.Edges[0].HostPorts) == 0 {
   359  				break
   360  			}
   361  			switch x.Reason {
   362  			case rony.RedirectReason_ReplicaSetSession:
   363  				conn.Redirect(http.StatusPermanentRedirect, x.Edges[0].HostPorts[0])
   364  			case rony.RedirectReason_ReplicaSetRequest:
   365  				conn.Redirect(http.StatusTemporaryRedirect, x.Edges[0].HostPorts[0])
   366  			}
   367  			return
   368  		}
   369  		err = errors.ErrUnexpectedResponse
   370  	}) {
   371  		err = errors.ErrInternalServer
   372  	}
   373  
   374  	return
   375  }
   376  
   377  func (sw *sampleWrapper) infoWithServerRedirectRestClient(conn rony.RestConn, ctx *edge.DispatchCtx) error {
   378  	req := &InfoRequest{}
   379  	req.ReplicaSet = tools.StrToUInt64(tools.GetString(conn.Get("ReplicaSet"), "0"))
   380  	req.RandomText = tools.GetString(conn.Get("RandomText"), "")
   381  
   382  	ctx.Fill(conn.ConnID(), C_SampleInfoWithServerRedirect, req)
   383  	return nil
   384  }
   385  func (sw *sampleWrapper) infoWithServerRedirectRestServer(conn rony.RestConn, ctx *edge.DispatchCtx) (err error) {
   386  	conn.WriteHeader("Content-Type", "application/json")
   387  	if !ctx.BufferPop(func(envelope *rony.MessageEnvelope) {
   388  		switch envelope.Constructor {
   389  		case C_InfoResponse:
   390  			x := &InfoResponse{}
   391  			_ = x.Unmarshal(envelope.Message)
   392  			var b []byte
   393  			b, err = x.MarshalJSON()
   394  			if err != nil {
   395  				return
   396  			}
   397  			err = conn.WriteBinary(ctx.StreamID(), b)
   398  			return
   399  		case rony.C_Error:
   400  			x := &rony.Error{}
   401  			_ = x.Unmarshal(envelope.Message)
   402  			err = x
   403  			return
   404  		case rony.C_Redirect:
   405  			x := &rony.Redirect{}
   406  			_ = x.Unmarshal(envelope.Message)
   407  			if len(x.Edges) == 0 || len(x.Edges[0].HostPorts) == 0 {
   408  				break
   409  			}
   410  			switch x.Reason {
   411  			case rony.RedirectReason_ReplicaSetSession:
   412  				conn.Redirect(http.StatusPermanentRedirect, x.Edges[0].HostPorts[0])
   413  			case rony.RedirectReason_ReplicaSetRequest:
   414  				conn.Redirect(http.StatusTemporaryRedirect, x.Edges[0].HostPorts[0])
   415  			}
   416  			return
   417  		}
   418  		err = errors.ErrUnexpectedResponse
   419  	}) {
   420  		err = errors.ErrInternalServer
   421  	}
   422  
   423  	return
   424  }
   425  
   426  type ISampleClient interface {
   427  	InfoWithClientRedirect(ctx context.Context, req *InfoRequest, kvs ...*rony.KeyValue) (*InfoResponse, error)
   428  	InfoWithServerRedirect(ctx context.Context, req *InfoRequest, kvs ...*rony.KeyValue) (*InfoResponse, error)
   429  }
   430  
   431  type SampleClient struct {
   432  	name string
   433  	c    edgec.Client
   434  }
   435  
   436  func NewSampleClient(name string, ec edgec.Client) *SampleClient {
   437  	return &SampleClient{
   438  		name: name,
   439  		c:    ec,
   440  	}
   441  }
   442  func (c *SampleClient) InfoWithClientRedirect(
   443  	ctx context.Context, req *InfoRequest, kvs ...*rony.KeyValue,
   444  ) (*InfoResponse, error) {
   445  	out := rony.PoolMessageEnvelope.Get()
   446  	defer rony.PoolMessageEnvelope.Put(out)
   447  	in := rony.PoolMessageEnvelope.Get()
   448  	defer rony.PoolMessageEnvelope.Put(in)
   449  	out.Fill(c.c.GetRequestID(), C_SampleInfoWithClientRedirect, req, kvs...)
   450  	err := c.c.Send(ctx, out, in)
   451  	if err != nil {
   452  		return nil, err
   453  	}
   454  	switch in.GetConstructor() {
   455  	case C_InfoResponse:
   456  		x := &InfoResponse{}
   457  		_ = proto.Unmarshal(in.Message, x)
   458  		return x, nil
   459  	case rony.C_Error:
   460  		x := &rony.Error{}
   461  		_ = x.Unmarshal(in.Message)
   462  		return nil, x
   463  	default:
   464  		return nil, fmt.Errorf("unknown message :%d", in.GetConstructor())
   465  	}
   466  }
   467  func (c *SampleClient) InfoWithServerRedirect(
   468  	ctx context.Context, req *InfoRequest, kvs ...*rony.KeyValue,
   469  ) (*InfoResponse, error) {
   470  	out := rony.PoolMessageEnvelope.Get()
   471  	defer rony.PoolMessageEnvelope.Put(out)
   472  	in := rony.PoolMessageEnvelope.Get()
   473  	defer rony.PoolMessageEnvelope.Put(in)
   474  	out.Fill(c.c.GetRequestID(), C_SampleInfoWithServerRedirect, req, kvs...)
   475  	err := c.c.Send(ctx, out, in)
   476  	if err != nil {
   477  		return nil, err
   478  	}
   479  	switch in.GetConstructor() {
   480  	case C_InfoResponse:
   481  		x := &InfoResponse{}
   482  		_ = proto.Unmarshal(in.Message, x)
   483  		return x, nil
   484  	case rony.C_Error:
   485  		x := &rony.Error{}
   486  		_ = x.Unmarshal(in.Message)
   487  		return nil, x
   488  	default:
   489  		return nil, fmt.Errorf("unknown message :%d", in.GetConstructor())
   490  	}
   491  }
   492  
   493  var _ = bytes.MinRead