Skip to content

Commit

Permalink
Fix a problem with omitting the second argument of UnitOf
Browse files Browse the repository at this point in the history
hadashiA committed Sep 28, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 41a364b commit 9b35996
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions src/UnitGenerator/SourceGenerator.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 9b35996

Please sign in to comment.