Skip to content

Commit

Permalink
apacheGH-34119: [C#] Add [] operator to Schema (apache#34126)
Browse files Browse the repository at this point in the history
Add the `[]` operator to Schema that retrieves a field by index if an int is passed, or retrieved a field by name if a string is passed.
* Closes: apache#34119

Lead-authored-by: DanTm99 <[email protected]>
Co-authored-by: Danyaal Khan <[email protected]>
Signed-off-by: Weston Pace <[email protected]>
DanTm99 authored Feb 16, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 1333545 commit 9033573
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions csharp/src/Apache.Arrow/Schema.cs
Original file line number Diff line number Diff line change
@@ -36,6 +36,10 @@ public IReadOnlyDictionary<string, Field> Fields

private readonly IList<Field> _fields;

public Field this[int index] => GetFieldByIndex(index);

public Field this[string name] => GetFieldByName(name);

public Schema(
IEnumerable<Field> fields,
IEnumerable<KeyValuePair<string, string>> metadata)
32 changes: 31 additions & 1 deletion csharp/test/Apache.Arrow.Tests/SchemaBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ public class Build
public void FieldsAreNullableByDefault()
{
var b = new Schema.Builder();

var schema = new Schema.Builder()
.Field(f => f.Name("f0").DataType(Int32Type.Default))
.Build();
@@ -117,6 +117,36 @@ public void GetFieldByNameIsCaseSensitive()
Assert.True(schema.GetFieldByName("f0") == f0 && schema.GetFieldByName("F0") == f0Uppercase);
}

[Fact]
public void SquareBracketOperatorWithStringReturnsGetFieldByName()
{
Field f0 = new Field.Builder().Name("f0").DataType(Int32Type.Default).Build();
Field f1 = new Field.Builder().Name("f1").DataType(Int8Type.Default).Build();

var schema = new Schema.Builder()
.Field(f0)
.Field(f1)
.Build();

Assert.Equal(schema.GetFieldByName("f0"), schema["f0"]);
Assert.Equal(schema.GetFieldByName("f1"), schema["f1"]);
}

[Fact]
public void SquareBracketOperatorWithIntReturnsGetFieldByIndex()
{
Field f0 = new Field.Builder().Name("f0").DataType(Int32Type.Default).Build();
Field f1 = new Field.Builder().Name("f1").DataType(Int8Type.Default).Build();

var schema = new Schema.Builder()
.Field(f0)
.Field(f1)
.Build();

Assert.Equal(schema.GetFieldByIndex(0), schema[0]);
Assert.Equal(schema.GetFieldByIndex(1), schema[1]);
}

[Fact]
public void MetadataConstruction()
{

0 comments on commit 9033573

Please sign in to comment.