TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | Custom |
ReleasedLast Release | Dec 2014 |
Maintained by Unclaimed.
Objective-C code generator for Pdef compiler and Objective-C implementation of descriptors, JSON format and HTTP RPC.
Code generator: Install the code generator as a python package:
$ pip install pdef-objc
Or download the release,
unzip it and in the generator
directory run:
$ python setup.py install
Objective-C package via CocoaPods:
pod "Pdef"
Pass a pdef package path or a url to the compiler:
$ pdefc generate-objc https://raw.github.com/pdef/pdef/1.2/example/world.yaml \
--out Generated
Map namespaces to class prefixes with the --prefix
argument:
$ pdefc generate-objc https://raw.github.com/pdef/pdef/1.2/example/world.yaml \
--prefix world.space:SP
--prefix world:WL
--out Generated
Generated messages implement the isEqual
, hash
methods, the NSCopying
and NSCoding
protocols and support merging. The copyWithZone
method returns a message deep copy.
The messages are not thread-safe. The examples are based on
the pdef example package.
Human *human = [[Human alloc]init];
human.id = 1;
human.name = @"John";
human.sex = Sex_MALE;
human.continent = ContinentName_EUROPE;
Human *copy = [human copy];
assert([human isEqual:copy]);
Messages support merging which deep copies fields from a source message to a destination one:
Human *human = [[Human alloc]init];
human.id = 1;
human.name = @"John";
human.sex = Sex_MALE;
human.continent = ContinentName_EUROPE;
Human *another [[Human alloc]init];
[another merge:human];
assert([another isEqual:human]);
Messages use unboxed types for primitives and bool flags to indicate when a field is set or empty.
// Primitive fields return default values, has{FieldName} returns NO.
Human *human = [[Human alloc]init];
assert(human.id == 0);
assert([human hasId] == NO);
// When a field is set has{FieldName} returns YES.
human.id = 123;
assert([human hasId] == YES);
// Execute a special clear method to clear a field.
[human clearId];
JSON serialization is based on NSJSONSerialization
with the additional support
to serialize/deserialize primitives as root JSON objects (not only objects and arrays).
// To a JSON-compatible dictionary.
NSDictionary *dict = [human toDictionary];
// To JSON data.
NSError *error = nil;
NSData *data = [human toJsonError:&error];
Parsing:
// From a JSON-compatible dictionary, supports polymorphic messages.
NSDictionary *dict = getJsonDictionary();
Human *human = [Human messageWithDictionary:dict];
// From JSON data, supports polymorphic messages.
Human *human1 = [Human messageWithData:data error:&error];
User PDJsonFormat
to read/write other pdef data types:
// Serialize a list of ints.
PDDataTypeDescriptor *listd = [PDDescriptors listWithElement:[PDDescriptors int32]];
NSArray *array = @[@1, @2, @3];
NSError *error = nil;
NSString *json = [PDJsonFormat writeString:array descriptor:listd error:&error];
// Parse a list of ints.
NSArray *parsed = [PDJsonFormat readString:array descriptor:listd error:&error];
RPC client is based on AFNetworking, the implementation is thread-safe.
// Create an RPC client as an invocation handler.
PDRpcClient *handler = [[PDRpcClient alloc]
initWithDescriptor:WorldDescriptor()
baseUrl:@"http://example.com/world"];
// Create a generated protocol client and pass the handler to it.
World *world = [[WorldClient alloc] initWithHandler:client];
// Execute a void remote method.
[world switchDayNightCallback:^(id result, NSError *error) {
NSLog(@"Switched the light");
}];
// Execute a remote method with result.
[[world humans] allLimit:10 offset:0 callback:^(id result, NSError *error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
} else {
NSArray *humans = result;
for (Human *h in humans) {
NSLog(@"%@", h.name);
}
}
}];
Initialize the PDRpcClient
with a custom NSURLSession
or AFURLSessionManager
when you need custom headers, or subclass it.
Remote application errors are passed as NSErrors
with the PDefErrorDomain
domain
and the PDRpcException
code, the exception message is put in the user dict
under the PDRpcExceptionKey
key.
// Get a remote exception.
[[world humans] allLimit:10 offset:0 callback:^(id result, NSError *error) {
if ([error.domain isEqualToString:PDefErrorDomain] && error.code == PDRpcException) {
PDMessage *exception = [error.userInfo objectForKey:PDRpcExceptionKey];
}
}];
// Or a simplified version.
[[world humans] allLimit:10 offset:0 callback:^(id result, NSError *error) {
if (PDIsRpcError(error)) {
PDMessage *exception = PDGetRpcException(error);
}
}];
Objective-C does not have a server implementation.
Copyright: 2013 Ivan Korobkov [email protected]
Licensed 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.