-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2babb20
commit df2bbb9
Showing
5 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters