Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: third_party/grpc/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #import "NSDictionary+GRPC.h"
35
36 #include <grpc/support/alloc.h>
37
38 #pragma mark Category for binary metadata elements
39
40 @interface NSData (GRPCMetadata)
41 + (instancetype)grpc_dataFromMetadataValue:(grpc_metadata *)metadata;
42
43 // Fill a metadata object with the binary value in this NSData.
44 - (void)grpc_initMetadata:(grpc_metadata *)metadata;
45 @end
46
47 @implementation NSData (GRPCMetadata)
48 + (instancetype)grpc_dataFromMetadataValue:(grpc_metadata *)metadata {
49 // TODO(jcanizales): Should we use a non-copy constructor?
50 return [self dataWithBytes:metadata->value length:metadata->value_length];
51 }
52
53 - (void)grpc_initMetadata:(grpc_metadata *)metadata {
54 metadata->value = self.bytes;
55 metadata->value_length = self.length;
56 }
57 @end
58
59 #pragma mark Category for textual metadata elements
60
61 @interface NSString (GRPCMetadata)
62 + (instancetype)grpc_stringFromMetadataValue:(grpc_metadata *)metadata;
63
64 // Fill a metadata object with the textual value in this NSString.
65 - (void)grpc_initMetadata:(grpc_metadata *)metadata;
66 @end
67
68 @implementation NSString (GRPCMetadata)
69 + (instancetype)grpc_stringFromMetadataValue:(grpc_metadata *)metadata {
70 return [[self alloc] initWithBytes:metadata->value
71 length:metadata->value_length
72 encoding:NSASCIIStringEncoding];
73 }
74
75 // Precondition: This object contains only ASCII characters.
76 - (void)grpc_initMetadata:(grpc_metadata *)metadata {
77 metadata->value = self.UTF8String;
78 metadata->value_length = self.length;
79 }
80 @end
81
82 #pragma mark Category for metadata arrays
83
84 @implementation NSDictionary (GRPC)
85 + (instancetype)grpc_dictionaryFromMetadataArray:(grpc_metadata_array)array {
86 return [self grpc_dictionaryFromMetadata:array.metadata count:array.count];
87 }
88
89 + (instancetype)grpc_dictionaryFromMetadata:(grpc_metadata *)entries count:(size _t)count {
90 NSMutableDictionary *metadata = [NSMutableDictionary dictionaryWithCapacity:co unt];
91 for (grpc_metadata *entry = entries; entry < entries + count; entry++) {
92 NSString *name = [NSString stringWithCString:entry->key encoding:NSASCIIStri ngEncoding];
93 if (!name || metadata[name]) {
94 // Log if name is nil?
95 continue;
96 }
97 id value;
98 if ([name hasSuffix:@"-bin"]) {
99 value = [NSData grpc_dataFromMetadataValue:entry];
100 } else {
101 value = [NSString grpc_stringFromMetadataValue:entry];
102 }
103 metadata[name] = value;
104 }
105 return metadata;
106 }
107
108 // Preconditions: All keys are ASCII strings. Keys ending in -bin have NSData va lues; the others
109 // have NSString values.
110 - (grpc_metadata *)grpc_metadataArray {
111 grpc_metadata *metadata = gpr_malloc([self count] * sizeof(grpc_metadata));
112 grpc_metadata *current = metadata;
113 for (NSString* key in self) {
114 id value = self[key];
115 current->key = key.UTF8String;
116 if ([value respondsToSelector:@selector(grpc_initMetadata:)]) {
117 [value grpc_initMetadata:current];
118 } else {
119 [NSException raise:NSInvalidArgumentException
120 format:@"Metadata values must be NSString or NSData."];
121 }
122 ++current;
123 }
124 return metadata;
125 }
126 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698