forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallHeaders.cs
More file actions
80 lines (63 loc) · 2.77 KB
/
CallHeaders.cs
File metadata and controls
80 lines (63 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Apache.Arrow.Flight.Middleware.Interfaces;
using Grpc.Core;
namespace Apache.Arrow.Flight.Middleware;
public class CallHeaders : ICallHeaders, IEnumerable<KeyValuePair<string, string>>
{
private readonly Metadata _metadata;
public CallHeaders(Metadata metadata)
{
_metadata = metadata;
}
public void Add(string key, string value) => _metadata.Add(key, value);
public bool ContainsKey(string key) => _metadata.Any(h => KeyEquals(h.Key, key));
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
foreach (var entry in _metadata)
yield return new KeyValuePair<string, string>(entry.Key, entry.Value);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public string this[string key]
{
get
{
var entry = _metadata.FirstOrDefault(h => KeyEquals(h.Key, key));
return entry?.Value;
}
set
{
var entry = _metadata.FirstOrDefault(h => KeyEquals(h.Key, key));
if (entry != null) _metadata.Remove(entry);
_metadata.Add(key, value);
}
}
public string Get(string key) => this[key];
public byte[] GetBytes(string key) =>
_metadata.FirstOrDefault(h => KeyEquals(h.Key, key))?.ValueBytes;
public IEnumerable<string> GetAll(string key) =>
_metadata.Where(h => KeyEquals(h.Key, key)).Select(h => h.Value);
public IEnumerable<byte[]> GetAllBytes(string key) =>
_metadata.Where(h => KeyEquals(h.Key, key)).Select(h => h.ValueBytes);
public void Insert(string key, string value) => Add(key, value);
public void Insert(string key, byte[] value) => _metadata.Add(key, value);
public ISet<string> Keys => new HashSet<string>(_metadata.Select(h => h.Key));
private static bool KeyEquals(string a, string b) =>
string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
}