RecordGenerator icon indicating copy to clipboard operation
RecordGenerator copied to clipboard

Default values are not applied

Open DmitriyBobrovskiy opened this issue 5 years ago • 3 comments

Hi, first of all thank you for this library. It's awesome.

Found out that if I try to set default value to property when there is only getter, then it's not set up. So here IsCenter will be null

public bool? IsCenter { get; } = true;

But if I make it like this

public bool? IsCenter { get; private set; } = true;

Then it will be true.

DmitriyBobrovskiy avatar Sep 24 '20 13:09 DmitriyBobrovskiy

But if I add private set; then I don't see that property in Builder.

DmitriyBobrovskiy avatar Sep 24 '20 13:09 DmitriyBobrovskiy

I believe this is due to this snippet not actually "copying over" the default value assignments to the builder's properties: https://github.com/amis92/RecordGenerator/blob/f1fca2d21453aa6d9d20ff53299bdf11db6bdf36/src/Amadevus.RecordGenerator.Generators/BuilderPartialGenerator.cs#L33-L48

Then once you add private set I think it's not using this for the Record at all as it only accepts read-only properties? I might be wrong on that one.

bddckr avatar Sep 26 '20 04:09 bddckr

Hi, yes you're correct - default values aren't copied over to the Builder. Since the record's constructor actually requires passing all of the properties into it, it wouldn't ever be used anyway. It could be indeed if "copied over" into Builder - but it introduced a lot of other headaches (e.g. default value being a class constant/static field would fail to resolve correctly inside Builder).

I won't be developing this project since C#9 provides record types which do exactly everything this project does, but better (and cleaner).

If you'd provide a PR with unit tests, it's possible I'd accept and release it however, because it doesn't seem to be a big change.

amis92 avatar Sep 30 '20 18:09 amis92