Skip to content

Commit

Permalink
Fix #148 allow text reader skip over container
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderNate authored and popematt committed Dec 19, 2022
1 parent ff73b85 commit 627d26f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
27 changes: 27 additions & 0 deletions Amazon.IonDotnet.Tests/Internals/TextReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,5 +350,32 @@ public void Blob_PartialRead(int size, int step)
var reader = new UserTextReader(text);
ReaderTestCommon.Blob_PartialRead(size, step, reader);
}

[TestMethod()]
public void SkipOverListInStruct()
{
var reader = new UserTextReader("{ somelist: [ 1 ] }");
Assert.AreEqual(IonType.Struct, reader.MoveNext());
reader.StepIn();
Assert.AreEqual(IonType.List, reader.MoveNext());
Assert.AreEqual("somelist", reader.CurrentFieldName);
// Now skip over the list instead of stepping into it
Assert.AreEqual(IonType.None, reader.MoveNext());
reader.StepOut(); // Step out of struct
Assert.AreEqual(IonType.None, reader.MoveNext());
}

[TestMethod()]
public void SkipOverStructInList()
{
var reader = new UserTextReader("[{}]");
Assert.AreEqual(IonType.List, reader.MoveNext());
reader.StepIn();
Assert.AreEqual(IonType.Struct, reader.MoveNext());
// Skip over the struct instead of stepping in
Assert.AreEqual(IonType.None, reader.MoveNext());
reader.StepOut(); // Step out of list
Assert.AreEqual(IonType.None, reader.MoveNext());
}
}
}
17 changes: 14 additions & 3 deletions Amazon.IonDotnet/Internals/Text/RawTextReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public void StepIn()

this.state = GetStateAtContainerStart(this.valueType);
this.containerStack.PushContainer(this.valueType);
var closeSymbol = this.GetClosingTokenForContainerType(this.valueType);
this.expectedContainerClosingSymbol.Push(closeSymbol);
this.scanner.MarkTokenFinished();

this.FinishValue();
Expand Down Expand Up @@ -605,17 +607,14 @@ private void ParseNext()
break;
case ActionStartStruct:
this.valueType = IonType.Struct;
this.expectedContainerClosingSymbol.Push(TextConstants.TokenCloseBrace);
this.state = StateBeforeFieldName;
return;
case ActionStartList:
this.valueType = IonType.List;
this.expectedContainerClosingSymbol.Push(TextConstants.TokenCloseSquare);
this.state = StateBeforeAnnotationContained;
return;
case ActionStartSexp:
this.valueType = IonType.Sexp;
this.expectedContainerClosingSymbol.Push(TextConstants.TokenCloseParen);
this.state = StateBeforeAnnotationSexp;
return;
case ActionStartLob:
Expand Down Expand Up @@ -757,6 +756,18 @@ private char GetCharacterValueOfClosingContainerToken(int tokenCode)
}
}

private int GetClosingTokenForContainerType(IonType containerType)
{
switch (containerType)
{
case IonType.Struct: return TextConstants.TokenCloseBrace;
case IonType.List: return TextConstants.TokenCloseSquare;
case IonType.Sexp: return TextConstants.TokenCloseParen;
default:
throw new Exception("Invalid value type while determining expectedContainerClosingSymbol");
}
}

private void ReadNullType(bool trailingWhitespace)
{
var kwt = trailingWhitespace ? TextConstants.KeywordNone : this.scanner.PeekNullTypeSymbol();
Expand Down

0 comments on commit 627d26f

Please sign in to comment.