From fad5b226daa8f82ae8b8143bc63707f259c05fde Mon Sep 17 00:00:00 2001 From: EnderElement <1000n00bshater@gmail.com> Date: Thu, 4 May 2023 22:01:17 +1000 Subject: [PATCH 1/2] Bypass "docs" in anchor idl generation Issue: From issue #22, generating code from an anchor idl which contains a docs element such as "docs":[ "String" ] will crash the code when executed. Solution: These lines of code will be able to look at the docs section without issue, however these changes do not generate code with the contents of docs. --- .../IIdlTypeDefinitionTyConverter.cs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Solnet.Anchor/Converters/IIdlTypeDefinitionTyConverter.cs b/Solnet.Anchor/Converters/IIdlTypeDefinitionTyConverter.cs index 16a4c00..d183310 100644 --- a/Solnet.Anchor/Converters/IIdlTypeDefinitionTyConverter.cs +++ b/Solnet.Anchor/Converters/IIdlTypeDefinitionTyConverter.cs @@ -33,7 +33,30 @@ public override IIdlTypeDefinitionTy[] Read(ref Utf8JsonReader reader, Type type if (reader.TokenType != JsonTokenType.String) throw new JsonException("Unexpected error value."); string typeName = reader.GetString(); + + reader.Read(); + if (reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Unexpected error value."); + + propertyName = reader.GetString(); + if ("docs" != propertyName) throw new JsonException("Unexpected error value."); + + reader.Read(); + if (reader.TokenType != JsonTokenType.StartArray) throw new JsonException("Unexpected error value."); + + reader.Read(); + if (reader.TokenType != JsonTokenType.String) throw new JsonException("Unexpected error value."); + + again: + reader.Read(); + if (reader.TokenType == JsonTokenType.String) + { + if (reader.TokenType != JsonTokenType.String) throw new JsonException("Unexpected error value."); + goto again; + } + + if (reader.TokenType != JsonTokenType.EndArray) throw new JsonException("Unexpected error value."); + reader.Read(); if (reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Unexpected error value."); @@ -173,4 +196,4 @@ public override void Write(Utf8JsonWriter writer, IIdlTypeDefinitionTy[] value, throw new NotImplementedException(); } } -} \ No newline at end of file +} From 1899dc937e936e05c87e6803667f6f1279645c8c Mon Sep 17 00:00:00 2001 From: EnderElement <1000n00bshater@gmail.com> Date: Thu, 4 May 2023 23:05:47 +1000 Subject: [PATCH 2/2] Handle different "docs" and "type" order --- .../IIdlTypeDefinitionTyConverter.cs | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/Solnet.Anchor/Converters/IIdlTypeDefinitionTyConverter.cs b/Solnet.Anchor/Converters/IIdlTypeDefinitionTyConverter.cs index d183310..f1be995 100644 --- a/Solnet.Anchor/Converters/IIdlTypeDefinitionTyConverter.cs +++ b/Solnet.Anchor/Converters/IIdlTypeDefinitionTyConverter.cs @@ -34,34 +34,30 @@ public override IIdlTypeDefinitionTy[] Read(ref Utf8JsonReader reader, Type type string typeName = reader.GetString(); - reader.Read(); if (reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Unexpected error value."); propertyName = reader.GetString(); - if ("docs" != propertyName) throw new JsonException("Unexpected error value."); + if (propertyName == "docs") + { + reader.Read(); + if (reader.TokenType != JsonTokenType.StartArray) throw new JsonException("Unexpected error value."); - reader.Read(); - if (reader.TokenType != JsonTokenType.StartArray) throw new JsonException("Unexpected error value."); + reader.Read(); + if (reader.TokenType != JsonTokenType.String) throw new JsonException("Unexpected error value."); - reader.Read(); - if (reader.TokenType != JsonTokenType.String) throw new JsonException("Unexpected error value."); + again: + reader.Read(); + if (reader.TokenType == JsonTokenType.String) goto again; - again: - reader.Read(); - if (reader.TokenType == JsonTokenType.String) - { - if (reader.TokenType != JsonTokenType.String) throw new JsonException("Unexpected error value."); - goto again; - } + if (reader.TokenType != JsonTokenType.EndArray) throw new JsonException("Unexpected error value."); - if (reader.TokenType != JsonTokenType.EndArray) throw new JsonException("Unexpected error value."); - + reader.Read(); + if (reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Unexpected error value."); - reader.Read(); - if (reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Unexpected error value."); + propertyName = reader.GetString(); + } - propertyName = reader.GetString(); if ("type" != propertyName) throw new JsonException("Unexpected error value."); reader.Read();