Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: support for pluggable evaluators #25

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions serve/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"io/fs"

"github.com/google/go-jsonnet"
statuspb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
Expand All @@ -21,20 +20,17 @@ type method struct {
desc protoreflect.MethodDescriptor
filename string
fs fs.FS
makeVM MakeVM
eval Evaluator
}

func newMethod(md protoreflect.MethodDescriptor, fs fs.FS, makeVM MakeVM) method {
func newMethod(md protoreflect.MethodDescriptor, fs fs.FS, eval Evaluator) method {
pkg, svc := md.ParentFile().Package(), md.Parent().Name()
filename := fmt.Sprintf("%s.%s.%s.jsonnet", pkg, svc, md.Name())
if makeVM == nil {
makeVM = jsonnet.MakeVM
}
return method{
desc: md,
filename: filename,
fs: fs,
makeVM: makeVM,
eval: eval,
}
}

Expand Down Expand Up @@ -66,7 +62,7 @@ func (m method) unaryClientCall(ss grpc.ServerStream) error {
return err
}

return m.evalJsonnet(input, ss)
return m.evaluate(input, ss)
}

func (m method) streamingClientCall(ss grpc.ServerStream) error {
Expand All @@ -88,7 +84,7 @@ func (m method) streamingClientCall(ss grpc.ServerStream) error {
return err
}

return m.evalJsonnet(input, ss)
return m.evaluate(input, ss)
}

func (m method) streamingBidiCall(ss grpc.ServerStream) error {
Expand All @@ -108,21 +104,15 @@ func (m method) streamingBidiCall(ss grpc.ServerStream) error {
if err != nil {
return err
}
if err := m.evalJsonnet(input, ss); err != nil {
if err := m.evaluate(input, ss); err != nil {
return err
}
}
return nil
}

func (m method) evalJsonnet(input string, ss grpc.ServerStream) error {
vm := m.makeVM()
vm.TLACode("input", input)
b, err := fs.ReadFile(m.fs, m.filename)
if err != nil {
return err
}
output, err := vm.EvaluateAnonymousSnippet(m.filename, string(b))
func (m method) evaluate(input string, ss grpc.ServerStream) error {
output, err := m.eval(string(m.desc.FullName()), input, m.fs)
if err != nil {
return err
}
Expand Down
26 changes: 23 additions & 3 deletions serve/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,31 @@ func WithLogger(logger Logger) Option {
}

func WithVM(makeVM MakeVM) Option {
return WithEvaluator(JsonnetEvaluator(makeVM))
}

type Evaluator func(method, input string, vfs fs.FS) (output string, err error)

func WithEvaluator(evaluator Evaluator) Option {
return func(s *Server) error {
s.makeVM = makeVM
s.eval = evaluator
return nil
}
}

func JsonnetEvaluator(makeVM MakeVM) Evaluator {
return func(method, input string, vfs fs.FS) (output string, err error) {
vm := makeVM()
vm.TLACode("input", input)
filename := method + ".jsonnet"
b, err := fs.ReadFile(vfs, filename)
if err != nil {
return "", err
}
return vm.EvaluateAnonymousSnippet(filename, string(b))
}
}

type Server struct {
methodDir string
protoSet string
Expand All @@ -57,7 +76,7 @@ type Server struct {
gs *grpc.Server
files *protoregistry.Files
fs fs.FS
makeVM MakeVM
eval Evaluator
}

var errUnknownHandler = errors.New("Unknown handler")
Expand All @@ -69,6 +88,7 @@ func NewServer(methodDir, protoSet string, options ...Option) (*Server, error) {
protoSet: protoSet,
log: NewLogger(os.Stderr, LogLevelError),
}
options = append([]Option{WithVM(jsonnet.MakeVM)}, options...)
for _, opt := range options {
if err := opt(s); err != nil {
return nil, err
Expand Down Expand Up @@ -133,7 +153,7 @@ func (s *Server) loadMethods() error {
for i := 0; i < sds.Len(); i++ {
mds := sds.Get(i).Methods()
for j := 0; j < mds.Len(); j++ {
m := newMethod(mds.Get(j), methodFS, s.makeVM)
m := newMethod(mds.Get(j), methodFS, s.eval)
s.methods[m.fullMethod()] = m
}
}
Expand Down