diff --git a/src/indice.Edi/EdiSerializer.cs b/src/indice.Edi/EdiSerializer.cs index e71e457..38b58c3 100644 --- a/src/indice.Edi/EdiSerializer.cs +++ b/src/indice.Edi/EdiSerializer.cs @@ -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); @@ -419,7 +429,7 @@ internal bool TryCreateContainer(EdiReader reader, Stack stack, Ed var current = stack.Peek(); var property = default(EdiPropertyDescriptor); - + var childCache = default(Queue); switch (newContainer) { case EdiStructureType.SegmentGroup: property = FindForCurrentSegment(reader, current, newContainer); @@ -428,7 +438,7 @@ internal bool TryCreateContainer(EdiReader reader, Stack 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); @@ -471,7 +481,7 @@ internal bool TryCreateContainer(EdiReader reader, Stack 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; } @@ -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 elementReads) { + elementReads = null; var candidates = currentStructure.GetMatchingProperties(newContainerType); if (candidates.Length == 0) { return null; @@ -511,6 +522,18 @@ private EdiPropertyDescriptor FindForCurrentElement(EdiReader reader, EdiStructu } else { property = ConditionalMatch(reader, currentStructure, newContainerType, matches); } + if (property != null) { + elementReads = new Queue(); + 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; } diff --git a/src/indice.Edi/Serialization/EdiPropertyDescriptor.cs b/src/indice.Edi/Serialization/EdiPropertyDescriptor.cs index 004ee9f..e5cdb50 100644 --- a/src/indice.Edi/Serialization/EdiPropertyDescriptor.cs +++ b/src/indice.Edi/Serialization/EdiPropertyDescriptor.cs @@ -122,10 +122,10 @@ public EdiPropertyDescriptor(PropertyInfo info, IEnumerable 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(); } diff --git a/test/indice.Edi.Tests/EdiTextReaderTests.cs b/test/indice.Edi.Tests/EdiTextReaderTests.cs index 700db55..6716f61 100644 --- a/test/indice.Edi.Tests/EdiTextReaderTests.cs +++ b/test/indice.Edi.Tests/EdiTextReaderTests.cs @@ -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(); @@ -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() {