NeVeSpl / NTypewriter

File/code generator using Scriban text templates populated with C# code metadata from Roslyn API.
https://nevespl.github.io/NTypewriter/
MIT License
126 stars 25 forks source link

extracting argument from attribute now fails #88

Closed gregveres closed 1 year ago

gregveres commented 1 year ago

I accidentally upgraded to the lastest version of NTypewriter and my scripts started failing. I have narrowed it down to this custom function:

        public static string ExtractTypescriptType(IAttribute attr)
        {
            if (attr.Name == "TypescriptType")
            {
                var attrType = attr.Arguments.FirstOrDefault()?.Value;
                if (attrType != null) return attrType as string;
            }

            return string.Empty;
        }

The failure happens with the statement return attrType as string;. In previous versions this used to properly take the attrType and convert it to a string and return it. In the lastest version this always returns null.

In the debugger, I can see that attrType is of type: For more context, the IAttribute is object {NTypewriter.CodeModel.Roslyn.TypedConstant} with a value of "string|null" and a type of {string}

I am defining the attribute in my project like this:

    /// <summary>
    ///     Indicates the type that should be used for this class property or class when exporting to typescript.
    ///     [TypescriptType(type = "string?")] for example this makes the property or every instance of the class
    ///     an optional string
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
    public class TypescriptType: Attribute
    {
        public string Type { get; set; }

        public override string ToString()
        {
            return Type;
        }
    }

In the Summary text you can see how it is used.

It looks like I used to be on version 0.4.1 and I have upgraded to 0.4.4.

NeVeSpl commented 1 year ago

There was a breaking change, IAttributeArgument.Value is now of ITypedConstant or ITypedConstant[].

So, smth like that should work:

public static string ExtractTypescriptType(IAttribute attr)
{
    if (attr.Name == "TypescriptType")
    {
        var attrType = attr.Arguments.FirstOrDefault()?.Value as ITypedConstant;
        if (attrType != null) return attrType.Value as string;
    }

    return string.Empty;
}
gregveres commented 1 year ago

Yes, thanks. I just figured that out and changed my code to something very similar to this. I was about to post something similar.

gregveres commented 1 year ago

I seem to remember there was a way to do an equivalent of a console.log() in the scriban script, but I can't find any reference to it and I didn't leave myself a note in my code. Is there such a thing or am I dreaming? Haivng fixed this issue, I am still running into a null reference in another script. I think another user created an issue for that one.

NeVeSpl commented 1 year ago

Debug.WriteLine

Version 0.4.4.1 with fixed null reference problem is published, hopefully, there is not another one.

gregveres commented 1 year ago

closing since this is due toa breaking change that can be handled in my code.