Skip to content

Commit

Permalink
editorservice: add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Jan 20, 2023
1 parent 6665f50 commit bbb44bd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ The kernel is used to run long running processes like shells and interacting wit

var opts []grpc.ServerOption
server := grpc.NewServer(opts...)
parserv1.RegisterParserServiceServer(server, editorservice.NewParserServiceServer())
parserv1.RegisterParserServiceServer(server, editorservice.NewParserServiceServer(logger))
kernelv1.RegisterKernelServiceServer(server, kernel.NewKernelServiceServer(logger))
return server.Serve(lis)
},
Expand Down
21 changes: 19 additions & 2 deletions internal/document/editor/editorservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ import (

"github.com/stateful/runme/internal/document/editor"
parserv1 "github.com/stateful/runme/internal/gen/proto/go/runme/parser/v1"
"go.uber.org/zap"
"golang.org/x/exp/constraints"
)

type parserServiceServer struct {
parserv1.UnimplementedParserServiceServer

logger *zap.Logger
}

func NewParserServiceServer() parserv1.ParserServiceServer {
return &parserServiceServer{}
func NewParserServiceServer(logger *zap.Logger) parserv1.ParserServiceServer {
return &parserServiceServer{logger: logger}
}

func (s *parserServiceServer) Deserialize(_ context.Context, req *parserv1.DeserializeRequest) (*parserv1.DeserializeResponse, error) {
s.logger.Info("Deserialize", zap.ByteString("source", req.Source[:min(len(req.Source), 64)]))

notebook, err := editor.Deserialize(req.Source)
if err != nil {
s.logger.Info("failed to call Deserialize", zap.Error(err))
return nil, err
}

Expand All @@ -40,6 +47,8 @@ func (s *parserServiceServer) Deserialize(_ context.Context, req *parserv1.Deser
}

func (s *parserServiceServer) Serialize(_ context.Context, req *parserv1.SerializeRequest) (*parserv1.SerializeResponse, error) {
s.logger.Info("Serialize")

cells := make([]*editor.Cell, 0, len(req.Notebook.Cells))
for _, cell := range req.Notebook.Cells {
cells = append(cells, &editor.Cell{
Expand All @@ -54,7 +63,15 @@ func (s *parserServiceServer) Serialize(_ context.Context, req *parserv1.Seriali
Metadata: req.Notebook.Metadata,
})
if err != nil {
s.logger.Info("failed to call Serialize", zap.Error(err))
return nil, err
}
return &parserv1.SerializeResponse{Result: data}, nil
}

func min[T constraints.Ordered](a, b T) T {
if a < b {
return a
}
return b
}
3 changes: 2 additions & 1 deletion internal/document/editor/editorservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
parserv1 "github.com/stateful/runme/internal/gen/proto/go/runme/parser/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
Expand All @@ -18,7 +19,7 @@ import (
func Test_parserServiceServer(t *testing.T) {
lis := bufconn.Listen(2048)
server := grpc.NewServer()
parserv1.RegisterParserServiceServer(server, NewParserServiceServer())
parserv1.RegisterParserServiceServer(server, NewParserServiceServer(zap.NewNop()))
go server.Serve(lis)

conn, err := grpc.Dial(
Expand Down

0 comments on commit bbb44bd

Please sign in to comment.