Skip to content

Commit

Permalink
chore: switch to the opaque protobuf API
Browse files Browse the repository at this point in the history
  • Loading branch information
sudorandom committed Dec 28, 2024
1 parent 2dbbf07 commit 47c1786
Show file tree
Hide file tree
Showing 24 changed files with 11,381 additions and 1,953 deletions.
4 changes: 2 additions & 2 deletions cmd/fauxrpc/cmd_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func (c *RegistryAddCmd) Run(globals *Globals) error {
filespb = append(filespb, protodesc.ToFileDescriptorProto(fd))
})
client := newRegistryClient(c.Addr)
if _, err := client.AddDescriptors(context.Background(), connect.NewRequest(&registryv1.AddDescriptorsRequest{
if _, err := client.AddDescriptors(context.Background(), connect.NewRequest(registryv1.AddDescriptorsRequest_builder{
Descriptors: &descriptorpb.FileDescriptorSet{
File: filespb,
},
})); err != nil {
}.Build())); err != nil {
return err
}

Expand Down
17 changes: 9 additions & 8 deletions cmd/fauxrpc/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/sudorandom/fauxrpc/private/server"
"github.com/sudorandom/fauxrpc/private/stubs"
stubsv1 "github.com/sudorandom/fauxrpc/proto/gen/stubs/v1"
"google.golang.org/protobuf/proto"
)

type RunCmd struct {
Expand Down Expand Up @@ -145,16 +146,16 @@ func (f StubFile) ToRequest() (*stubsv1.AddStubsRequest, error) {
}
contentsJSON = string(b)
}
stubs[i] = &stubsv1.Stub{
Ref: &stubsv1.StubRef{Id: stub.ID, Target: stub.Target},
Content: &stubsv1.Stub_Json{Json: contentsJSON},
CelContent: stub.CelContent,
ActiveIf: stub.ActiveIf,
Priority: stub.Priority,
}
stubs[i] = stubsv1.Stub_builder{
Ref: stubsv1.StubRef_builder{Id: proto.String(stub.ID), Target: proto.String(stub.Target)}.Build(),
Json: proto.String(contentsJSON),
CelContent: proto.String(stub.CelContent),
ActiveIf: proto.String(stub.ActiveIf),
Priority: proto.Int32(stub.Priority),
}.Build()
}

return &stubsv1.AddStubsRequest{Stubs: stubs}, nil
return stubsv1.AddStubsRequest_builder{Stubs: stubs}.Build(), nil
}

type StubFileEntry struct {
Expand Down
82 changes: 41 additions & 41 deletions cmd/fauxrpc/cmd_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
stubsv1 "github.com/sudorandom/fauxrpc/proto/gen/stubs/v1"
"github.com/sudorandom/fauxrpc/proto/gen/stubs/v1/stubsv1connect"
"golang.org/x/net/http2"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)

