Documentation
¶
Overview ¶
Package fakeproto fills protobuf messages with random fake data.
It uses github.com/brianvoe/gofakeit/v7 as the data-generation engine and walks the protobuf message descriptor via protoreflect, which means it correctly handles oneof, map, repeated, proto3 optional presence, enum values from the declared set, and well-known types.
Why a separate module? ¶
Protobuf support lives here, not in gofakeit, to preserve gofakeit's zero-dependency guarantee. Users who don't need protobuf don't pay for it.
Basic usage ¶
var msg mypb.MyMessage
if err := fakeproto.Message(&msg); err != nil {
log.Fatal(err)
}
Use WithFaker to supply a seeded gofakeit.Faker for deterministic output:
f := gofakeit.New(42) // fixed seed
var msg mypb.MyMessage
if err := fakeproto.Message(&msg, fakeproto.WithFaker(f)); err != nil {
log.Fatal(err)
}
Use Enum to fill a single enum variable with a random declared value:
var s mypb.Status fakeproto.Enum(&s)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Enum ¶
Enum sets *e to a random value chosen from the enum's declared set. Reserved numbers are never produced.
var p pb.Priority fakeproto.Enum(&p)
Example ¶
package main
import (
"fmt"
"github.com/brianvoe/gofakeit/v7"
"github.com/psyhatter/fakeproto"
testpb "github.com/psyhatter/fakeproto/internal/test/gen"
)
func main() {
var p testpb.Priority
fakeproto.Enum(&p, fakeproto.WithFaker(gofakeit.New(42)))
fmt.Println(p)
}
Output: PRIORITY_MEDIUM
func Message ¶
Message fills msg with random fake data.
Use WithFaker to supply a seeded gofakeit.Faker for deterministic output:
f := gofakeit.New(42) err := fakeproto.Message(msg, fakeproto.WithFaker(f))
Example ¶
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/brianvoe/gofakeit/v7"
"google.golang.org/protobuf/encoding/protojson"
"github.com/psyhatter/fakeproto"
testpb "github.com/psyhatter/fakeproto/internal/test/gen"
)
func main() {
user := &testpb.User{}
// WithFaker sets a fixed seed so the output is always identical.
if err := fakeproto.Message(user, fakeproto.WithFaker(gofakeit.New(657))); err != nil {
panic(err) // TODO: Handle error.
}
// protojson.Marshal works better with timestamppb.Timestamp than json.Marshal.
b, err := protojson.Marshal(user)
if err != nil {
panic(err) // TODO: Handle error.
}
// To make the output prettier.
buf := bytes.NewBuffer(nil)
err = json.Indent(buf, b, "", " ")
if err != nil {
panic(err) // TODO: Handle error.
}
fmt.Println(buf.String())
}
Output: { "id": "z3f7cb4zc8kn4ukbk56x", "email": "cliffordking@clark.com", "name": "Aleen Schroeder", "status": "STATUS_ACTIVE", "createdAt": "2006-07-23T05:03:41.200315279Z", "age": 29, "tags": [ "not", "which", "example" ] }
Types ¶
type Option ¶
type Option func(*config)
Option configures the behavior of Message and Enum.
func WithFaker ¶
WithFaker sets the gofakeit.Faker instance used for random data generation. A seeded faker (gofakeit.New) produces deterministic output, which is useful for snapshot tests and godoc examples. Defaults to gofakeit.GlobalFaker.
func WithMaxDepth ¶
WithMaxDepth sets the maximum JSON-container nesting depth of the generated output. Each message, array, and map object counts as one container level. Containers that would exceed d are left empty or nil. Default is 5.
func WithMaxRepeated ¶
WithMaxRepeated sets the maximum length of generated repeated fields and map entries. The actual length is chosen randomly in [1, n]. Default is 5.
func WithOptionalProbability ¶
WithOptionalProbability sets the probability p in [0, 1] that an explicit proto3 optional scalar field is left unset. p=0 means always set; p=1 means always unset. Default is 0.10.
func WithSkipOneof ¶
func WithSkipOneof() Option
WithSkipOneof instructs the generator to leave all oneof fields unset.
type ProtoEnum ¶
type ProtoEnum interface {
protoreflect.Enum
~int32
}
ProtoEnum is satisfied by every protobuf-generated enum type. All such types have int32 as their underlying type.