Commit 449ed7e9 authored by Miro Knejp's avatar Miro Knejp

Generate convenience initializers for Objective-C records

This provides a consistent pair of initializers for all records:
- (nonnull instancetype)initWithArg:(A*)arg;
+ (nonnull instancetype)myRecordWithArg:(A*)arg;

The static convenience initializer is only created for records *without*
the +o extension. This also applies to records with no members. Now even
empty records have a designated initializer.
parent 708d6218
......@@ -4,7 +4,8 @@
#import <Foundation/Foundation.h>
@interface TXSItemList : NSObject
- (nonnull id)initWithItems:(nonnull NSArray *)items;
- (nonnull instancetype)initWithItems:(nonnull NSArray *)items;
+ (nonnull instancetype)itemListWithItems:(nonnull NSArray *)items;
@property (nonatomic, readonly, nonnull) NSArray * items;
......
......@@ -6,7 +6,7 @@
@implementation TXSItemList
- (id)initWithItems:(nonnull NSArray *)items
- (nonnull instancetype)initWithItems:(nonnull NSArray *)items
{
if (self = [super init]) {
_items = items;
......@@ -14,4 +14,9 @@
return self;
}
+ (nonnull instancetype)itemListWithItems:(nonnull NSArray *)items
{
return [[self alloc] initWithItems:items];
}
@end
......@@ -191,19 +191,21 @@ class ObjcGenerator(spec: Spec) extends Generator(spec) {
case _ => false
}
val firstInitializerArg = if(r.fields.isEmpty) "" else IdentStyle.camelUpper("with_" + r.fields.head.ident.name)
writeObjcFile(marshal.headerName(objcName), origin, refs.header, w => {
writeDoc(w, doc)
w.wl(s"@interface $self : NSObject")
if (!r.fields.isEmpty) {
val head = r.fields.head
val skipFirst = SkipFirst()
val first = if(r.fields.isEmpty) "" else IdentStyle.camelUpper("with_" + r.fields.head.ident.name)
val decl = s"- (nonnull id)init$first"
def writeInitializer(sign: String, prefix: String) {
val decl = s"$sign (nonnull instancetype)$prefix$firstInitializerArg"
writeAlignedObjcCall(w, decl, r.fields, "", f => (idObjc.field(f.ident), s"(${marshal.paramType(f.ty)})${idObjc.local(f.ident)}"))
w.wl(";")
}
writeInitializer("-", "init")
if (!r.ext.objc) writeInitializer("+", IdentStyle.camelLower(objcName))
for (f <- r.fields) {
w.wl
writeDoc(w, f.doc)
......@@ -234,22 +236,31 @@ class ObjcGenerator(spec: Spec) extends Generator(spec) {
w.wl(s"@implementation $self")
w.wl
// Constructor from all fields (not copying)
if (!r.fields.isEmpty) {
val head = r.fields.head
val skipFirst = SkipFirst()
val decl = s"- (id)${idObjc.method("init_with_" + head.ident.name)}"
val init = s"- (nonnull instancetype)init$firstInitializerArg"
writeAlignedObjcCall(w, init, r.fields, "", f => (idObjc.field(f.ident), s"(${marshal.paramType(f.ty)})${idObjc.local(f.ident)}"))
w.wl
w.braced {
w.w("if (self = [super init])").braced {
for (f <- r.fields) {
if (checkMutable(f.ty.resolved))
w.wl(s"_${idObjc.field(f.ident)} = [${idObjc.local(f.ident)} copy];")
else
w.wl(s"_${idObjc.field(f.ident)} = ${idObjc.local(f.ident)};")
}
}
w.wl("return self;")
}
w.wl
// Convenience initializer
if(!r.ext.objc) {
val decl = s"+ (nonnull instancetype)${IdentStyle.camelLower(objcName)}$firstInitializerArg"
writeAlignedObjcCall(w, decl, r.fields, "", f => (idObjc.field(f.ident), s"(${marshal.paramType(f.ty)})${idObjc.local(f.ident)}"))
w.wl
w.braced {
w.w("if (self = [super init])").braced {
for (f <- r.fields) {
if (checkMutable(f.ty.resolved))
w.wl(s"_${idObjc.field(f.ident)} = [${idObjc.local(f.ident)} copy];")
else
w.wl(s"_${idObjc.field(f.ident)} = ${idObjc.local(f.ident)};")
}
}
w.wl("return self;")
val call = s"return [[self alloc] init$firstInitializerArg"
writeAlignedObjcCall(w, call, r.fields, "", f => (idObjc.field(f.ident), s"${idObjc.local(f.ident)}"))
w.wl("];")
}
w.wl
}
......
......@@ -34,3 +34,7 @@ test_helpers = interface +c {
static id_binary(b: binary): binary;
}
empty_record = record {
}
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file generated by Djinni from test.djinni
#pragma once
#include <utility>
struct EmptyRecord final {
};
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file generated by Djinni from test.djinni
package com.dropbox.djinni.test;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
public final class EmptyRecord {
public EmptyRecord(
) {
}
}
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file generated by Djinni from test.djinni
#include "NativeEmptyRecord.hpp" // my header
namespace djinni_generated {
NativeEmptyRecord::NativeEmptyRecord() = default;
NativeEmptyRecord::~NativeEmptyRecord() = default;
auto NativeEmptyRecord::fromCpp(JNIEnv* jniEnv, const CppType& c) -> ::djinni::LocalRef<JniType> {
(void)c; // Suppress warnings in release builds for empty records
const auto& data = ::djinni::JniClass<NativeEmptyRecord>::get();
auto r = ::djinni::LocalRef<JniType>{jniEnv->NewObject(data.clazz.get(), data.jconstructor)};
::djinni::jniExceptionCheck(jniEnv);
return r;
}
auto NativeEmptyRecord::toCpp(JNIEnv* jniEnv, JniType j) -> CppType {
::djinni::JniLocalScope jscope(jniEnv, 1);
assert(j != nullptr);
(void)j; // Suppress warnings in release builds for empty records
return {};
}
} // namespace djinni_generated
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file generated by Djinni from test.djinni
#pragma once
#include "djinni_support.hpp"
#include "empty_record.hpp"
namespace djinni_generated {
class NativeEmptyRecord final {
public:
using CppType = ::EmptyRecord;
using JniType = jobject;
using Boxed = NativeEmptyRecord;
~NativeEmptyRecord();
static CppType toCpp(JNIEnv* jniEnv, JniType j);
static ::djinni::LocalRef<JniType> fromCpp(JNIEnv* jniEnv, const CppType& c);
private:
NativeEmptyRecord();
friend ::djinni::JniClass<NativeEmptyRecord>;
const ::djinni::GlobalRef<jclass> clazz { ::djinni::jniFindClass("com/dropbox/djinni/test/EmptyRecord") };
const jmethodID jconstructor { ::djinni::jniGetMethodID(clazz.get(), "<init>", "()V") };
};
} // namespace djinni_generated
......@@ -4,20 +4,34 @@
#import <Foundation/Foundation.h>
@interface DBAssortedPrimitives : NSObject
- (nonnull id)initWithB:(BOOL)b
eight:(int8_t)eight
sixteen:(int16_t)sixteen
thirtytwo:(int32_t)thirtytwo
sixtyfour:(int64_t)sixtyfour
fthirtytwo:(float)fthirtytwo
fsixtyfour:(double)fsixtyfour
oB:(nullable NSNumber *)oB
oEight:(nullable NSNumber *)oEight
oSixteen:(nullable NSNumber *)oSixteen
oThirtytwo:(nullable NSNumber *)oThirtytwo
oSixtyfour:(nullable NSNumber *)oSixtyfour
oFthirtytwo:(nullable NSNumber *)oFthirtytwo
oFsixtyfour:(nullable NSNumber *)oFsixtyfour;
- (nonnull instancetype)initWithB:(BOOL)b
eight:(int8_t)eight
sixteen:(int16_t)sixteen
thirtytwo:(int32_t)thirtytwo
sixtyfour:(int64_t)sixtyfour
fthirtytwo:(float)fthirtytwo
fsixtyfour:(double)fsixtyfour
oB:(nullable NSNumber *)oB
oEight:(nullable NSNumber *)oEight
oSixteen:(nullable NSNumber *)oSixteen
oThirtytwo:(nullable NSNumber *)oThirtytwo
oSixtyfour:(nullable NSNumber *)oSixtyfour
oFthirtytwo:(nullable NSNumber *)oFthirtytwo
oFsixtyfour:(nullable NSNumber *)oFsixtyfour;
+ (nonnull instancetype)assortedPrimitivesWithB:(BOOL)b
eight:(int8_t)eight
sixteen:(int16_t)sixteen
thirtytwo:(int32_t)thirtytwo
sixtyfour:(int64_t)sixtyfour
fthirtytwo:(float)fthirtytwo
fsixtyfour:(double)fsixtyfour
oB:(nullable NSNumber *)oB
oEight:(nullable NSNumber *)oEight
oSixteen:(nullable NSNumber *)oSixteen
oThirtytwo:(nullable NSNumber *)oThirtytwo
oSixtyfour:(nullable NSNumber *)oSixtyfour
oFthirtytwo:(nullable NSNumber *)oFthirtytwo
oFsixtyfour:(nullable NSNumber *)oFsixtyfour;
@property (nonatomic, readonly) BOOL b;
......
......@@ -6,20 +6,20 @@
@implementation DBAssortedPrimitives
- (id)initWithB:(BOOL)b
eight:(int8_t)eight
sixteen:(int16_t)sixteen
thirtytwo:(int32_t)thirtytwo
sixtyfour:(int64_t)sixtyfour
fthirtytwo:(float)fthirtytwo
fsixtyfour:(double)fsixtyfour
oB:(nullable NSNumber *)oB
oEight:(nullable NSNumber *)oEight
oSixteen:(nullable NSNumber *)oSixteen
oThirtytwo:(nullable NSNumber *)oThirtytwo
oSixtyfour:(nullable NSNumber *)oSixtyfour
oFthirtytwo:(nullable NSNumber *)oFthirtytwo
oFsixtyfour:(nullable NSNumber *)oFsixtyfour
- (nonnull instancetype)initWithB:(BOOL)b
eight:(int8_t)eight
sixteen:(int16_t)sixteen
thirtytwo:(int32_t)thirtytwo
sixtyfour:(int64_t)sixtyfour
fthirtytwo:(float)fthirtytwo
fsixtyfour:(double)fsixtyfour
oB:(nullable NSNumber *)oB
oEight:(nullable NSNumber *)oEight
oSixteen:(nullable NSNumber *)oSixteen
oThirtytwo:(nullable NSNumber *)oThirtytwo
oSixtyfour:(nullable NSNumber *)oSixtyfour
oFthirtytwo:(nullable NSNumber *)oFthirtytwo
oFsixtyfour:(nullable NSNumber *)oFsixtyfour
{
if (self = [super init]) {
_b = b;
......@@ -40,6 +40,37 @@
return self;
}
+ (nonnull instancetype)assortedPrimitivesWithB:(BOOL)b
eight:(int8_t)eight
sixteen:(int16_t)sixteen
thirtytwo:(int32_t)thirtytwo
sixtyfour:(int64_t)sixtyfour
fthirtytwo:(float)fthirtytwo
fsixtyfour:(double)fsixtyfour
oB:(nullable NSNumber *)oB
oEight:(nullable NSNumber *)oEight
oSixteen:(nullable NSNumber *)oSixteen
oThirtytwo:(nullable NSNumber *)oThirtytwo
oSixtyfour:(nullable NSNumber *)oSixtyfour
oFthirtytwo:(nullable NSNumber *)oFthirtytwo
oFsixtyfour:(nullable NSNumber *)oFsixtyfour
{
return [[self alloc] initWithB:b
eight:eight
sixteen:sixteen
thirtytwo:thirtytwo
sixtyfour:sixtyfour
fthirtytwo:fthirtytwo
fsixtyfour:fsixtyfour
oB:oB
oEight:oEight
oSixteen:oSixteen
oThirtytwo:oThirtytwo
oSixtyfour:oSixtyfour
oFthirtytwo:oFthirtytwo
oFsixtyfour:oFsixtyfour];
}
- (BOOL)isEqual:(id)other
{
if (![other isKindOfClass:[DBAssortedPrimitives class]]) {
......
......@@ -4,9 +4,12 @@
#import <Foundation/Foundation.h>
@interface DBClientReturnedRecord : NSObject
- (nonnull id)initWithRecordId:(int64_t)recordId
content:(nonnull NSString *)content
misc:(nullable NSString *)misc;
- (nonnull instancetype)initWithRecordId:(int64_t)recordId
content:(nonnull NSString *)content
misc:(nullable NSString *)misc;
+ (nonnull instancetype)clientReturnedRecordWithRecordId:(int64_t)recordId
content:(nonnull NSString *)content
misc:(nullable NSString *)misc;
@property (nonatomic, readonly) int64_t recordId;
......
......@@ -6,9 +6,9 @@
@implementation DBClientReturnedRecord
- (id)initWithRecordId:(int64_t)recordId
content:(nonnull NSString *)content
misc:(nullable NSString *)misc
- (nonnull instancetype)initWithRecordId:(int64_t)recordId
content:(nonnull NSString *)content
misc:(nullable NSString *)misc
{
if (self = [super init]) {
_recordId = recordId;
......@@ -18,4 +18,13 @@
return self;
}
+ (nonnull instancetype)clientReturnedRecordWithRecordId:(int64_t)recordId
content:(nonnull NSString *)content
misc:(nullable NSString *)misc
{
return [[self alloc] initWithRecordId:recordId
content:content
misc:misc];
}
@end
......@@ -5,8 +5,10 @@
#import <Foundation/Foundation.h>
@interface DBConstants : NSObject
- (nonnull id)initWithSomeInteger:(int32_t)someInteger
someString:(nonnull NSString *)someString;
- (nonnull instancetype)initWithSomeInteger:(int32_t)someInteger
someString:(nonnull NSString *)someString;
+ (nonnull instancetype)constantsWithSomeInteger:(int32_t)someInteger
someString:(nonnull NSString *)someString;
@property (nonatomic, readonly) int32_t someInteger;
......
......@@ -31,8 +31,8 @@ DBConstants * __nonnull const DBConstantsObjectConstant = [[DBConstants alloc] i
@implementation DBConstants
- (id)initWithSomeInteger:(int32_t)someInteger
someString:(nonnull NSString *)someString
- (nonnull instancetype)initWithSomeInteger:(int32_t)someInteger
someString:(nonnull NSString *)someString
{
if (self = [super init]) {
_someInteger = someInteger;
......@@ -41,4 +41,11 @@ DBConstants * __nonnull const DBConstantsObjectConstant = [[DBConstants alloc] i
return self;
}
+ (nonnull instancetype)constantsWithSomeInteger:(int32_t)someInteger
someString:(nonnull NSString *)someString
{
return [[self alloc] initWithSomeInteger:someInteger
someString:someString];
}
@end
......@@ -4,7 +4,8 @@
#import <Foundation/Foundation.h>
@interface DBDateRecord : NSObject
- (nonnull id)initWithCreatedAt:(nonnull NSDate *)createdAt;
- (nonnull instancetype)initWithCreatedAt:(nonnull NSDate *)createdAt;
+ (nonnull instancetype)dateRecordWithCreatedAt:(nonnull NSDate *)createdAt;
@property (nonatomic, readonly, nonnull) NSDate * createdAt;
......
......@@ -6,7 +6,7 @@
@implementation DBDateRecord
- (id)initWithCreatedAt:(nonnull NSDate *)createdAt
- (nonnull instancetype)initWithCreatedAt:(nonnull NSDate *)createdAt
{
if (self = [super init]) {
_createdAt = createdAt;
......@@ -14,6 +14,11 @@
return self;
}
+ (nonnull instancetype)dateRecordWithCreatedAt:(nonnull NSDate *)createdAt
{
return [[self alloc] initWithCreatedAt:createdAt];
}
- (BOOL)isEqual:(id)other
{
if (![other isKindOfClass:[DBDateRecord class]]) {
......
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file generated by Djinni from test.djinni
#import "DBEmptyRecord.h"
#include "empty_record.hpp"
static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for this file");
@class DBEmptyRecord;
namespace djinni_generated {
struct EmptyRecord
{
using CppType = ::EmptyRecord;
using ObjcType = DBEmptyRecord*;
using Boxed = EmptyRecord;
static CppType toCpp(ObjcType objc);
static ObjcType fromCpp(const CppType& cpp);
};
} // namespace djinni_generated
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file generated by Djinni from test.djinni
#import "DBEmptyRecord+Private.h"
#include <cassert>
namespace djinni_generated {
auto EmptyRecord::toCpp(ObjcType obj) -> CppType
{
assert(obj);
(void)obj; // Suppress warnings in relase builds for empty records
return {};
}
auto EmptyRecord::fromCpp(const CppType& cpp) -> ObjcType
{
(void)cpp; // Suppress warnings in relase builds for empty records
return [[DBEmptyRecord alloc] init];
}
} // namespace djinni_generated
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file generated by Djinni from test.djinni
#import <Foundation/Foundation.h>
@interface DBEmptyRecord : NSObject
- (nonnull instancetype)init;
+ (nonnull instancetype)emptyRecord;
@end
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file generated by Djinni from test.djinni
#import "DBEmptyRecord.h"
@implementation DBEmptyRecord
- (nonnull instancetype)init
{
if (self = [super init]) {
}
return self;
}
+ (nonnull instancetype)emptyRecord
{
return [[self alloc] init];
}
@end
......@@ -7,8 +7,10 @@
/** This file tests YAML dumped by Djinni can be parsed back in */
@interface DBExternRecordWithDerivings : NSObject
- (nonnull id)initWithMember:(nonnull DBRecordWithDerivings *)member
e:(DBColor)e;
- (nonnull instancetype)initWithMember:(nonnull DBRecordWithDerivings *)member
e:(DBColor)e;
+ (nonnull instancetype)externRecordWithDerivingsWithMember:(nonnull DBRecordWithDerivings *)member
e:(DBColor)e;
@property (nonatomic, readonly, nonnull) DBRecordWithDerivings * member;
......
......@@ -6,8 +6,8 @@
@implementation DBExternRecordWithDerivings
- (id)initWithMember:(nonnull DBRecordWithDerivings *)member
e:(DBColor)e
- (nonnull instancetype)initWithMember:(nonnull DBRecordWithDerivings *)member
e:(DBColor)e
{
if (self = [super init]) {
_member = member;
......@@ -16,6 +16,13 @@
return self;
}
+ (nonnull instancetype)externRecordWithDerivingsWithMember:(nonnull DBRecordWithDerivings *)member
e:(DBColor)e
{
return [[self alloc] initWithMember:member
e:e];
}
- (BOOL)isEqual:(id)other
{
if (![other isKindOfClass:[DBExternRecordWithDerivings class]]) {
......
......@@ -4,7 +4,8 @@
#import <Foundation/Foundation.h>
@interface DBMapDateRecord : NSObject
- (nonnull id)initWithDatesById:(nonnull NSDictionary *)datesById;
- (nonnull instancetype)initWithDatesById:(nonnull NSDictionary *)datesById;
+ (nonnull instancetype)mapDateRecordWithDatesById:(nonnull NSDictionary *)datesById;
@property (nonatomic, readonly, nonnull) NSDictionary * datesById;
......
......@@ -6,7 +6,7 @@
@implementation DBMapDateRecord
- (id)initWithDatesById:(nonnull NSDictionary *)datesById
- (nonnull instancetype)initWithDatesById:(nonnull NSDictionary *)datesById
{
if (self = [super init]) {
_datesById = datesById;
......@@ -14,4 +14,9 @@
return self;
}
+ (nonnull instancetype)mapDateRecordWithDatesById:(nonnull NSDictionary *)datesById
{
return [[self alloc] initWithDatesById:datesById];
}
@end
......@@ -4,7 +4,8 @@
#import <Foundation/Foundation.h>
@interface DBMapListRecord : NSObject
- (nonnull id)initWithMapList:(nonnull NSArray *)mapList;
- (nonnull instancetype)initWithMapList:(nonnull NSArray *)mapList;
+ (nonnull instancetype)mapListRecordWithMapList:(nonnull NSArray *)mapList;
@property (nonatomic, readonly, nonnull) NSArray * mapList;
......
......@@ -6,7 +6,7 @@
@implementation DBMapListRecord
- (id)initWithMapList:(nonnull NSArray *)mapList
- (nonnull instancetype)initWithMapList:(nonnull NSArray *)mapList
{
if (self = [super init]) {
_mapList = mapList;
......@@ -14,4 +14,9 @@
return self;
}
+ (nonnull instancetype)mapListRecordWithMapList:(nonnull NSArray *)mapList
{
return [[self alloc] initWithMapList:mapList];
}
@end
......@@ -4,8 +4,10 @@
#import <Foundation/Foundation.h>
@interface DBMapRecord : NSObject
- (nonnull id)initWithMap:(nonnull NSDictionary *)map
imap:(nonnull NSDictionary *)imap;
- (nonnull instancetype)initWithMap:(nonnull NSDictionary *)map
imap:(nonnull NSDictionary *)imap;
+ (nonnull instancetype)mapRecordWithMap:(nonnull NSDictionary *)map
imap:(nonnull NSDictionary *)imap;
@property (nonatomic, readonly, nonnull) NSDictionary * map;
......
......@@ -6,8 +6,8 @@
@implementation DBMapRecord
- (id)initWithMap:(nonnull NSDictionary *)map
imap:(nonnull NSDictionary *)imap
- (nonnull instancetype)initWithMap:(nonnull NSDictionary *)map
imap:(nonnull NSDictionary *)imap
{
if (self = [super init]) {
_map = map;
......@@ -16,4 +16,11 @@
return self;
}
+ (nonnull instancetype)mapRecordWithMap:(nonnull NSDictionary *)map
imap:(nonnull NSDictionary *)imap
{
return [[self alloc] initWithMap:map
imap:imap];
}
@end
......@@ -4,7 +4,8 @@
#import <Foundation/Foundation.h>
@interface DBNestedCollection : NSObject
- (nonnull id)initWithSetList:(nonnull NSArray *)setList;
- (nonnull instancetype)initWithSetList:(nonnull NSArray *)setList;
+ (nonnull instancetype)nestedCollectionWithSetList:(nonnull NSArray *)setList;
@property (nonatomic, readonly, nonnull) NSArray * setList;
......
......@@ -6,7 +6,7 @@
@implementation DBNestedCollection
- (id)initWithSetList:(nonnull NSArray *)setList
- (nonnull instancetype)initWithSetList:(nonnull NSArray *)setList
{
if (self = [super init]) {
_setList = setList;
......@@ -14,4 +14,9 @@
return self;
}
+ (nonnull instancetype)nestedCollectionWithSetList:(nonnull NSArray *)setList
{
return [[self alloc] initWithSetList:setList];
}
@end
......@@ -4,7 +4,8 @@
#import <Foundation/Foundation.h>
@interface DBPrimitiveList : NSObject
- (nonnull id)initWithList:(nonnull NSArray *)list;
- (nonnull instancetype)initWithList:(nonnull NSArray *)list;
+ (nonnull instancetype)primitiveListWithList:(nonnull NSArray *)list;
@property (nonatomic, readonly, nonnull) NSArray * list;
......
......@@ -6,7 +6,7 @@
@implementation DBPrimitiveList
- (id)initWithList:(nonnull NSArray *)list
- (nonnull instancetype)initWithList:(nonnull NSArray *)list
{
if (self = [super init]) {
_list = list;
......@@ -14,4 +14,9 @@
return self;
}
+ (nonnull instancetype)primitiveListWithList:(nonnull NSArray *)list
{
return [[self alloc] initWithList:list];
}
@end
......@@ -4,8 +4,10 @@
#import <Foundation/Foundation.h>
@interface DBRecordWithDerivings : NSObject
- (nonnull id)initWithKey1:(int32_t)key1
key2:(nonnull NSString *)key2;
- (nonnull instancetype)initWithKey1:(int32_t)key1
key2:(nonnull NSString *)key2;
+ (nonnull instancetype)recordWithDerivingsWithKey1:(int32_t)key1
key2:(nonnull NSString *)key2;
@property (nonatomic, readonly) int32_t key1;
......
......@@ -6,8 +6,8 @@
@implementation DBRecordWithDerivings
- (id)initWithKey1:(int32_t)key1
key2:(nonnull NSString *)key2
- (nonnull instancetype)initWithKey1:(int32_t)key1
key2:(nonnull NSString *)key2
{
if (self = [super init]) {
_key1 = key1;
......@@ -16,6 +16,13 @@
return self;
}
+ (nonnull instancetype)recordWithDerivingsWithKey1:(int32_t)key1
key2:(nonnull NSString *)key2
{
return [[self alloc] initWithKey1:key1
key2:key2];
}
- (BOOL)isEqual:(id)other
{
if (![other isKindOfClass:[DBRecordWithDerivings class]]) {
......
......@@ -4,7 +4,8 @@
#import <Foundation/Foundation.h>
@interface DBRecordWithDurationAndDerivings : NSObject
- (nonnull id)initWithDt:(NSTimeInterval)dt;
- (nonnull instancetype)initWithDt:(NSTimeInterval)dt;
+ (nonnull instancetype)recordWithDurationAndDerivingsWithDt:(NSTimeInterval)dt;
@property (nonatomic, readonly) NSTimeInterval dt;
......
......@@ -6,7 +6,7 @@
@implementation DBRecordWithDurationAndDerivings
- (id)initWithDt:(NSTimeInterval)dt
- (nonnull instancetype)initWithDt:(NSTimeInterval)dt
{
if (self = [super init]) {
_dt = dt;
......@@ -14,6 +14,11 @@
return self;
}
+ (nonnull instancetype)recordWithDurationAndDerivingsWithDt:(NSTimeInterval)dt
{
return [[self alloc] initWithDt:dt];
}
- (BOOL)isEqual:(id)other
{
if (![other isKindOfClass:[DBRecordWithDurationAndDerivings class]]) {
......
......@@ -5,8 +5,10 @@
#import <Foundation/Foundation.h>
@interface DBRecordWithNestedDerivings : NSObject
- (nonnull id)initWithKey:(int32_t)key
rec:(nonnull DBRecordWithDerivings *)rec;
- (nonnull instancetype)initWithKey:(int32_t)key
rec:(nonnull DBRecordWithDerivings *)rec;
+ (nonnull instancetype)recordWithNestedDerivingsWithKey:(int32_t)key
rec:(nonnull DBRecordWithDerivings *)rec;
@property (nonatomic, readonly) int32_t key;
......
......@@ -6,8 +6,8 @@
@implementation DBRecordWithNestedDerivings
- (id)initWithKey:(int32_t)key
rec:(nonnull DBRecordWithDerivings *)rec
- (nonnull instancetype)initWithKey:(int32_t)key
rec:(nonnull DBRecordWithDerivings *)rec
{
if (self = [super init]) {
_key = key;
......@@ -16,6 +16,13 @@
return self;
}
+ (nonnull instancetype)recordWithNestedDerivingsWithKey:(int32_t)key
rec:(nonnull DBRecordWithDerivings *)rec
{
return [[self alloc] initWithKey:key
rec:rec];
}
- (BOOL)isEqual:(id)other
{
if (![other isKindOfClass:[DBRecordWithNestedDerivings class]]) {
......
......@@ -4,8 +4,10 @@
#import <Foundation/Foundation.h>
@interface DBSetRecord : NSObject
- (nonnull id)initWithSet:(nonnull NSSet *)set
iset:(nonnull NSSet *)iset;
- (nonnull instancetype)initWithSet:(nonnull NSSet *)set
iset:(nonnull NSSet *)iset;
+ (nonnull instancetype)setRecordWithSet:(nonnull NSSet *)set
iset:(nonnull NSSet *)iset;
@property (nonatomic, readonly, nonnull) NSSet * set;
......
......@@ -6,8 +6,8 @@
@implementation DBSetRecord
- (id)initWithSet:(nonnull NSSet *)set
iset:(nonnull NSSet *)iset
- (nonnull instancetype)initWithSet:(nonnull NSSet *)set
iset:(nonnull NSSet *)iset
{
if (self = [super init]) {
_set = set;
......@@ -16,4 +16,11 @@
return self;
}
+ (nonnull instancetype)setRecordWithSet:(nonnull NSSet *)set
iset:(nonnull NSSet *)iset
{
return [[self alloc] initWithSet:set
iset:iset];
}
@end
......@@ -11,6 +11,7 @@ djinni-output-temp/cpp/constants_interface.cpp
djinni-output-temp/cpp/assorted_primitives.hpp
djinni-output-temp/cpp/assorted_primitives.cpp
djinni-output-temp/cpp/test_helpers.hpp
djinni-output-temp/cpp/empty_record.hpp
djinni-output-temp/cpp/token.hpp
djinni-output-temp/cpp/color.hpp
djinni-output-temp/cpp/client_returned_record.hpp
......@@ -33,6 +34,7 @@ djinni-output-temp/java/Constants.java
djinni-output-temp/java/ConstantsInterface.java
djinni-output-temp/java/AssortedPrimitives.java
djinni-output-temp/java/TestHelpers.java
djinni-output-temp/java/EmptyRecord.java
djinni-output-temp/java/Token.java
djinni-output-temp/java/Color.java
djinni-output-temp/java/ClientReturnedRecord.java
......@@ -61,6 +63,8 @@ djinni-output-temp/jni/NativeAssortedPrimitives.hpp
djinni-output-temp/jni/NativeAssortedPrimitives.cpp
djinni-output-temp/jni/NativeTestHelpers.hpp
djinni-output-temp/jni/NativeTestHelpers.cpp
djinni-output-temp/jni/NativeEmptyRecord.hpp
djinni-output-temp/jni/NativeEmptyRecord.cpp
djinni-output-temp/jni/NativeToken.hpp
djinni-output-temp/jni/NativeToken.cpp
djinni-output-temp/jni/NativeColor.hpp
......@@ -98,6 +102,8 @@ djinni-output-temp/objc/DBConstantsInterface.mm
djinni-output-temp/objc/DBAssortedPrimitives.h
djinni-output-temp/objc/DBAssortedPrimitives.mm
djinni-output-temp/objc/DBTestHelpers.h
djinni-output-temp/objc/DBEmptyRecord.h
djinni-output-temp/objc/DBEmptyRecord.mm
djinni-output-temp/objc/DBToken.h
djinni-output-temp/objc/DBColor.h
djinni-output-temp/objc/DBClientReturnedRecord.h
......@@ -134,6 +140,8 @@ djinni-output-temp/objc/DBAssortedPrimitives+Private.h
djinni-output-temp/objc/DBAssortedPrimitives+Private.mm
djinni-output-temp/objc/DBTestHelpers+Private.h
djinni-output-temp/objc/DBTestHelpers+Private.mm
djinni-output-temp/objc/DBEmptyRecord+Private.h
djinni-output-temp/objc/DBEmptyRecord+Private.mm
djinni-output-temp/objc/DBToken+Private.h
djinni-output-temp/objc/DBToken+Private.mm
djinni-output-temp/objc/DBClientReturnedRecord+Private.h
......
......@@ -31,7 +31,7 @@
- (void)testObjcToCpp
{
DBMapRecord *objcMapRecord = [[DBMapRecord alloc] initWithMap:[self getObjcMap] imap:[NSDictionary dictionary]];
DBMapRecord *objcMapRecord = [DBMapRecord mapRecordWithMap:[self getObjcMap] imap:[NSDictionary dictionary]];
MapRecord cppMapRecord = djinni_generated::MapRecord::toCpp(objcMapRecord);
[self checkCppMap:cppMapRecord.map];
}
......@@ -46,7 +46,7 @@
- (void)testObjcToCppEmpty
{
DBMapRecord *objcMapRecord = [[DBMapRecord alloc] initWithMap:[NSDictionary dictionary] imap:[NSDictionary dictionary]];
DBMapRecord *objcMapRecord = [DBMapRecord mapRecordWithMap:[NSDictionary dictionary] imap:[NSDictionary dictionary]];
MapRecord cppMapRecord = djinni_generated::MapRecord::toCpp(objcMapRecord);
auto & cppMap = cppMapRecord.map;
XCTAssertEqual(cppMap.size(), (size_t)0, @"Count 0 expected, actual: %zd", cppMap.size());
......@@ -64,7 +64,7 @@
- (void)testObjcMapListToCpp
{
NSArray *objcMapList = @[ [self getObjcMap] ];
DBMapListRecord *objcMapListRecord = [[DBMapListRecord alloc] initWithMapList:objcMapList];
DBMapListRecord *objcMapListRecord = [DBMapListRecord mapListRecordWithMapList:objcMapList];
auto cppMapListRecord = djinni_generated::MapListRecord::toCpp(objcMapListRecord);
auto & cppMapList = cppMapListRecord.map_list;
XCTAssertEqual(cppMapList.size(), (size_t)1, @"List with 1 map expected, actual no: %zd", cppMapList.size());
......
......@@ -4,7 +4,7 @@
#include "nested_collection.hpp"
static NestedCollection cppNestedCollection { { {u8"String1", u8"String2"}, {u8"StringA", u8"StringB"} } };
static DBNestedCollection *objcNestedCollection = [[DBNestedCollection alloc] initWithSetList:@[
static DBNestedCollection *objcNestedCollection = [DBNestedCollection nestedCollectionWithSetList:@[
[NSSet setWithArray:@[ @"String1", @"String2" ]],
[NSSet setWithArray:@[ @"StringA", @"StringB" ]],
]];
......
......@@ -4,11 +4,7 @@
#import <XCTest/XCTest.h>
static PrimitiveList cppPrimitiveList { { 1, 2, 3 } };
static DBPrimitiveList *objcPrimitiveList = [[DBPrimitiveList alloc] initWithList:
[[NSArray alloc] initWithObjects:[NSNumber numberWithLongLong:1],
[NSNumber numberWithLongLong:2],
[NSNumber numberWithLongLong:3],
nil]];
static DBPrimitiveList *objcPrimitiveList = [DBPrimitiveList primitiveListWithList:@[@1, @2, @3]];
@interface DBPrimitiveListTests : XCTestCase
......
......@@ -4,6 +4,7 @@
#import "DBAssortedPrimitives.h"
#import "DBTestHelpers.h"
#import "DBEmptyRecord.h"
@interface DBPrimitivesTests : XCTestCase
......@@ -23,24 +24,28 @@
- (void)testPrimitives
{
DBAssortedPrimitives * p = [[DBAssortedPrimitives alloc]
initWithB:YES
eight:(int8_t)123
sixteen:(int16_t)20000
thirtytwo:(int32_t)1000000000
sixtyfour:(int64_t)1234567890123456789L
fthirtytwo:(float)1.23
fsixtyfour:1.23
oB:[NSNumber numberWithBool:YES]
oEight:[NSNumber numberWithChar:123]
oSixteen:[NSNumber numberWithShort:20000]
oThirtytwo:[NSNumber numberWithInt:1000000000]
oSixtyfour:[NSNumber numberWithLongLong:1234567890123456789L]
oFthirtytwo:[NSNumber numberWithFloat:(float)123]
oFsixtyfour:[NSNumber numberWithDouble:123]];
DBAssortedPrimitives * p = [DBAssortedPrimitives assortedPrimitivesWithB:YES
eight:(int8_t)123
sixteen:(int16_t)20000
thirtytwo:(int32_t)1000000000
sixtyfour:(int64_t)1234567890123456789L
fthirtytwo:(float)1.23
fsixtyfour:1.23
oB:[NSNumber numberWithBool:YES]
oEight:[NSNumber numberWithChar:123]
oSixteen:[NSNumber numberWithShort:20000]
oThirtytwo:[NSNumber numberWithInt:1000000000]
oSixtyfour:[NSNumber numberWithLongLong:1234567890123456789L]
oFthirtytwo:[NSNumber numberWithFloat:(float)123]
oFsixtyfour:[NSNumber numberWithDouble:123]];
XCTAssertEqualObjects(p, [DBTestHelpers assortedPrimitivesId:p]);
}
- (void)testEmptyRecord
{
(void)[DBEmptyRecord emptyRecord];
}
- (void)testObjcToCppConverter
{
}
......
......@@ -3,14 +3,14 @@
#import <XCTest/XCTest.h>
static DBRecordWithDerivings *record1 = [[DBRecordWithDerivings alloc] initWithKey1:1 key2:@"String1"];
static DBRecordWithDerivings *record1A = [[DBRecordWithDerivings alloc] initWithKey1:1 key2:@"String1"];
static DBRecordWithDerivings *record2 = [[DBRecordWithDerivings alloc] initWithKey1:1 key2:@"String2"];
static DBRecordWithDerivings *record3 = [[DBRecordWithDerivings alloc] initWithKey1:2 key2:@"String1"];
static DBRecordWithDerivings *record1 = [DBRecordWithDerivings recordWithDerivingsWithKey1:1 key2:@"String1"];
static DBRecordWithDerivings *record1A = [DBRecordWithDerivings recordWithDerivingsWithKey1:1 key2:@"String1"];
static DBRecordWithDerivings *record2 = [DBRecordWithDerivings recordWithDerivingsWithKey1:1 key2:@"String2"];
static DBRecordWithDerivings *record3 = [DBRecordWithDerivings recordWithDerivingsWithKey1:2 key2:@"String1"];
static DBRecordWithNestedDerivings *nestedRecord1 = [[DBRecordWithNestedDerivings alloc] initWithKey:1 rec:record1];
static DBRecordWithNestedDerivings *nestedRecord1A = [[DBRecordWithNestedDerivings alloc] initWithKey:1 rec:record1A];
static DBRecordWithNestedDerivings *nestedRecord2 = [[DBRecordWithNestedDerivings alloc] initWithKey:1 rec:record2];
static DBRecordWithNestedDerivings *nestedRecord1 = [DBRecordWithNestedDerivings recordWithNestedDerivingsWithKey:1 rec:record1];
static DBRecordWithNestedDerivings *nestedRecord1A = [DBRecordWithNestedDerivings recordWithNestedDerivingsWithKey:1 rec:record1A];
static DBRecordWithNestedDerivings *nestedRecord2 = [DBRecordWithNestedDerivings recordWithNestedDerivingsWithKey:1 rec:record2];
@interface DBRecordWithDerivingsObjcTests : XCTestCase
......
......@@ -16,8 +16,8 @@
- (DBSetRecord *)getObjcSetRecord
{
NSSet *set = [[NSSet alloc] initWithObjects:@"StringA", @"StringB", @"StringC", nil];
DBSetRecord *objcSetRecord = [[DBSetRecord alloc] initWithSet:set iset:[NSSet set]];
NSSet *set = [NSSet setWithObjects:@"StringA", @"StringB", @"StringC", nil];
DBSetRecord *objcSetRecord = [DBSetRecord setRecordWithSet:set iset:[NSSet set]];
return objcSetRecord;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment