We are using gRPC with protobuf schema and it would be awesome to mock some of that data so we can test underlying functionality in unit tests. We were gonna just use the parser to build out an object to send to something like Faker.js, but I wanted to see if you guys had already built something like this or had any other clever ideas.
As an example, I have this schema:
message Order {
float total = 1;
repeated Item items = 2;
message Item {
string name = 1;
float price = 2;
}
}
and do something like this:
var testData = protoMocker('/path/to/order.proto');
console.log(testData);
and have it output something like:
{
Order: {
total: 24.4,
items: [{
name: 'banana',
price: 1.2
}, {
name: 'apple',
price: 5.22
}]
}
}
Not asking you to build it or anything, but just trying not to reinvent the wheel and figured this'd be a good place to start since I couldn't find anything on Google, etc! =)
There is pbjs which is capable of processing .proto files to json. So you have two options:
Well, both those options convert it to the JSON object of the structure definition, but it's not "mock data" based off of the schema... does that make sense? Or am _I_ missing something?
And yeah, I can take that JSON structure and convert it into mock data; I'm just seeing if it's been done before.
If you need to populate messages with some dummy data, I suggest taking a look at reflection. Afaik nothing like this has been implemented yet, so your best bet is to inspect the field types and generate the data your own.
Closing this for now.
Feel free to send a pull request if this is still a requirement.
+1
It feels really tedious to generate mock data based on the proto definition, and manually change it when protofile updates
Also it would be nice if we can have a tool to create flow-typed type file that matches the mock data type.
Most helpful comment
+1
It feels really tedious to generate mock data based on the proto definition, and manually change it when protofile updates
Also it would be nice if we can have a tool to create flow-typed type file that matches the mock data type.