Skip to content

Commit

Permalink
Add index-based access to ODataPath (#2811)
Browse files Browse the repository at this point in the history
* Add index-based access to ODataPath

* Implement First and Last Segments using indices

* Update PublicAPI tests
  • Loading branch information
habbes authored Dec 1, 2023
1 parent 1f67bb9 commit d1f9201
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.OData.UriParser.ODataPath.this[int index].get -> Microsoft.OData.UriParser.ODataPathSegment
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.OData.UriParser.ODataPath.this[int index].get -> Microsoft.OData.UriParser.ODataPathSegment
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.OData.UriParser.ODataPath.this[int index].get -> Microsoft.OData.UriParser.ODataPathSegment
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.OData.UriParser.ODataPath.this[int index].get -> Microsoft.OData.UriParser.ODataPathSegment
10 changes: 8 additions & 2 deletions src/Microsoft.OData.Core/UriParser/SemanticAst/ODataPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ODataPathSegment FirstSegment
{
get
{
return this.segments.FirstOrDefault();
return this.segments.Count > 0 ? this.segments[0] : null;
}
}

Expand All @@ -88,10 +88,16 @@ public ODataPathSegment LastSegment
{
get
{
return this.segments.LastOrDefault();
return this.segments.Count > 0 ? this.segments[this.segments.Count - 1] : null;
}
}

/// <summary>
/// Get the segment at the specified index.
/// </summary>
/// <param name="index">The index of the segment to retrieve</param>
public ODataPathSegment this[int index] => this.segments[index];

/// <summary>
/// Get the number of segments in this path.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentOutOfRangeException>(() => path[0]);

ODataPath path2 = new ODataPath(BatchSegment.Instance, CountSegment.Instance);
Assert.Throws<ArgumentOutOfRangeException>(() => path2[2]);
Assert.Throws<ArgumentOutOfRangeException>(() => path2[-1]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down

0 comments on commit d1f9201

Please sign in to comment.