protorand

package module
v0.0.0-...-9ca69d8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2022 License: MIT Imports: 7 Imported by: 0

README

protorand

GoDoc

protorand generates a protobuf message struct with random values from any type implementing the proto.Message interface. proto.Message is implemented by all structs generated by the default complier for Protocol Buffers. protorand also recursibely embeds values to struct fields in a struct and supports Map, Slice (repeated in Protobuf) and OneOf.

What is this for?

You can generate proto messages filled with random values. You can use it for some testing purposes like random value test or performance test.

Install

$ go get -u github.com/sryoya/protorand

Example

pr := protorand.New()
pb := &testpb.TestMessage{} // some protobuf message struct
res, err := pr.Gen(pb) 
if err != nil {
    // some error handling
}
fmt.Println(res)
// now it prints the protobuf message struct with random values like this:
// some_str:"EtNkGoBvGg"  some_int32:895194746  some_sint32:1378122731  some_uint32:2018831629  some_fixed32:3152315036  some_sfixed32:1347258978  some_int64:6534493099247216387  some_sint64:724134999722101364  some_uint64:9797562765629663753  some_fixed64:9494449334166659795  some_sfixed64:2365978907336111062  some_float32:0.31029108  some_float64:0.07361103  some_slice:"qgWT3LG6Nh"  some_msg:{some_int:1226009465}  some_msgs:{some_int:276429239}  some_map:{key:1431285010  value:{some_int:387368828}}  some_enum:SOME_ENUM_VALUE_1  one_of_int32:1768554790

FAQ

Q. Does this package allow me to set fixed values or use specific rules for certain fields?

A. No, at least now. We can operate proto message structs with the normal way like msg.FieldA = "some value" . When we have specific rules for some fields, we can just change the values in them, overwriting the values that protorand generates. protorand aims to provide an easy and common way to help generating the random protobuf message struct.

To Be Developped

This package still has some points to update. The unimplemented features are follwing:

  • Handling for known types like TimeStamp
  • Randomize the number of elements in List and Map (Currently, it always sets just one element)
  • Optional field

Contribution

You are more than welcome to contribute to this project. Fork and make a Pull Request, or create an Issue if you see any problem.

Licence

MIT License

Documentation

Overview

Package protorand provides a random number generator for the protobuf message struct. You can simply generate the protobuf message struct with random values.

Index

Constants

View Source
const (
	// MaxDepth is the maximum stack depth for recursion, which prevents stack overflows if a message is (directly or
	// transitively) self-referential.
	MaxDepth = 16
)

Variables

View Source
var (
	// Chars is the set of characters used to generate random strings.
	Chars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
)

Functions

This section is empty.

Types

type ProtoRand

type ProtoRand struct {
	// contains filtered or unexported fields
}

ProtoRand is a source of random values for protobuf fields.

func New

func New() *ProtoRand

New creates a new ProtoRand.

func (*ProtoRand) Gen

func (p *ProtoRand) Gen(in proto.Message) (proto.Message, error)

Gen generates a new proto.Message having randoms value in its fields. The input is used to specify the type of the generated message. The input itself is immutable.

func (*ProtoRand) NewDynamicProtoRand

func (p *ProtoRand) NewDynamicProtoRand(mds protoreflect.MessageDescriptor) (*dynamicpb.Message, error)

NewDynamicProtoRand creates dynamicpb with assigning random values to a proto.

func (*ProtoRand) Seed

func (p *ProtoRand) Seed(seed int64)

Seed sets the seed of the random generator.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL