wrap

package
v0.0.0-...-97303ad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2025 License: MIT Imports: 11 Imported by: 10

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrMethodNotFound = status.Error(codes.Unimplemented, "method not found")
View Source
var ErrMethodShape = status.Error(codes.Internal, "method stream shape mismatch")

Functions

func ServerToClient

func ServerToClient(desc grpc.ServiceDesc, srv any) grpc.ClientConnInterface

ServerToClient returns a grpc.ClientConnInterface that can service all methods described by desc. srv is the gRPC server implementation type (a type that could be passed to a grpc RegisterXxxServer function).

Unary and streaming calls are supported.

The call options grpc.Header and grpc.Trailer are supported for unary RPCs. All other call options are ignored.

Example
package main

import (
	"context"
	"fmt"

	"github.com/smart-core-os/sc-golang/internal/testproto"
)

// Given the following gRPC service definition:
//
//     syntax = "proto3";
//     service TestApi {
//     	 rpc Unary(UnaryRequest) returns (UnaryResponse);
//     }
//     message UnaryRequest {
//     	 string msg = 1;
//     }
//     message UnaryResponse {
//     	 string msg = 1;
//     }

func main() {
	srv := &exampleServer{}
	conn := ServerToClient(testproto.TestApi_ServiceDesc, srv)
	client := testproto.NewTestApiClient(conn)

	res, err := client.Unary(context.Background(), &testproto.UnaryRequest{Msg: "hello"})
	if err != nil {
		panic(err)
	}
	fmt.Println(res.Msg)
}

type exampleServer struct {
	testproto.UnimplementedTestApiServer
}

func (s *exampleServer) Unary(ctx context.Context, req *testproto.UnaryRequest) (*testproto.UnaryResponse, error) {
	return &testproto.UnaryResponse{Msg: req.Msg}, nil
}
Output:

hello

func UnwrapFully

func UnwrapFully(obj any) any

UnwrapFully repeatedly casts then unwraps obj until obj no longer implements Unwrapper.

Types

type ClientServerStream

type ClientServerStream struct {
	// contains filtered or unexported fields
}

ClientServerStream combines both a grpc.ServerStream and grpc.ClientStream

func NewClientServerStream

func NewClientServerStream(ctx context.Context) *ClientServerStream

func (*ClientServerStream) Client

func (s *ClientServerStream) Client() grpc.ClientStream

func (*ClientServerStream) Close

func (s *ClientServerStream) Close(err error)

func (*ClientServerStream) Server

func (s *ClientServerStream) Server() grpc.ServerStream

type ServiceUnwrapper

type ServiceUnwrapper interface {
	UnwrapService() (grpc.ClientConnInterface, grpc.ServiceDesc)
}

type Unwrapper

type Unwrapper interface {
	Unwrap() any
}

Unwrapper defines the Unwrap method for exposing underlying implementation types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL