SteveDunn / Vogen

A semi-opinionated library which is a source generator and a code analyser. It Source generates Value Objects
Apache License 2.0
888 stars 46 forks source link

Exception is thrown if type is placed in the `Validation` namespace #669

Closed nZeus closed 2 months ago

nZeus commented 2 months ago

Describe the bug

This worked well before v5.0.0

Place your type inside the Validation folder:

using Vogen;

namespace Xxx.Validation;

[ValueObject<string>]
public partial record MyValueType {
    //...
}

This error is thrown from the generated class: error CS0234: The type or namespace name 'Invalid' does not exist in the namespaceXxx.Validation' (are you missing an assembly reference?)`

I think it is related to this piece of code: https://github.com/SteveDunn/Vogen/blob/dedb1f964983449109f3e264c7ca6f88a1a7e19a/src/Vogen/GenerateCodeForTryFrom.cs#L105

Kind regards, Ilia

Steps to reproduce

^

Expected behaviour

^

TipluSebastian commented 2 months ago

The code producing the bug seems to be the following:

public static ValueObjectOrError<MyValueObject> TryFrom(System.String value)
{
    if (value is null)
    {
        return new ValueObjectOrError<MyValueObject>(Validation.Invalid("The value provided was null"));
    }

    var validation = MyValueObject.Validate(value);
    if (validation != Vogen.Validation.Ok)
    {
        return new ValueObjectOrError<MyValueObject>(validation);
    }

    return new ValueObjectOrError<MyValueObject>(new MyValueObject(value));
}

Instead of Validation.Invalid("The value provided was null") it should be Vogen.Validation.Invalid("The value provided was null").

SteveDunn commented 2 months ago

Hi, thanks for the feedback @nZeus and @TipluSebastian . I'm just building a fix for this and will release a new version later today.

AthenaAzuraeaX commented 2 months ago

I think the bigger problem here is that Vogen relies on using directives. If anything conflicts with names in the Vogen namespace, than the generator will generate incorrect code. It would be best for the generator to use the full names of the types and not add the use Vogen directive at the top of the generated files.

SteveDunn commented 2 months ago

I think the bigger problem here is that Vogen relies on using directives. If anything conflicts with names in the Vogen namespace, than the generator will generate incorrect code. It would be best for the generator to use the full names of the types and not add the use Vogen directive at the top of the generated files.

That is a very good point! The next release won't have these in.

SteveDunn commented 2 months ago

Fixed in 5.0.2