aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objective-c
diff options
context:
space:
mode:
authorGravatar Jorge Canizales <jcanizales@google.com>2015-09-02 19:35:13 -0700
committerGravatar Jorge Canizales <jcanizales@google.com>2015-09-02 22:03:07 -0700
commit05640a58dfef890165b49045cd158cc4defe7a86 (patch)
tree91c7b3d8364a4e8c5aee8876f0f1ffe9232d5168 /src/objective-c
parent030e5d0c9c2b9abd17dc061a3a682114a4d2cc38 (diff)
Simpler code, better exceptions.
Diffstat (limited to 'src/objective-c')
-rw-r--r--src/objective-c/GRPCClient/GRPCRequestHeaders.m85
1 files changed, 35 insertions, 50 deletions
diff --git a/src/objective-c/GRPCClient/GRPCRequestHeaders.m b/src/objective-c/GRPCClient/GRPCRequestHeaders.m
index 23140cb78a..d4bbbdf84a 100644
--- a/src/objective-c/GRPCClient/GRPCRequestHeaders.m
+++ b/src/objective-c/GRPCClient/GRPCRequestHeaders.m
@@ -38,25 +38,33 @@
#import "GRPCCall.h"
#import "NSDictionary+GRPC.h"
+// Used by the setters.
static NSString* NormalizeKey(NSString* key) {
- if ([key canBeConvertedToEncoding:NSASCIIStringEncoding]) {
- return [key lowercaseString];
- } else {
- return nil;
+ if (!key) {
+ [NSException raise:NSInvalidArgumentException format:@"Key cannot be nil"];
+ }
+ if (![key canBeConvertedToEncoding:NSASCIIStringEncoding]) {
+ [NSException raise:NSInvalidArgumentException
+ format:@"Key %@ contains non-ASCII characters", key];
}
+ return key.lowercaseString;
}
-static bool IsKeyValuePairValid(NSString *key, id value) {
+// Precondition: key isn't nil.
+static void CheckKeyValuePairIsValid(NSString *key, id value) {
if ([key hasSuffix:@"-bin"]) {
- if (![value isKindOfClass:[NSData class]]) {
- return false;
+ if (![value isKindOfClass:NSData.class]) {
+ [NSException raise:NSInvalidArgumentException
+ format:@"Expected NSData value for header %@ ending in \"-bin\", "
+ @"instead got %@", key, value];
}
} else {
- if (![value isKindOfClass:[NSString class]]) {
- return false;
+ if (![value isKindOfClass:NSString.class]) {
+ [NSException raise:NSInvalidArgumentException
+ format:@"Expected NSString value for header %@ not ending in \"-bin\", "
+ @"instead got %@", key, value];
}
}
- return true;
}
@implementation GRPCRequestHeaders {
@@ -72,55 +80,32 @@ static bool IsKeyValuePairValid(NSString *key, id value) {
return self;
}
-- (id)objectForKeyedSubscript:(NSString *)key {
- NSString *normalizedKey = NormalizeKey(key);
- if (normalizedKey) {
- return _proxy[normalizedKey];
- } else {
- return [NSNull null];
+- (void)checkCallIsNotStarted {
+ if (_call.state != GRXWriterStateNotStarted) {
+ [NSException raise:@"Invalid modification"
+ format:@"Cannot modify request headers after call is started"];
}
}
+- (id)objectForKeyedSubscript:(NSString *)key {
+ return _proxy[key.lowercaseString];
+}
+
- (void)setObject:(id)obj forKeyedSubscript:(NSString *)key {
- if (_call.state == GRXWriterStateNotStarted) {
- NSString *normalizedKey = NormalizeKey(key);
- if (normalizedKey) {
- if (IsKeyValuePairValid(key, obj)) {
- _proxy[normalizedKey] = obj;
- } else {
- [NSException raise:@"Invalid key/value pair"
- format:@"Key %@ could not be added with value %@", key, obj];
- }
- } else {
- [NSException raise:@"Invalid key" format:@"Key %@ contains illegal characters", key];
- }
- } else {
- [NSException raise:@"Invalid modification"
- format:@"Cannot modify request metadata after call is started"];
- }
+ [self checkCallIsNotStarted];
+ key = NormalizeKey(key);
+ CheckKeyValuePairIsValid(key, obj);
+ _proxy[key] = obj;
}
-- (void)removeObjectForKey:(NSString *)aKey {
- if (_call.state == GRXWriterStateNotStarted) {
- NSString *normalizedKey = NormalizeKey(aKey);
- if (normalizedKey) {
- [_proxy removeObjectForKey:normalizedKey];
- } else {
- [NSException raise:@"Invalid key" format:@"Key %@ contains illegal characters", aKey];
- }
- } else {
- [NSException raise:@"Invalid modification"
- format:@"Cannot modify request metadata after call is started"];
- }
+- (void)removeObjectForKey:(NSString *)key {
+ [self checkCallIsNotStarted];
+ [_proxy removeObjectForKey:NormalizeKey(key)];
}
- (void)removeAllObjects {
- if (_call.state == GRXWriterStateNotStarted) {
- [_proxy removeAllObjects];
- } else {
- [NSException raise:@"Invalid modification"
- format:@"Cannot modify request metadata after call is started"];
- }
+ [self checkCallIsNotStarted];
+ [_proxy removeAllObjects];
}
// TODO(jcanizales): Just forward all invocations?