Giter VIP home page Giter VIP logo

Comments (2)

bchavez avatar bchavez commented on May 17, 2024

You have a Generate(1) that is randomly called call within a rule controlled y.Random.Bool() ? clients.Generate(1).

This is what causes the UniqueIndex to appear to skip values from the outer scope Generate(4). So, the first of Generate(4), may or may not call Generate(1) for a client. If it does call Generate(1) for a client it will cause the UniqueIndex to increment again.

UniqueIndex is not intended to be used or referenced as sequential incrementing value, but more as a globally unique integer that spans the entirety of an app domain and faker instance creations.

If you want to control your own counter (or in this case move sequential across the array of values), you'll have to define and increment your own counter.

Thanks,
Brian

🍂 🍃 Distance - Falling (ft Alys Be)

from bogus.

mdmoura avatar mdmoura commented on May 17, 2024

Hi,

Wouldn't be possible to add to Bogus an, for example, UniqueLocalIndex (or any other name) that would be incremented only in each generation block?

Or maybe a better way to integrate user created indexes in Fluent code of Bogus? Something like:

   int index = 0;
   var users = 
      new Faker<User>()
        .UseIndex(index, 0) // 0 is the initial value. Could be another one.
        .RuleFor(x => x.Name, y => new String[] { "John", "Mary", "Mike", "Tom" }[index % 4])
        .RuleFor(x => x.Email, (y, x) => { return $"{x.Name}@xyz.com".Replace(" ", "").ToLower(); })
        .RuleFor(x => x.Client, y => y.Random.Bool() ? clients.Generate(1).First() : null)
        .RuleFor(x => x.UserName, (y, x) => x.Email)
        .UpdateIndex(index++) // index++ or index = index + 2, etc. index is updated in each cycle. 
        .Generate(4).ToList());

Using Bogus for quite sometime having a local index well integrated is really helpful.
This is very useful when generating data in more complex database models.

Thank You,
Miguel

from bogus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.