NeVeSpl / NTypewriter

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

AttributeArgument.Value is not always ITypedConstant or ITypedConstant[] #91

Closed Xriuk closed 11 months ago

Xriuk commented 11 months ago

When casting the IAttributeArgument.Value to ITypedConstant i got an error saying "int cannot be casted to ITypedConstant", so I checked and here: https://github.com/NeVeSpl/NTypewriter/blob/7928b542199daf13a4bf6fade55c3ced2dfd63aa/NTypewriter.CodeModel.Roslyn/TypedConstant.cs#L30-L41 It looks that it's not always creating an ITypedConstant, but it can also be an INamedType or even just a plain object with the actual value.

So I don't know if the docs are not updated or there is an error in the code above https://github.com/NeVeSpl/NTypewriter/blob/master/Documentation/CodeModel.md#iattributeargument

NeVeSpl commented 11 months ago

Docs are not updated, Using ITypedConstant for every possible type caused too many unforeseen breaking changes, so its usage was restricted only for enums for now.

xumix commented 11 months ago

Here is another TypedConstant bug:

var jsonConverterAttribute = property.Attributes?.FirstOrDefault(x => x.FullName == "Newtonsoft.Json.JsonConverterAttribute");

            if (jsonConverterAttribute != null && jsonConverterAttribute.Arguments.Any(x => x.Value.ToString() == "HighPrecisionDecimalConverter"))
            {
                return "string";
            }

The code .Any(x => x.Value.ToString() == "HighPrecisionDecimalConverter") causes error TypedConstant is an array. Use Values property.

NeVeSpl commented 11 months ago

@xumix, it would be nice to have a separate issue for this and also it would also be nice if that issue contained everything needed to reproduce a bug.

xumix commented 11 months ago

@xumix, it would be nice to have a separate issue for this and also it would also be nice if that issue contained everything needed to reproduce a bug.

Done in #92