Skip to content

Commit

Permalink
Add command to generate fake data
Browse files Browse the repository at this point in the history
  • Loading branch information
sudorandom committed Oct 13, 2024
1 parent 2babb20 commit df2bbb9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
84 changes: 84 additions & 0 deletions cmd/fauxrpc/cmd_generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"fmt"
"os"
"strings"
"time"

"github.com/brianvoe/gofakeit/v7"
"github.com/brianvoe/gofakeit/v7/source"
"github.com/sudorandom/fauxrpc"
"github.com/sudorandom/fauxrpc/private/grpc"
"github.com/sudorandom/fauxrpc/private/registry"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)

type GenerateCmd struct {
Schema []string `required:"" help:"The modules to use for the RPC schema. It can be protobuf descriptors (binpb, json, yaml), a URL for reflection or a directory of descriptors."`
Target string `required:"" help:"Protobuf type" example:"'connectrpc.eliza.v1.IntroduceResponse'"`
Format string `default:"json" enum:"json,proto,grpc" help:"Format to output"`
Seed *uint64 `help:"Seed for random number generator"`
}

func (c *GenerateCmd) Run(globals *Globals) error {
theRegistry, err := registry.NewServiceRegistry()
if err != nil {
return err
}
for _, schema := range c.Schema {
if err := registry.AddServicesFromPath(theRegistry, schema); err != nil {
if strings.Contains(err.Error(), "name conflict") {
continue
}
return err
}
}

desc, err := theRegistry.Files().FindDescriptorByName(protoreflect.FullName(c.Target))
if err != nil {
return err
}
md, ok := desc.(protoreflect.MessageDescriptor)
if !ok {
return fmt.Errorf("unexpected type: %T", desc)
}

seed := uint64(0)
if c.Seed == nil {
seed = uint64(time.Now().UnixNano())
} else {
seed = *c.Seed
}
fakeSrc := source.NewJSF(seed)
msg := fauxrpc.NewMessage(md, fauxrpc.GenOptions{
Faker: gofakeit.NewFaker(fakeSrc, true),
})

switch c.Format {
case "json":
jsonBytes, err := protojson.Marshal(msg)
if err != nil {
return err
}
os.Stdout.Write(jsonBytes)
case "proto":
protoBytes, err := proto.Marshal(msg)
if err != nil {
return err
}
os.Stdout.Write(protoBytes)
case "grpc":
protoBytes, err := proto.Marshal(msg)
if err != nil {
return err
}
grpc.WriteGRPCMessage(os.Stdout, protoBytes)
default:
return fmt.Errorf("unexpected format: %s", c.Format)
}

return nil
}
1 change: 1 addition & 0 deletions cmd/fauxrpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type CLI struct {

Run RunCmd `cmd:"" help:"Run the FauxRPC server"`
Stub StubCmd `cmd:"" help:"Contains stub commands"`
Generate GenerateCmd `cmd:"generate" help:"Generate fake data"`
Registry RegistryCmd `cmd:"" help:"Contains registry commands"`
}

Expand Down
4 changes: 2 additions & 2 deletions private/server/grpc.go → private/grpc/encoding.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package server
package grpc

import (
"encoding/binary"
"io"
)

func writeGRPCMessage(w io.Writer, msg []byte) {
func WriteGRPCMessage(w io.Writer, msg []byte) {
prefix := make([]byte, 5)
binary.BigEndian.PutUint32(prefix[1:], uint32(len(msg)))
_, _ = w.Write(prefix)
Expand Down
1 change: 1 addition & 0 deletions private/registry/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func AddServicesFromBSR(registry ServiceRegistry, module string) error {
// AddServicesFromGlobal adds the 'well known' types to the registry. This is typically implicitly called.
func AddServicesFromGlobal(registry ServiceRegistry) error {
for _, fd := range []protoreflect.FileDescriptor{
descriptorpb.File_google_protobuf_descriptor_proto,
anypb.File_google_protobuf_any_proto,
apipb.File_google_protobuf_api_proto,
durationpb.File_google_protobuf_duration_proto,
Expand Down
3 changes: 2 additions & 1 deletion private/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"connectrpc.com/connect"
"github.com/sudorandom/fauxrpc"
"github.com/sudorandom/fauxrpc/private/grpc"
"github.com/sudorandom/fauxrpc/private/stubs"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -57,7 +58,7 @@ func NewHandler(service protoreflect.ServiceDescriptor, db stubs.StubDatabase) h
w.Header().Set("Grpc-Message", err.Error())
return
}
writeGRPCMessage(w, b)
grpc.WriteGRPCMessage(w, b)
w.Header().Set("Grpc-Status", "0")
w.Header().Set("Grpc-Message", "")
})
Expand Down

0 comments on commit df2bbb9

Please sign in to comment.