github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/starlib/net/starlarkgrpc/grpc.go (about)

     1  package starlarkgrpc
     2  
     3  // TODO: create a gRPC client based off of larking.Mux.
     4  
     5  /*import (
     6  	"context"
     7  	"io"
     8  	"strings"
     9  	"time"
    10  
    11  	"go.starlark.net/starlark"
    12  	"github.com/emcfarlane/larking/starlib/starlarkstruct"
    13  	"github.com/emcfarlane/larking/starlib/starlarkthread"
    14  	"github.com/emcfarlane/larking/starlib/starlarkproto"
    15  	"google.golang.org/grpc"
    16  	"google.golang.org/grpc/codes"
    17  	"google.golang.org/grpc/metadata"
    18  	"google.golang.org/grpc/status"
    19  	"google.golang.org/protobuf/proto"
    20  )
    21  
    22  // Bootstrap...
    23  // TODO: xDS
    24  
    25  func NewModule(mux *Mux) *starlarkstruct.Module {
    26  	s := NewStarlark(mux)
    27  	return &starlarkstruct.Module{
    28  		Name: "grpc",
    29  		Members: starlark.StringDict{
    30  			"dial":    starlark.NewBuiltin("grpc.dial", s.Dial),
    31  		},
    32  	}
    33  }
    34  
    35  type Starlark struct {
    36  	mux *Mux
    37  }
    38  
    39  func NewStarlark(mux *Mux) *Starlark {
    40  	return &Starlark{mux: mux}
    41  }
    42  
    43  // Dial accepts an address and optional
    44  func (s *Starlark) Dial(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    45  	var target string
    46  	if err := starlark.UnpackPositionalArgs("grpc.dial", args, kwargs, 1, &target); err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	ctx := starlarkthread.Context(thread)
    51  
    52  	// TODO: handle security, opts.
    53  	dialCtx, cancel := context.WithTimeout(ctx, time.Second*5)
    54  	defer cancel()
    55  	cc, err := grpc.DialContext(dialCtx, target, grpc.WithInsecure())
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	if err := s.mux.RegisterConn(ctx, cc); err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	// TODO?
    65  	return starlark.None, nil
    66  }*/