From d1f9201e4b0d72e76c1c7d99d1b86b7af88e29bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Habinshuti?= Date: Fri, 1 Dec 2023 11:57:37 +0300 Subject: [PATCH] Add index-based access to ODataPath (#2811) * Add index-based access to ODataPath * Implement First and Last Segments using indices * Update PublicAPI tests --- .../PublicAPI/net45/PublicAPI.Unshipped.txt | 1 + .../netcoreapp3.1/PublicAPI.Unshipped.txt | 1 + .../netstandard1.1/PublicAPI.Unshipped.txt | 1 + .../netstandard2.0/PublicAPI.Unshipped.txt | 1 + .../UriParser/SemanticAst/ODataPath.cs | 10 ++++++++-- .../UriParser/SemanticAst/ODataPathTests.cs | 19 +++++++++++++++++++ .../Microsoft.OData.PublicApi.net45.bsl | 4 ++++ ...crosoft.OData.PublicApi.netstandard1.1.bsl | 4 ++++ ...crosoft.OData.PublicApi.netstandard2.0.bsl | 4 ++++ 9 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OData.Core/PublicAPI/net45/PublicAPI.Unshipped.txt b/src/Microsoft.OData.Core/PublicAPI/net45/PublicAPI.Unshipped.txt index e69de29bb2..2b7a2e51d1 100644 --- a/src/Microsoft.OData.Core/PublicAPI/net45/PublicAPI.Unshipped.txt +++ b/src/Microsoft.OData.Core/PublicAPI/net45/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +Microsoft.OData.UriParser.ODataPath.this[int index].get -> Microsoft.OData.UriParser.ODataPathSegment diff --git a/src/Microsoft.OData.Core/PublicAPI/netcoreapp3.1/PublicAPI.Unshipped.txt b/src/Microsoft.OData.Core/PublicAPI/netcoreapp3.1/PublicAPI.Unshipped.txt index e69de29bb2..2b7a2e51d1 100644 --- a/src/Microsoft.OData.Core/PublicAPI/netcoreapp3.1/PublicAPI.Unshipped.txt +++ b/src/Microsoft.OData.Core/PublicAPI/netcoreapp3.1/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +Microsoft.OData.UriParser.ODataPath.this[int index].get -> Microsoft.OData.UriParser.ODataPathSegment diff --git a/src/Microsoft.OData.Core/PublicAPI/netstandard1.1/PublicAPI.Unshipped.txt b/src/Microsoft.OData.Core/PublicAPI/netstandard1.1/PublicAPI.Unshipped.txt index e69de29bb2..2b7a2e51d1 100644 --- a/src/Microsoft.OData.Core/PublicAPI/netstandard1.1/PublicAPI.Unshipped.txt +++ b/src/Microsoft.OData.Core/PublicAPI/netstandard1.1/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +Microsoft.OData.UriParser.ODataPath.this[int index].get -> Microsoft.OData.UriParser.ODataPathSegment diff --git a/src/Microsoft.OData.Core/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/src/Microsoft.OData.Core/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index e69de29bb2..af73de1441 100644 --- a/src/Microsoft.OData.Core/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/Microsoft.OData.Core/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +Microsoft.OData.UriParser.ODataPath.this[int index].get -> Microsoft.OData.UriParser.ODataPathSegment \ No newline at end of file diff --git a/src/Microsoft.OData.Core/UriParser/SemanticAst/ODataPath.cs b/src/Microsoft.OData.Core/UriParser/SemanticAst/ODataPath.cs index 118ed13b85..41aade96a5 100644 --- a/src/Microsoft.OData.Core/UriParser/SemanticAst/ODataPath.cs +++ b/src/Microsoft.OData.Core/UriParser/SemanticAst/ODataPath.cs @@ -77,7 +77,7 @@ public ODataPathSegment FirstSegment { get { - return this.segments.FirstOrDefault(); + return this.segments.Count > 0 ? this.segments[0] : null; } } @@ -88,10 +88,16 @@ public ODataPathSegment LastSegment { get { - return this.segments.LastOrDefault(); + return this.segments.Count > 0 ? this.segments[this.segments.Count - 1] : null; } } + /// + /// Get the segment at the specified index. + /// + /// The index of the segment to retrieve + public ODataPathSegment this[int index] => this.segments[index]; + /// /// Get the number of segments in this path. /// diff --git a/test/FunctionalTests/Microsoft.OData.Core.Tests/UriParser/SemanticAst/ODataPathTests.cs b/test/FunctionalTests/Microsoft.OData.Core.Tests/UriParser/SemanticAst/ODataPathTests.cs index bcd15eb43b..0eea5fe823 100644 --- a/test/FunctionalTests/Microsoft.OData.Core.Tests/UriParser/SemanticAst/ODataPathTests.cs +++ b/test/FunctionalTests/Microsoft.OData.Core.Tests/UriParser/SemanticAst/ODataPathTests.cs @@ -107,5 +107,24 @@ public void PathsOfDifferentLengthsAreNotEqualBatch() { Assert.False(new ODataPath().Equals(new ODataPath(BatchSegment.Instance))); } + + [Fact] + public void IndexShouldReturnCorrectSegment() + { + ODataPath path = new ODataPath(BatchSegment.Instance, CountSegment.Instance); + path[0].ShouldBeBatchSegment(); + path[1].ShouldBeCountSegment(); + } + + [Fact] + public void IndexingOutOfBoundsShouldThrowException() + { + ODataPath path = new ODataPath(); + Assert.Throws(() => path[0]); + + ODataPath path2 = new ODataPath(BatchSegment.Instance, CountSegment.Instance); + Assert.Throws(() => path2[2]); + Assert.Throws(() => path2[-1]); + } } } diff --git a/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.net45.bsl b/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.net45.bsl index a04b813651..4b190af903 100644 --- a/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.net45.bsl +++ b/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.net45.bsl @@ -6484,12 +6484,16 @@ public class Microsoft.OData.UriParser.ODataExpandPath : Microsoft.OData.UriPars public ODataExpandPath (System.Collections.Generic.IEnumerable`1[[Microsoft.OData.UriParser.ODataPathSegment]] segments) } +[ +DefaultMemberAttribute(), +] public class Microsoft.OData.UriParser.ODataPath : IEnumerable, IEnumerable`1 { public ODataPath (Microsoft.OData.UriParser.ODataPathSegment[] segments) public ODataPath (System.Collections.Generic.IEnumerable`1[[Microsoft.OData.UriParser.ODataPathSegment]] segments) int Count { public get; } Microsoft.OData.UriParser.ODataPathSegment FirstSegment { public get; } + Microsoft.OData.UriParser.ODataPathSegment Item [int index] { public get; } Microsoft.OData.UriParser.ODataPathSegment LastSegment { public get; } public virtual System.Collections.Generic.IEnumerator`1[[Microsoft.OData.UriParser.ODataPathSegment]] GetEnumerator () diff --git a/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.netstandard1.1.bsl b/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.netstandard1.1.bsl index 8c1b0af937..68edf066fc 100644 --- a/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.netstandard1.1.bsl +++ b/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.netstandard1.1.bsl @@ -6484,12 +6484,16 @@ public class Microsoft.OData.UriParser.ODataExpandPath : Microsoft.OData.UriPars public ODataExpandPath (System.Collections.Generic.IEnumerable`1[[Microsoft.OData.UriParser.ODataPathSegment]] segments) } +[ +DefaultMemberAttribute(), +] public class Microsoft.OData.UriParser.ODataPath : IEnumerable, IEnumerable`1 { public ODataPath (Microsoft.OData.UriParser.ODataPathSegment[] segments) public ODataPath (System.Collections.Generic.IEnumerable`1[[Microsoft.OData.UriParser.ODataPathSegment]] segments) int Count { public get; } Microsoft.OData.UriParser.ODataPathSegment FirstSegment { public get; } + Microsoft.OData.UriParser.ODataPathSegment Item [int index] { public get; } Microsoft.OData.UriParser.ODataPathSegment LastSegment { public get; } public virtual System.Collections.Generic.IEnumerator`1[[Microsoft.OData.UriParser.ODataPathSegment]] GetEnumerator () diff --git a/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.netstandard2.0.bsl b/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.netstandard2.0.bsl index a04b813651..4b190af903 100644 --- a/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.netstandard2.0.bsl +++ b/test/PublicApiTests/BaseLine/Microsoft.OData.PublicApi.netstandard2.0.bsl @@ -6484,12 +6484,16 @@ public class Microsoft.OData.UriParser.ODataExpandPath : Microsoft.OData.UriPars public ODataExpandPath (System.Collections.Generic.IEnumerable`1[[Microsoft.OData.UriParser.ODataPathSegment]] segments) } +[ +DefaultMemberAttribute(), +] public class Microsoft.OData.UriParser.ODataPath : IEnumerable, IEnumerable`1 { public ODataPath (Microsoft.OData.UriParser.ODataPathSegment[] segments) public ODataPath (System.Collections.Generic.IEnumerable`1[[Microsoft.OData.UriParser.ODataPathSegment]] segments) int Count { public get; } Microsoft.OData.UriParser.ODataPathSegment FirstSegment { public get; } + Microsoft.OData.UriParser.ODataPathSegment Item [int index] { public get; } Microsoft.OData.UriParser.ODataPathSegment LastSegment { public get; } public virtual System.Collections.Generic.IEnumerator`1[[Microsoft.OData.UriParser.ODataPathSegment]] GetEnumerator ()