Skip to content

Instantly share code, notes, and snippets.

@yamingd
Created October 26, 2014 02:37
Show Gist options
  • Save yamingd/7ced3e49ed120c591382 to your computer and use it in GitHub Desktop.
Save yamingd/7ced3e49ed120c591382 to your computer and use it in GitHub Desktop.
iOS Protobuf Wrapper
//
// TSAppResponse.hh
// k12
//
// Created by Yaming on 10/25/14.
// Copyright (c) 2014 All rights reserved.
//
#import <Foundation/Foundation.h>
#import "TSProtocolBufferWrapper.hh"
@interface TSAppResponse : TSProtocolBufferWrapper
@property (nonatomic,strong) NSString* msg;
@property (nonatomic,strong) NSString* sessionId;
@property (nonatomic,strong) NSString* version;
@property (nonatomic,strong) NSNumber* code;
@property (nonatomic,strong) NSNumber* total;
@property (readonly,nonatomic,strong) NSArray* data;
@property (readonly,nonatomic,strong) NSArray* errors;
@property (readonly,nonatomic,strong) NSData *protocolData;
-(NSArray*) parseData:(Class)type;
@end
//
// TSAppResponse.m
// k12
//
// Created by Yaming on 10/25/14.
// Copyright (c) 2014. All rights reserved.
//
#import "TSAppResponse.hh"
#import "PAppResponse.pb.hh"
@interface TSAppResponse ()
@property (nonatomic,strong) NSArray* data;
@property (nonatomic,strong) NSArray* errors;
@property (nonatomic,strong) NSData *protocolData;
@end
@implementation TSAppResponse
-(instancetype) initWithProtocolData:(NSData*) data {
return [self initWithData:data];
}
-(NSData*) getProtocolData {
return self.protocolData;
}
-(instancetype) initWithData:(NSData*) data {
if(self = [super init]) {
// c++
PAppResponse* resp = [self deserialize:data];
const std::string msg = resp->msg();
const std::string sessionId = resp->sessionid();
const std::string version = resp->version();
const uint32_t code = resp->code();
const uint32_t total = resp->total();
NSMutableArray *errors= [[NSMutableArray alloc] init];
for (int i=0; i<resp->errors_size(); i++) {
const std::string e = resp->errors(i);
[errors addObject:[self cppStringToObjc:e]];
}
NSMutableArray *dslist= [[NSMutableArray alloc] init];
for (int i=0; i<resp->data_size(); i++) {
const std::string ds = resp->data(i);
[dslist addObject:[self cppStringToObjcData:ds]];
}
// c++->objective C
self.protocolData = data;
self.msg = [self cppStringToObjc:msg];
self.sessionId = [self cppStringToObjc:sessionId];
self.version = [self cppStringToObjc:version];
self.code = [self cppUInt32ToNSNumber:code];
self.total = [self cppUInt32ToNSNumber:total];
self.errors = errors;
self.data = dslist;
}
return self;
}
-(NSArray*) parseData:(Class)type{
NSMutableArray *dslist= [[NSMutableArray alloc] init];
for (NSData* item in self.data) {
id obj = [[type alloc] initWithData:item];
[dslist addObject:obj];
}
return dslist;
}
#pragma mark private
-(const std::string) serializedProtocolBufferAsString {
PAppResponse *message = new PAppResponse;
// objective c->c++
const std::string msg = [self objcStringToCpp:self.msg];
const std::string sessionId = [self objcStringToCpp:self.sessionId];
const std::string version = [self objcStringToCpp:self.version];
const uint32_t code = [self objcNumberToCppUInt32:self.code];
const uint32_t total = [self objcNumberToCppUInt32:self.total];
for (NSString* error in self.errors) {
message->add_errors([self objcStringToCpp:error]);
}
for (NSData* item in self.data) {
message->add_data([self objcDataToCppString:item]);
}
// c++->protocol buffer
message->set_msg(msg);
message->set_sessionid(sessionId);
message->set_version(version);
message->set_code(code);
message->set_total(total);
std::string ps = message->SerializeAsString();
return ps;
}
#pragma mark private methods
- (PAppResponse *)deserialize:(NSData *)data {
int len = [data length];
char raw[len];
PAppResponse *resp = new PAppResponse;
[data getBytes:raw length:len];
resp->ParseFromArray(raw, len);
return resp;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment