MelGrubb / BuilderGenerator

A source-generator-based implementation of the Builder pattern
https://melgrubb.github.io/BuilderGenerator/
MIT License
38 stars 8 forks source link

Generator has issues with classes that inherit from chain of classes #14

Closed powerdude closed 2 years ago

powerdude commented 2 years ago

Hi,

I have the following set of classes:

public abstract class Base1
{
    public string Id { get; set; }
}

public abstract class Base2 : Base1
{
}

public class Foo : Base2
{
    public string Name { get; set; }
}

/// <summary>
///     The foo builder.
/// </summary>
[BuilderFor(typeof(Foo))]
public partial class FooBuilder
{
}

and this is the resulting builder:

 // Generated at 2022-06-18T14:17:26
    public partial class FooBuilder : BuilderGenerator.Builder<Domain.Foo>
    {
        public System.Lazy<string> Id = new System.Lazy<string>(() => default(string));
        public System.Lazy<string> Id = new System.Lazy<string>(() => default(string));
        public System.Lazy<string> Name = new System.Lazy<string>(() => default(string));

        public override Domain.Foo Build()
        {
            if (Object?.IsValueCreated != true)
            {
                Object = new System.Lazy<Domain.Foo>(() => 
                {
                    var result = new Domain.Foo 
                    {
                        Id = Id.Value,
                        Id = Id.Value,
                        Name = Name.Value
                    };

                    return result;
                });

                PostProcess(Object.Value);
            }

            return Object.Value;
        }

        public FooBuilder WithId(string value)
        {
            return WithId(() => value);
        }

        public FooBuilder WithId(System.Func<string> func)
        {
            Id = new System.Lazy<string>(func);
            return this;
        }

        public FooBuilder WithoutId()
        {                    
            Id = new System.Lazy<string>(() => default(string));
            return this;
        }

        public FooBuilder WithId(string value)
        {
            return WithId(() => value);
        }

        public FooBuilder WithId(System.Func<string> func)
        {
            Id = new System.Lazy<string>(func);
            return this;
        }

        public FooBuilder WithoutId()
        {                    
            Id = new System.Lazy<string>(() => default(string));
            return this;
        }

        public FooBuilder WithName(string value)
        {
            return WithName(() => value);
        }

        public FooBuilder WithName(System.Func<string> func)
        {
            Name = new System.Lazy<string>(func);
            return this;
        }

        public FooBuilder WithoutName()
        {                    
            Name = new System.Lazy<string>(() => default(string));
            return this;
        }
    }
MelGrubb commented 2 years ago

Well that's disappointing. I'll have to dig in and see what I can find.

MelGrubb commented 2 years ago

I've fixed the core issue, but I'm having trouble getting it published. I'm trying to set up github actions to publish the new versions to NuGet and it's all a bit new to me. I can hand publish again this weekend if I don't get the pipeline working by then.

MelGrubb commented 2 years ago

Assuming what got published is what I'm working with locally, this should be fixed now. Sorry, I'm trying to get a build and publish pipeline set up but it doesn't want to cooperate just yet.