Documentation ¶
Overview ¶
Package protogofakeit provides a utility for producing fake data into Protocol Buffer messages.
Example ¶
package main import ( "bytes" "encoding/json" "fmt" "github.com/brianvoe/gofakeit/v6" "github.com/rodaine/protogofakeit" "github.com/rodaine/protogofakeit/gen/gofakeit/example" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/proto" ) func main() { faker := gofakeit.New(3) protoFaker := protogofakeit.New(faker) user := &example.User{} _ = protoFaker.FakeProto(user) fmt.Println(toJSON(user)) } func toJSON(msg proto.Message) string { data, _ := protojson.Marshal(msg) buf := &bytes.Buffer{} _ = json.Indent(buf, data, "", " ") return buf.String() }
Output: { "userId": "15864040051628833669", "firstName": "Philip", "lastName": "Casper", "email": "tyreekstroman@mitchell.biz", "location": { "latitude": 1.383491, "longitude": -62.968324 }, "hobbies": [ "Curling", "Slot car", "Fishkeeping" ], "pets": { "Archie": "PET_TYPE_DOG" } }
Index ¶
- type Option
- func WithBytesSize(min, max int) Option
- func WithListSize(min, max int) Option
- func WithMapSize(min, max int) Option
- func WithMaxDepth(depth int) Option
- func WithStringSize(min, max int) Option
- func WithTemplateOptions(opts *gofakeit.TemplateOptions) Option
- func WithTimestampFormat(format string) Option
- type ProtoFaker
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
An Option modifies the default behavior of a ProtoFaker, configured via New.
func WithBytesSize ¶
WithBytesSize sets the default minimum and maximum lengths (inclusive) of random bytes produced. Individual lengths can be configured on a per-field basis within the protobuf definition. The default range is 4-10.
func WithListSize ¶
WithListSize sets the default minimum and maximum lengths (inclusive) of random repeated fields produced. Individual lengths can be configured on a per-field basis within the protobuf definition. The default range is 4-10.
func WithMapSize ¶
WithMapSize sets the default minimum and maximum lengths (inclusive) of random map fields produced. Individual lengths can be configured on a per-field basis within the protobuf definition. The default range is 4-10.
func WithMaxDepth ¶
WithMaxDepth limits the recursion depth of message fields populated. Beyond this depth, message fields are not set. The default value is 5.
func WithStringSize ¶
WithStringSize sets the default minimum and maximum lengths (inclusive) of random strings produced. Individual lengths can be configured on a per-field basis within the protobuf definition. The default range is 4-10.
func WithTemplateOptions ¶
func WithTemplateOptions(opts *gofakeit.TemplateOptions) Option
WithTemplateOptions allows specifying any custom data and functions that should be available to fields constructed via templates. The default is unset.
func WithTimestampFormat ¶
WithTimestampFormat sets the format used to parse timestamps from tag or template results. The format uses the same structure as time.Layout. The default format is time.RFC3339Nano.
type ProtoFaker ¶
type ProtoFaker interface { // FakeProto populates msg with fake data, optionally configured through // annotations on the protobuf message. An error is returned if the // configuration on msg is invalid (typically a parse error). FakeProto(msg proto.Message) error }
ProtoFaker populates a protobuf message with fake data.
func New ¶
func New(faker *gofakeit.Faker, options ...Option) ProtoFaker
New creates a ProtoFaker from the given gofakeit.Faker and Option values. If the returned value is intended to be used in a concurrent context, the provided faker should also be configured for concurrent use.