Expand All @@ -41,29 +42,28 @@ type StubAddCmd struct {

func (c *StubAddCmd) Run(globals *Globals) error {
client := newStubClient(c.Addr)
stub := &stubsv1.Stub{
Ref: &stubsv1.StubRef{
Id: c.ID,
Target: c.Target,
},
ActiveIf: c.ActiveIf,
Priority: c.Priority,
CelContent: c.CEL,
}
stub := stubsv1.Stub_builder{
Ref: stubsv1.StubRef_builder{
Id: proto.String(c.ID),
Target: proto.String(c.Target),
}.Build(),
ActiveIf: proto.String(c.ActiveIf),
Priority: proto.Int32(c.Priority),
CelContent: proto.String(c.CEL),
}.Build()
if c.JSON != "" {
stub.Content = &stubsv1.Stub_Json{Json: c.JSON}
stub.SetJson(c.JSON)
} else if c.ErrorCode != nil {
stub.Content = &stubsv1.Stub_Error{
Error: &stubsv1.Error{
Code: stubsv1.ErrorCode(*c.ErrorCode),
Message: c.ErrorMessage,
Details: []*anypb.Any{},
},
}
code := stubsv1.ErrorCode(*c.ErrorCode)
stub.SetError(stubsv1.Error_builder{
Code: &code,
Message: proto.String(c.ErrorMessage),
Details: []*anypb.Any{},
}.Build())
} else {
return errors.New("one of: --error-code or --json is required.")
}
resp, err := client.AddStubs(context.Background(), connect.NewRequest(&stubsv1.AddStubsRequest{Stubs: []*stubsv1.Stub{stub}}))
resp, err := client.AddStubs(context.Background(), connect.NewRequest(stubsv1.AddStubsRequest_builder{Stubs: []*stubsv1.Stub{stub}}.Build()))
if err != nil {
return err
}
Expand All @@ -82,7 +82,7 @@ func (c *StubListCmd) Run(globals *Globals) error {
return err
}
groupedStubs := map[string][]string{}
for _, stub := range resp.Msg.Stubs {
for _, stub := range resp.Msg.GetStubs() {
ref := stub.GetRef()
name := ref.GetTarget()
groupedStubs[name] = append(groupedStubs[name], ref.GetId())
Expand All @@ -107,12 +107,12 @@ type StubGetCmd struct {

func (c *StubGetCmd) Run(globals *Globals) error {
client := newStubClient(c.Addr)
resp, err := client.ListStubs(context.Background(), connect.NewRequest(&stubsv1.ListStubsRequest{
StubRef: &stubsv1.StubRef{
Id: c.ID,
Target: c.Target,
},
}))
resp, err := client.ListStubs(context.Background(), connect.NewRequest(stubsv1.ListStubsRequest_builder{
StubRef: stubsv1.StubRef_builder{
Id: proto.String(c.ID),
Target: proto.String(c.Target),
}.Build(),
}.Build()))
if err != nil {
return err
}
Expand All @@ -128,14 +128,14 @@ type StubRemoveCmd struct {

func (c *StubRemoveCmd) Run(globals *Globals) error {
client := newStubClient(c.Addr)
_, err := client.RemoveStubs(context.Background(), connect.NewRequest(&stubsv1.RemoveStubsRequest{
_, err := client.RemoveStubs(context.Background(), connect.NewRequest(stubsv1.RemoveStubsRequest_builder{
StubRefs: []*stubsv1.StubRef{
{
Id: c.ID,
Target: c.Target,
},
stubsv1.StubRef_builder{
Id: proto.String(c.ID),
Target: proto.String(c.Target),
}.Build(),
},
}))
}.Build()))
if err != nil {
return err
}
Expand Down Expand Up @@ -171,23 +171,23 @@ func outputStubs(stubs []*stubsv1.Stub) {
})
for _, stub := range stubs {
outputStub := StubForOutput{
Ref: stub.Ref,
ActiveIf: stub.ActiveIf,
Priority: stub.Priority,
CelContent: stub.CelContent,
Ref: stub.GetRef(),
ActiveIf: stub.GetActiveIf(),
Priority: stub.GetPriority(),
CelContent: stub.GetCelContent(),
}

switch t := stub.GetContent().(type) {
case *stubsv1.Stub_Json:
switch stub.WhichContent() {
case stubsv1.Stub_Json_case:
var v any
if err := json.Unmarshal([]byte(t.Json), &v); err != nil {
if err := json.Unmarshal([]byte(stub.GetJson()), &v); err != nil {
slog.Error("error marshalling for output", slog.Any("error", err))
continue
}
outputStub.Content = v
case *stubsv1.Stub_Error:
outputStub.ErrorCode = int(t.Error.GetCode())
outputStub.ErrorMessage = t.Error.GetMessage()
case stubsv1.Stub_Error_case:
outputStub.ErrorCode = int(stub.GetError().GetCode())
outputStub.ErrorMessage = stub.GetError().GetMessage()
}
b, err := json.MarshalIndent(outputStub, "", " ")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
golang.org/x/net v0.31.0
golang.org/x/sync v0.10.0
google.golang.org/grpc v1.68.0
google.golang.org/protobuf v1.35.2
google.golang.org/protobuf v1.36.1
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,8 @@ google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
2 changes: 1 addition & 1 deletion private/registry/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewHandler(registry ServiceRegistry) *handler {

// AddDescriptors implements registryv1connect.RegistryServiceHandler.
func (h *handler) AddDescriptors(ctx context.Context, req *connect.Request[registryv1.AddDescriptorsRequest]) (*connect.Response[registryv1.AddDescriptorsResponse], error) {
files, err := protodesc.NewFiles(req.Msg.Descriptors)
files, err := protodesc.NewFiles(req.Msg.GetDescriptors())
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions private/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ func grpcWriteStatus(w http.ResponseWriter, st *status.Status) {
}

func grpcStatusFromError(e *stubsv1.Error) *status.Status {
status := status.New(codes.Code(e.Code), e.GetMessage())
if len(e.Details) > 0 {
details := make([]protoiface.MessageV1, len(e.Details))
for i, detail := range e.Details {
status := status.New(codes.Code(e.GetCode()), e.GetMessage())
if len(e.GetDetails()) > 0 {
details := make([]protoiface.MessageV1, len(e.GetDetails()))
for i, detail := range e.GetDetails() {
details[i] = detail
}
s, err := status.WithDetails(details...)
Expand Down
63 changes: 31 additions & 32 deletions private/stubs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ func NewHandler(registry registry.ServiceRegistry, stubdb StubDatabase) *handler

// AddStubs implements stubsv1connect.StubsServiceHandler.
func (h *handler) AddStubs(ctx context.Context, req *connect.Request[stubsv1.AddStubsRequest]) (*connect.Response[stubsv1.AddStubsResponse], error) {
entries := make([]StubEntry, len(req.Msg.Stubs))
stubs := make([]*stubsv1.Stub, len(req.Msg.Stubs))
for i, stub := range req.Msg.Stubs {
if stub.Ref == nil {
stub.Ref = &stubsv1.StubRef{}
entries := make([]StubEntry, len(req.Msg.GetStubs()))
stubs := make([]*stubsv1.Stub, len(req.Msg.GetStubs()))
for i, stub := range req.Msg.GetStubs() {
if !stub.HasRef() {
stub.SetRef(&stubsv1.StubRef{})
}
if stub.GetRef().Id == "" {
stub.Ref.Id = gofakeit.AdjectiveDescriptive() + "-" + strings.ReplaceAll(gofakeit.Animal(), " ", "-") + gofakeit.DigitN(3)
if stub.GetRef().GetId() == "" {
stub.GetRef().SetId(gofakeit.AdjectiveDescriptive() + "-" + strings.ReplaceAll(gofakeit.Animal(), " ", "-") + gofakeit.DigitN(3))
}

ref := stub.GetRef()
Expand All @@ -59,8 +59,8 @@ func (h *handler) AddStubs(ctx context.Context, req *connect.Request[stubsv1.Add
var md protoreflect.MessageDescriptor
switch t := desc.(type) {
case protoreflect.MethodDescriptor:
if len(stub.ActiveIf) > 0 {
r, err := NewActiveIf(t, stub.ActiveIf)
if len(stub.GetActiveIf()) > 0 {
r, err := NewActiveIf(t, stub.GetActiveIf())
if err != nil {
return nil, err
}
Expand All @@ -77,30 +77,30 @@ func (h *handler) AddStubs(ctx context.Context, req *connect.Request[stubsv1.Add
return nil, fmt.Errorf("not valid for %T", desc)
}

ref.Target = string(md.FullName())
ref.SetTarget(string(md.FullName()))
entry.Key = StubKey{ID: stub.GetRef().GetId(), Name: name}

switch t := stub.GetContent().(type) {
case *stubsv1.Stub_Json:
if t.Json != "" {
switch stub.WhichContent() {
case stubsv1.Stub_Json_case:
if stub.GetJson() != "" {
msg := registry.NewMessage(md).Interface()
if err := protojson.Unmarshal([]byte(t.Json), msg); err != nil {
if err := protojson.Unmarshal([]byte(stub.GetJson()), msg); err != nil {
return nil, err
}
entry.Message = msg
}
case *stubsv1.Stub_Proto:
case stubsv1.Stub_Proto_case:
msg := registry.NewMessage(md).Interface()
if err := proto.Unmarshal(t.Proto, msg); err != nil {
if err := proto.Unmarshal(stub.GetProto(), msg); err != nil {
return nil, err
}
entry.Message = msg
case *stubsv1.Stub_Error:
entry.Error = &StatusError{StubsError: t.Error}
case stubsv1.Stub_Error_case:
entry.Error = &StatusError{StubsError: stub.GetError()}
}

if stub.CelContent != "" {
celmsg, err := protocel.New(h.registry.Files(), md, stub.CelContent)
if stub.GetCelContent() != "" {
celmsg, err := protocel.New(h.registry.Files(), md, stub.GetCelContent())
if err != nil {
return nil, err
}
Expand All @@ -115,7 +115,7 @@ func (h *handler) AddStubs(ctx context.Context, req *connect.Request[stubsv1.Add
h.stubdb.AddStub(entry)
}

return connect.NewResponse(&stubsv1.AddStubsResponse{Stubs: stubs}), nil
return connect.NewResponse(stubsv1.AddStubsResponse_builder{Stubs: stubs}.Build()), nil
}

// ListStubs implements stubsv1connect.StubsServiceHandler.
Expand All @@ -140,7 +140,7 @@ func (h *handler) ListStubs(ctx context.Context, req *connect.Request[stubsv1.Li
if err != nil {
return nil, err
}
return connect.NewResponse(&stubsv1.ListStubsResponse{Stubs: pbstubs}), nil
return connect.NewResponse(stubsv1.ListStubsResponse_builder{Stubs: pbstubs}.Build()), nil

}

Expand Down Expand Up @@ -168,24 +168,23 @@ func (h *handler) RemoveStubs(ctx context.Context, msg *connect.Request[stubsv1.
func stubsToProto(stubs []StubEntry) ([]*stubsv1.Stub, error) {
pbStubs := []*stubsv1.Stub{}
for _, stub := range stubs {
pbStub := &stubsv1.Stub{
Ref: &stubsv1.StubRef{
Id: stub.Key.ID,
Target: string(stub.Key.Name),
},
}
pbStub := &stubsv1.Stub{}
pbStub.SetRef(stubsv1.StubRef_builder{
Id: proto.String(stub.Key.ID),
Target: proto.String(string(stub.Key.Name)),
}.Build())
if stub.ActiveIf != nil {
pbStub.ActiveIf = stub.ActiveIf.GetString()
pbStub.SetActiveIf(stub.ActiveIf.GetString())
}
if stub.Error != nil {
pbStub.Content = &stubsv1.Stub_Error{Error: stub.Error.StubsError}
pbStub.SetError(stub.Error.StubsError)
}
if stub.Message != nil {
content, err := protojson.Marshal(stub.Message)
if err != nil {
return nil, err
}
pbStub.Content = &stubsv1.Stub_Json{Json: string(content)}
pbStub.SetJson(string(content))
}
pbStubs = append(pbStubs, pbStub)
}
Expand All @@ -211,5 +210,5 @@ type StatusError struct {
}

func (s *StatusError) Error() string {
return s.Message
return s.GetMessage()
}
1 change: 1 addition & 0 deletions proto/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ managed:
value: github.com/sudorandom/fauxrpc/proto/gen
disable:
- module: buf.build/bufbuild/protovalidate
- module: buf.build/protocolbuffers/wellknowntypes
plugins:
- remote: buf.build/protocolbuffers/go
out: gen
Expand Down
7 changes: 5 additions & 2 deletions proto/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
version: v2
deps:
- name: buf.build/bufbuild/protovalidate
commit: a6c49f84cc0f4e038680d390392e2ab0
digest: b5:e968392e88ff7915adcbd1635d670b45bff8836ec2415d81fc559ca5470a695dbdc30030bad8bc5764647c731079e9e7bba0023ea25c4e4a1672a7d2561d4a19
commit: a3320276596649bcad929ac829d451f4
digest: b5:285a6d3a423b195a21f45aacc97ee222ac09cfb01a42f0d546aa51d92177b0b9d00eb9ae93e72dabbbefdc77f35a4c7a11f15d913cc08da764fcb6071f85d148
- name: buf.build/protocolbuffers/wellknowntypes
commit: d4f14e5e0a9c40889c90d373c74e95eb
digest: b5:39b4d0887abcd8ee1594086283f4120f688e1c33ec9ccd554ab0362ad9ad482154d0e07e3787d394bb22970930b452aac1c5c105c05efe129cec299ff5b5e05e
1 change: 1 addition & 0 deletions proto/buf.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: v2
deps:
- buf.build/bufbuild/protovalidate
- buf.build/protocolbuffers/wellknowntypes:v29.2
Loading

0 comments on commit 47c1786

Please sign in to comment.