Skip to content

Commit

Permalink
Element list with conditions and wildcard paths. Fixes #121
Browse files Browse the repository at this point in the history
  • Loading branch information
cleftheris committed Oct 11, 2019
1 parent df6ad6b commit 49e452a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
33 changes: 28 additions & 5 deletions src/indice.Edi/EdiSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,19 @@ internal virtual object DeserializeInternal(EdiReader reader, Type objectType) {
reader.Value));
}
}
} else if (reader.TokenType == EdiToken.ElementStart) {
}

if (reader.TokenType == EdiToken.ElementStart) {
TryCreateContainer(reader, stack, EdiStructureType.Element);
}
else if (stack.Count > 0 && stack.Peek().CachedReads.Count > 0) {
var allCachedReads = stack.Peek().CachedReads;
while (allCachedReads.Count > 0 && TryCreateContainer(reader, stack, EdiStructureType.Element)) {
if (stack.Peek().CachedReads.Any(x => x.HasValue)) {
PopulateValue(reader, stack, ref structuralComparer);
}
}
}

if (reader.TokenType == EdiToken.ComponentStart || (stack.Count > 0 && stack.Peek().CachedReads.Count > 0 && reader.TokenType.IsPrimitiveToken())) {
PopulateValue(reader, stack, ref structuralComparer);
Expand Down Expand Up @@ -419,7 +429,7 @@ internal bool TryCreateContainer(EdiReader reader, Stack<EdiStructure> stack, Ed

var current = stack.Peek();
var property = default(EdiPropertyDescriptor);

var childCache = default(Queue<EdiEntry>);
switch (newContainer) {
case EdiStructureType.SegmentGroup:
property = FindForCurrentSegment(reader, current, newContainer);
Expand All @@ -428,7 +438,7 @@ internal bool TryCreateContainer(EdiReader reader, Stack<EdiStructure> stack, Ed
property = FindForCurrentSegment(reader, current, newContainer);
break;
case EdiStructureType.Element:
property = FindForCurrentElement(reader, current, newContainer);
property = FindForCurrentElement(reader, current, newContainer, out childCache);
break;
default:
property = FindForCurrentLogicalStructure(reader, current, newContainer);
Expand Down Expand Up @@ -471,7 +481,7 @@ internal bool TryCreateContainer(EdiReader reader, Stack<EdiStructure> stack, Ed
}
propValue = item;
}
stack.Push(new EdiStructure(newContainer, current, property, propValue, index, current.CachedReads));
stack.Push(new EdiStructure(newContainer, current, property, propValue, index, childCache ?? current.CachedReads));
return true;
}

Expand All @@ -495,7 +505,8 @@ private EdiPropertyDescriptor FindForCurrentSegment(EdiReader reader, EdiStructu
return property;
}

private EdiPropertyDescriptor FindForCurrentElement(EdiReader reader, EdiStructure currentStructure, EdiStructureType newContainerType) {
private EdiPropertyDescriptor FindForCurrentElement(EdiReader reader, EdiStructure currentStructure, EdiStructureType newContainerType, out Queue<EdiEntry> elementReads) {
elementReads = null;
var candidates = currentStructure.GetMatchingProperties(newContainerType);
if (candidates.Length == 0) {
return null;
Expand All @@ -511,6 +522,18 @@ private EdiPropertyDescriptor FindForCurrentElement(EdiReader reader, EdiStructu
} else {
property = ConditionalMatch(reader, currentStructure, newContainerType, matches);
}
if (property != null) {
elementReads = new Queue<EdiEntry>();
var parentCache = currentStructure.CachedReads;
while (parentCache.Count > 0 && elementPath == ((EdiPath)parentCache.Peek().Path).ToString("E")) {
elementReads.Enqueue(parentCache.Dequeue());
}
//foreach (var item in currentStructure.CachedReads) {
// if (elementPath == ((EdiPath)item.Path).ToString("E")) {
// elementReads.Enqueue(item);
// }
//}
}
}
return property;
}
Expand Down
4 changes: 2 additions & 2 deletions src/indice.Edi/Serialization/EdiPropertyDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ public EdiPropertyDescriptor(PropertyInfo info, IEnumerable<EdiAttribute> attrib

public override string ToString() {
if (ValueInfo != null) {
return $"Value @ {Path}";
return $"Value @ {_PathInfo?.PathInternal.ToString("o")}";
}
if (Attributes.Count > 0) {
return $"{Attributes.InferStructure()} @ {Path}";
return $"{Attributes.InferStructure()} @ {_PathInfo?.PathInternal.ToString("o")}";
}
return base.ToString();
}
Expand Down
8 changes: 6 additions & 2 deletions test/indice.Edi.Tests/EdiTextReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ public void EdiFact_ElementList() {
Assert.Equal("N", interchange.Msg.Attributes[2].Infos[5].Value);
}

/*

[Fact, Trait(Traits.Tag, "EdiFact"), Trait(Traits.Issue, "#121")]
public void EdiFact_ElementList_WithConditions() {
var grammar = EdiGrammar.NewEdiFact();
Expand All @@ -944,8 +944,12 @@ public void EdiFact_ElementList_WithConditions() {
Assert.Equal(6, interchange.Msg.Attributes[2].Infos.Count);
Assert.Equal("PRIL", interchange.Msg.Attributes[2].Infos[5].Code);
Assert.Equal("N", interchange.Msg.Attributes[2].Infos[5].Value);
Assert.Single(interchange.Msg.Attributes1);
Assert.Equal(2, interchange.Msg.Attributes1[0].Infos.Count);
Assert.Equal("V", interchange.Msg.Attributes1[0].Infos[1].Code);
Assert.Equal("�2", interchange.Msg.Attributes1[0].LTS.Value);
}
*/


[Fact, Trait(Traits.Tag, "X12"), Trait(Traits.Issue, "#135")]
public void X12_DTM_Should_not_be_empty() {
Expand Down

0 comments on commit 49e452a

Please sign in to comment.