Merge pull request #22743 from JamesNK/jamesnk/metadata-helpers

Add helper methods for getting metadata values
pull/22786/head
Jan Tattermusch 5 years ago committed by GitHub
commit 71a22caaf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 87
      src/csharp/Grpc.Core.Api/Metadata.cs
  2. 106
      src/csharp/Grpc.Core.Tests/MetadataTest.cs

@ -75,7 +75,72 @@ namespace Grpc.Core
return this;
}
// TODO: add support for access by key
/// <summary>
/// Gets the last metadata entry with the specified key.
/// If there are no matching entries then <c>null</c> is returned.
/// </summary>
public Entry Get(string key)
{
for (int i = entries.Count - 1; i >= 0; i--)
{
if (entries[i].Key == key)
{
return entries[i];
}
}
return null;
}
/// <summary>
/// Gets the string value of the last metadata entry with the specified key.
/// If the metadata entry is binary then an exception is thrown.
/// If there are no matching entries then <c>null</c> is returned.
/// </summary>
public string GetValue(string key)
{
return Get(key)?.Value;
}
/// <summary>
/// Gets the bytes value of the last metadata entry with the specified key.
/// If the metadata entry is not binary the string value will be returned as ASCII encoded bytes.
/// If there are no matching entries then <c>null</c> is returned.
/// </summary>
public byte[] GetValueBytes(string key)
{
return Get(key)?.ValueBytes;
}
/// <summary>
/// Gets all metadata entries with the specified key.
/// </summary>
public IEnumerable<Entry> GetAll(string key)
{
for (int i = 0; i < entries.Count; i++)
{
if (entries[i].Key == key)
{
yield return entries[i];
}
}
}
/// <summary>
/// Adds a new ASCII-valued metadata entry. See <c>Metadata.Entry</c> constructor for params.
/// </summary>
public void Add(string key, string value)
{
Add(new Entry(key, value));
}
/// <summary>
/// Adds a new binary-valued metadata entry. See <c>Metadata.Entry</c> constructor for params.
/// </summary>
public void Add(string key, byte[] valueBytes)
{
Add(new Entry(key, valueBytes));
}
#region IList members
@ -135,22 +200,6 @@ namespace Grpc.Core
entries.Add(item);
}
/// <summary>
/// Adds a new ASCII-valued metadata entry. See <c>Metadata.Entry</c> constructor for params.
/// </summary>
public void Add(string key, string value)
{
Add(new Entry(key, value));
}
/// <summary>
/// Adds a new binary-valued metadata entry. See <c>Metadata.Entry</c> constructor for params.
/// </summary>
public void Add(string key, byte[] valueBytes)
{
Add(new Entry(key, valueBytes));
}
/// <summary>
/// <see cref="T:IList`1"/>
/// </summary>
@ -280,6 +329,7 @@ namespace Grpc.Core
/// <summary>
/// Gets the binary value of this metadata entry.
/// If the metadata entry is not binary the string value will be returned as ASCII encoded bytes.
/// </summary>
public byte[] ValueBytes
{
@ -299,13 +349,14 @@ namespace Grpc.Core
/// <summary>
/// Gets the string value of this metadata entry.
/// If the metadata entry is binary then an exception is thrown.
/// </summary>
public string Value
{
get
{
GrpcPreconditions.CheckState(!IsBinary, "Cannot access string value of a binary metadata entry");
return value ?? EncodingASCII.GetString(valueBytes);
return value;
}
}

@ -18,7 +18,9 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
@ -242,6 +244,110 @@ namespace Grpc.Core.Tests
Assert.Throws<InvalidOperationException>(() => metadata.Remove(metadata[0]));
}
[Test]
public void GetAll()
{
var metadata = new Metadata
{
{ "abc", "abc-value1" },
{ "abc", "abc-value2" },
{ "xyz", "xyz-value1" },
};
var abcEntries = metadata.GetAll("abc").ToList();
Assert.AreEqual(2, abcEntries.Count);
Assert.AreEqual("abc-value1", abcEntries[0].Value);
Assert.AreEqual("abc-value2", abcEntries[1].Value);
var xyzEntries = metadata.GetAll("xyz").ToList();
Assert.AreEqual(1, xyzEntries.Count);
Assert.AreEqual("xyz-value1", xyzEntries[0].Value);
}
[Test]
public void Get()
{
var metadata = new Metadata
{
{ "abc", "abc-value1" },
{ "abc", "abc-value2" },
{ "xyz", "xyz-value1" },
};
var abcEntry = metadata.Get("abc");
Assert.AreEqual("abc-value2", abcEntry.Value);
var xyzEntry = metadata.Get("xyz");
Assert.AreEqual("xyz-value1", xyzEntry.Value);
var notFound = metadata.Get("not-found");
Assert.AreEqual(null, notFound);
}
[Test]
public void GetValue()
{
var metadata = new Metadata
{
{ "abc", "abc-value1" },
{ "abc", "abc-value2" },
{ "xyz", "xyz-value1" },
{ "xyz-bin", Encoding.ASCII.GetBytes("xyz-value1") },
};
var abcValue = metadata.GetValue("abc");
Assert.AreEqual("abc-value2", abcValue);
var xyzValue = metadata.GetValue("xyz");
Assert.AreEqual("xyz-value1", xyzValue);
var notFound = metadata.GetValue("not-found");
Assert.AreEqual(null, notFound);
}
[Test]
public void GetValue_BytesValue()
{
var metadata = new Metadata
{
{ "xyz-bin", Encoding.ASCII.GetBytes("xyz-value1") },
};
Assert.Throws<InvalidOperationException>(() => metadata.GetValue("xyz-bin"));
}
[Test]
public void GetValueBytes()
{
var metadata = new Metadata
{
{ "abc-bin", Encoding.ASCII.GetBytes("abc-value1") },
{ "abc-bin", Encoding.ASCII.GetBytes("abc-value2") },
{ "xyz-bin", Encoding.ASCII.GetBytes("xyz-value1") },
};
var abcValue = metadata.GetValueBytes("abc-bin");
Assert.AreEqual(Encoding.ASCII.GetBytes("abc-value2"), abcValue);
var xyzValue = metadata.GetValueBytes("xyz-bin");
Assert.AreEqual(Encoding.ASCII.GetBytes("xyz-value1"), xyzValue);
var notFound = metadata.GetValueBytes("not-found");
Assert.AreEqual(null, notFound);
}
[Test]
public void GetValueBytes_StringValue()
{
var metadata = new Metadata
{
{ "xyz", "xyz-value1" },
};
var xyzValue = metadata.GetValueBytes("xyz");
Assert.AreEqual(Encoding.ASCII.GetBytes("xyz-value1"), xyzValue);
}
private Metadata CreateMetadata()
{
return new Metadata

Loading…
Cancel
Save