bchavez / Bogus

:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Other
8.73k stars 496 forks source link

Include the increment count for the Rules Expressions #545

Closed rsr-maersk closed 5 months ago

rsr-maersk commented 5 months ago

Description

NBuilder can provide the index of the array that is being produced. E.g.

.RuleFor((x, i) => EventTimestamp, f => f.Date.Future(i)))

This means we can do logic resolved value based on the count.

LINQPad Code Example

new Faker().RuleFor((x, i) => EventTimestamp, f => f.Date.Future(i))).Generate(5);

What alternatives have you considered?

I have to iterate over the creation instead of a 1 liner.

Could you help with a pull-request?

Yes

bchavez commented 5 months ago

I think f => f.IndexFaker already exits as an auto-increment mechanism for the index of the item currently being built. Also, there's f.IndexVariable for more complex needs. For example,

void Main()
{
   var f = new Faker<Shipment>()
      .RuleFor(u => u.Id,       f => f.IndexFaker)
      .RuleFor(u => u.AltId,    f => f.IndexVariable += 2)
      .RuleFor(u => u.Receiver, f => f.Name.FirstName());
   f.Generate(10).Dump();
}
class Shipment
{
   public int Id;
   public int AltId;
   public string Receiver;
}

image

Going to close the issue, but feel free to continue the conversation if the example above doesn't suit your need or use case.