Skip to content

Commit

Permalink
Fixed crash when no error codes are defined in the IDL. (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiago18c authored Mar 30, 2022
1 parent 84d5186 commit 2b84866
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion SharedBuildProperties.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Product>Solnet</Product>
<Version>0.2.0</Version>
<Version>0.2.1</Version>
<Copyright>Copyright 2022 &#169; Solnet</Copyright>
<Authors>blockmountain</Authors>
<PublisherName>blockmountain</PublisherName>
Expand Down
23 changes: 12 additions & 11 deletions Solnet.Anchor/ClientGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,12 @@ private List<UsingDirectiveSyntax> GenerateUsings(Idl idl)
UsingDirective(IdentifierName("Solnet.Wallet")),
UsingDirective(IdentifierName(idl.Name.ToPascalCase())),
UsingDirective(IdentifierName(idl.Name.ToPascalCase() + ".Program")),
UsingDirective(IdentifierName(idl.Name.ToPascalCase() + ".Errors"))
};

if (idl.Accounts != null && idl.Accounts.Length > 0)
usings.Add(UsingDirective(IdentifierName(idl.Name.ToPascalCase() + ".Accounts")));

if (idl.Errors != null && idl.Errors.Length > 0)
usings.Add(UsingDirective(IdentifierName(idl.Name.ToPascalCase() + ".Errors")));

if (idl.Events != null && idl.Events.Length > 0)
usings.Add(UsingDirective(IdentifierName(idl.Name.ToPascalCase() + ".Events")));

Expand Down Expand Up @@ -916,12 +914,14 @@ private MemberDeclarationSyntax GenerateErrorMapping(Idl idl)


/// {1u, new ProgramError(EnumKind.EnumVariant, "error message)},
foreach (var val in idl.Errors)
if (idl.Errors != null)
{
var errValue = InitializerExpression(
SyntaxKind.ComplexElementInitializerExpression,
SeparatedList<ExpressionSyntax>(
new SyntaxNodeOrToken[]{
foreach (var val in idl.Errors)
{
var errValue = InitializerExpression(
SyntaxKind.ComplexElementInitializerExpression,
SeparatedList<ExpressionSyntax>(
new SyntaxNodeOrToken[]{
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(val.Code)),
Token(SyntaxKind.CommaToken),
ObjectCreationExpression(
Expand All @@ -942,8 +942,9 @@ private MemberDeclarationSyntax GenerateErrorMapping(Idl idl)
LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal(val.Msg)))})), default)}));
syntaxNodeOrTokens.Add(errValue);
syntaxNodeOrTokens.Add(Token(SyntaxKind.CommaToken));
syntaxNodeOrTokens.Add(errValue);
syntaxNodeOrTokens.Add(Token(SyntaxKind.CommaToken));
}
}


Expand Down Expand Up @@ -2189,7 +2190,7 @@ private MemberDeclarationSyntax GenerateErrorsSyntaxTree(Idl idl)
{
SyntaxNodeOrTokenList errors = new SyntaxNodeOrTokenList();

for (int i = 0; i < idl.Errors.Length; i++)
for (int i = 0; i < idl.Errors?.Length; i++)
{
var dec = EnumMemberDeclaration(Identifier(idl.Errors[i].Name.ToPascalCase()))
.WithEqualsValue(
Expand Down

0 comments on commit 2b84866

Please sign in to comment.