-
-
Notifications
You must be signed in to change notification settings - Fork 478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FinishWith for the whole generate set #55
Comments
Hi @levipage Thanks for the suggestion. Your suggestion makes sense, but I'll need some time to think this one through. I'm slightly concerned with the API code smell/inconsistency and semantics. The API code smell relates to Need to think about this one a bit more. If you have any other ideas on better API sematnics, let me know. 💨 🚶 "Bubbles of gas in my brain... Send me off balance, it's not enough" |
I see your dilemma. I spent awhile thinking about this. Ultimately I think I actually like coupling the collection behavior to the rule because you in a way it's already coupled. I am already using this single rule to generate a list so it would only make sense that for it to be a complete rule I need to be able to determine the behavior of how that list gets generated. Another alternative would be to apply the collection finish rule to both the list and the single item. It would be like applying the rule to a single item list (you already alluded to this in your response). I don't really like this idea though, because it feels ambiguous and if I was using a a properly named method that denoted a collection specific rule, I would expect it doesn't run. I think as a dev if I am trying to enforce a group rule on both single items and multiple items, I would simply call the overload using |
I absolutely agree, having to specify the My thinking on However, I do see your motivation for including a "first item rule" in What if we did something like this:
Would this work for you? I have a local branch where I've been working on some refactorings to allow better index/count usages ever since issue #48 came up. Perhaps this might be a viable solution and best of both worlds. Let me know, |
This would work for my one use case. But, I think it would be very limiting otherwise. You may want to post sort, etc., etc. I don't think my suggestion breaks the pattern at all. It's part of the rule... When you return more than one item (or a list)... then do this... You just need a way to define that. You don't have a concept of a list rule so I don't see any other place to put it other than in some other FinishWith. I think it's implied than when you aren't returning a list, the list rule wouldn't run. Not sure the best solution beyond what I proposed. Maybe FinishAll()... expose a list here. Let me do what I want with the items. Maybe there will be only one (as when using regular Generate()). Maybe there will be more than one. If I only return one item via Generate()... apply the rule anyway and just return the one item after applying the rule as if it were a list. Hopefully that makes sense. |
Hi @levipage, Thinking about this a bit more. Would you consider this a viable solution?
This is where my gut feeling is on this. I really want to avoid adding an extra API call just for post generation contexts. Please let me know, |
@levipage still need this one? |
The issue came up again on Twitter. https://twitter.com/hackmum/status/1080206253073354752 Still need to mentally chew on this problem a little bit more. |
With the introduction of C# 8 features; specifically, Indicies and Ranges, there might finally be an elegant way to "set the first element" after generating a collection with some API fluency. Off the top of my mind, something like this would be cool: var rules = new Faker<Issue55Object>()
.RuleFor(o => o.Code, f => f.Random.AlphaNumeric(5))
.RuleFor(o => o.Dba, f => f.Company.CompanyName())
.Set(^1, (f, t) =>
{
// do something with the last item of the generation
})
.Set(0..2, (f, t[]) =>
{
// do something with the first 2 items of the generation
});
var fakes = rules.Generate(5); Kind of obvious there are problems with the code above and probably won't work because specifying Indexes and Ranges are more related to the generation context than So maybe: var fakerObject = new Faker<Issue55Object>()
.RuleFor(o => o.Code, f => f.Random.AlphaNumeric(5))
.RuleFor(o => o.Dba, f => f.Company.CompanyName());
var objects = fakerObject
.Set(^1, (f, t) =>
{
// do something with the last item of the generation
})
.Set(0..2, (f, t[]) =>
{
// do something with the first 2 items of the generation
})
.Generate(5); This way, you keep Need to explore more if it is better to call This way of thinking about the problem kinda makes sense. Need to think about it a bit more. Mostly these are mental notes of the idea I had when reading C# 8 and wanted to write this random idea down before I forgot. Another idea is, Alternatives:var fakerObject = new Faker<Issue55Object>()
.RuleFor(o => o.Code, f => f.Random.AlphaNumeric(5))
.RuleFor(o => o.Dba, f => f.Company.CompanyName());
var objects = fakerObject.Generate(5)
.Modify(^1, (f, t) =>
{
// do something with the last item of the generation
})
.Modify(0..2, (f, t[]) =>
{
// do something with the first 2 items of the generation
}); 💨 🚶 "Bubbles of gas in my brain... Send me off balance, it's not enough" |
I am happy to post this somewhere else if you have a uservoice or something. It would be nice if there was a way to do finishwith on a whole set. For instance, I want to generate a collection of objects and always have the first one set to primary.
Currently I have to do the following:
Instead it would nice to make the rule responsible for this:
The text was updated successfully, but these errors were encountered: