Skip to content

Commit

Permalink
Merge pull request #34 from Cysharp/hadashiA/fix-arg-2nd
Browse files Browse the repository at this point in the history
Fix a problem with omitting the second argument of UnitOf
  • Loading branch information
hadashiA committed Sep 28, 2023
2 parents 897b873 + 9b35996 commit 07f9065
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
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 07f9065

Please sign in to comment.