From 9b359965896ca408a4aa6b090810df989e904c7a Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 28 Sep 2023 11:53:20 +0900 Subject: [PATCH] Fix a problem with omitting the second argument of UnitOf --- src/UnitGenerator/SourceGenerator.cs | 61 +++++++++++++++------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/src/UnitGenerator/SourceGenerator.cs b/src/UnitGenerator/SourceGenerator.cs index 1a4d7fd..55ff438 100644 --- a/src/UnitGenerator/SourceGenerator.cs +++ b/src/UnitGenerator/SourceGenerator.cs @@ -41,40 +41,43 @@ public void Execute(GeneratorExecutionContext context) { var arg = attr.ArgumentList.Arguments[i]; var expr = arg.Expression; - - if (i == 0) // Type type + + var argName = arg.NameEquals?.Name.ToString(); + switch (argName) { - if (expr is TypeOfExpressionSyntax typeOfExpr) - { - var typeSymbol = model.GetSymbolInfo(typeOfExpr.Type).Symbol as ITypeSymbol; - if (typeSymbol == null) throw new Exception("require type-symbol."); - prop.Type = typeSymbol; - } - else + case "ArithmeticOperators": { - throw new Exception("require UnitOf attribute and ctor."); + var parsed = Enum.ToObject(typeof(UnitArithmeticOperators), model.GetConstantValue(expr).Value); + prop.ArithmeticOperators = (UnitArithmeticOperators)parsed; + break; } - } - else if (i == 1) // UnitGenerateOptions options - { - // e.g. UnitGenerateOptions.ImplicitOperator | UnitGenerateOptions.ParseMethod => ImplicitOperatior, ParseMethod - var parsed = Enum.ToObject(typeof(UnitGenerateOptions), model.GetConstantValue(expr).Value); - prop.Options = (UnitGenerateOptions)parsed; - } - else - { - var argName = arg.NameEquals?.Name.ToString(); - switch (argName) + case "ToStringFormat": { - case "ArithmeticOperators": - var parsed = Enum.ToObject(typeof(UnitArithmeticOperators), model.GetConstantValue(expr).Value); - prop.ArithmeticOperators = (UnitArithmeticOperators)parsed; - break; - case "ToStringFormat": - var format = model.GetConstantValue(expr).Value?.ToString(); - prop.ToStringFormat = format; - break; + var format = model.GetConstantValue(expr).Value?.ToString(); + prop.ToStringFormat = format; + break; } + default: + if (i == 0) // Type type + { + if (expr is TypeOfExpressionSyntax typeOfExpr) + { + var typeSymbol = model.GetSymbolInfo(typeOfExpr.Type).Symbol as ITypeSymbol; + if (typeSymbol == null) throw new Exception("require type-symbol."); + prop.Type = typeSymbol; + } + else + { + throw new Exception("require UnitOf attribute and ctor."); + } + } + else if (i == 1) // UnitGenerateOptions options + { + // e.g. UnitGenerateOptions.ImplicitOperator | UnitGenerateOptions.ParseMethod => ImplicitOperatior, ParseMethod + var parsed = Enum.ToObject(typeof(UnitGenerateOptions), model.GetConstantValue(expr).Value); + prop.Options = (UnitGenerateOptions)parsed; + } + break; } }