Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested object type definitions #256

Merged
merged 2 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions Examples/Cocoa/Sources/Objective_C/Everything.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "Everything.h"
#import "Everything.h"
#import "Image.h"
#import "Nested.h"
#import "Pin.h"
#import "User.h"

Expand Down Expand Up @@ -598,6 +599,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
unsigned int EverythingDirtyPropertyMapWithObjectValues:1;
unsigned int EverythingDirtyPropertyMapWithOtherModelValues:1;
unsigned int EverythingDirtyPropertyMapWithPrimitiveValues:1;
unsigned int EverythingDirtyPropertyNestedObject:1;
unsigned int EverythingDirtyPropertyNsintegerEnum:1;
unsigned int EverythingDirtyPropertyNsuintegerEnum:1;
unsigned int EverythingDirtyPropertyNumberProp:1;
Expand Down Expand Up @@ -995,6 +997,15 @@ - (instancetype)initWithModelDictionary:(NS_VALID_UNTIL_END_OF_SCOPE NSDictionar
self->_everythingDirtyProperties.EverythingDirtyPropertyMapWithPrimitiveValues = 1;
}
}
{
__unsafe_unretained id value = modelDictionary[@"nested_object"]; // Collection will retain.
if (value != nil) {
if (value != (id)kCFNull) {
self->_nestedObject = [Nested modelObjectWithDictionary:value];
}
self->_everythingDirtyProperties.EverythingDirtyPropertyNestedObject = 1;
}
}
{
__unsafe_unretained id value = modelDictionary[@"nsinteger_enum"]; // Collection will retain.
if (value != nil) {
Expand Down Expand Up @@ -1245,6 +1256,7 @@ - (instancetype)initWithBuilder:(EverythingBuilder *)builder initType:(PlankMode
_mapWithObjectValues = builder.mapWithObjectValues;
_mapWithOtherModelValues = builder.mapWithOtherModelValues;
_mapWithPrimitiveValues = builder.mapWithPrimitiveValues;
_nestedObject = builder.nestedObject;
_nsintegerEnum = builder.nsintegerEnum;
_nsuintegerEnum = builder.nsuintegerEnum;
_numberProp = builder.numberProp;
Expand Down Expand Up @@ -1272,7 +1284,7 @@ - (instancetype)initWithBuilder:(EverythingBuilder *)builder initType:(PlankMode
- (NSString *)debugDescription
{
NSArray<NSString *> *parentDebugDescription = [[super debugDescription] componentsSeparatedByString:@"\n"];
NSMutableArray *descriptionFields = [NSMutableArray arrayWithCapacity:36];
NSMutableArray *descriptionFields = [NSMutableArray arrayWithCapacity:37];
[descriptionFields addObject:parentDebugDescription];
struct EverythingDirtyProperties props = _everythingDirtyProperties;
if (props.EverythingDirtyPropertyArrayProp) {
Expand Down Expand Up @@ -1329,6 +1341,9 @@ - (NSString *)debugDescription
if (props.EverythingDirtyPropertyMapWithPrimitiveValues) {
[descriptionFields addObject:[@"_mapWithPrimitiveValues = " stringByAppendingFormat:@"%@", _mapWithPrimitiveValues]];
}
if (props.EverythingDirtyPropertyNestedObject) {
[descriptionFields addObject:[@"_nestedObject = " stringByAppendingFormat:@"%@", _nestedObject]];
}
if (props.EverythingDirtyPropertyNsintegerEnum) {
[descriptionFields addObject:[@"_nsintegerEnum = " stringByAppendingFormat:@"%@", @(_nsintegerEnum)]];
}
Expand Down Expand Up @@ -1433,6 +1448,7 @@ - (BOOL)isEqualToEverything:(Everything *)anObject
(_mapWithObjectValues == anObject.mapWithObjectValues || [_mapWithObjectValues isEqualToDictionary:anObject.mapWithObjectValues]) &&
(_mapWithOtherModelValues == anObject.mapWithOtherModelValues || [_mapWithOtherModelValues isEqualToDictionary:anObject.mapWithOtherModelValues]) &&
(_mapWithPrimitiveValues == anObject.mapWithPrimitiveValues || [_mapWithPrimitiveValues isEqualToDictionary:anObject.mapWithPrimitiveValues]) &&
(_nestedObject == anObject.nestedObject || [_nestedObject isEqual:anObject.nestedObject]) &&
(_otherModelProp == anObject.otherModelProp || [_otherModelProp isEqual:anObject.otherModelProp]) &&
(_polymorphicProp == anObject.polymorphicProp || [_polymorphicProp isEqual:anObject.polymorphicProp]) &&
(_setProp == anObject.setProp || [_setProp isEqualToSet:anObject.setProp]) &&
Expand Down Expand Up @@ -1467,6 +1483,7 @@ - (NSUInteger)hash
[_mapWithObjectValues hash],
[_mapWithOtherModelValues hash],
[_mapWithPrimitiveValues hash],
[_nestedObject hash],
(NSUInteger)_nsintegerEnum,
(NSUInteger)_nsuintegerEnum,
[@(_numberProp) hash],
Expand Down Expand Up @@ -1500,7 +1517,7 @@ - (instancetype)mergeWithModel:(Everything *)modelObject initType:(PlankModelIni
}
- (NSDictionary *)dictionaryObjectRepresentation
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:36];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:37];
if (_everythingDirtyProperties.EverythingDirtyPropertyArrayProp) {
if (_arrayProp != nil) {
[dict setObject:_arrayProp forKey:@"array_prop"];
Expand Down Expand Up @@ -1691,6 +1708,13 @@ - (NSDictionary *)dictionaryObjectRepresentation
[dict setObject:[NSNull null] forKey:@"map_with_primitive_values"];
}
}
if (_everythingDirtyProperties.EverythingDirtyPropertyNestedObject) {
if (_nestedObject != nil) {
[dict setObject:[_nestedObject dictionaryObjectRepresentation] forKey:@"nested_object"];
} else {
[dict setObject:[NSNull null] forKey:@"nested_object"];
}
}
if (_everythingDirtyProperties.EverythingDirtyPropertyNsintegerEnum) {
[dict setObject:@(_nsintegerEnum) forKey:@"nsinteger_enum"];
}
Expand Down Expand Up @@ -1874,6 +1898,10 @@ - (BOOL)isMapWithPrimitiveValuesSet
{
return _everythingDirtyProperties.EverythingDirtyPropertyMapWithPrimitiveValues == 1;
}
- (BOOL)isNestedObjectSet
{
return _everythingDirtyProperties.EverythingDirtyPropertyNestedObject == 1;
}
- (BOOL)isNsintegerEnumSet
{
return _everythingDirtyProperties.EverythingDirtyPropertyNsintegerEnum == 1;
Expand Down Expand Up @@ -1979,6 +2007,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder
_mapWithObjectValues = [aDecoder decodeObjectOfClasses:[NSSet setWithArray:@[[NSDictionary class], [NSString class]]] forKey:@"map_with_object_values"];
_mapWithOtherModelValues = [aDecoder decodeObjectOfClasses:[NSSet setWithArray:@[[NSDictionary class], [User class]]] forKey:@"map_with_other_model_values"];
_mapWithPrimitiveValues = [aDecoder decodeObjectOfClasses:[NSSet setWithArray:@[[NSDictionary class], [NSNumber class]]] forKey:@"map_with_primitive_values"];
_nestedObject = [aDecoder decodeObjectOfClass:[Nested class] forKey:@"nested_object"];
_nsintegerEnum = (EverythingNsintegerEnum)[aDecoder decodeIntegerForKey:@"nsinteger_enum"];
_nsuintegerEnum = (EverythingNsuintegerEnum)[aDecoder decodeIntegerForKey:@"nsuinteger_enum"];
_numberProp = [aDecoder decodeDoubleForKey:@"number_prop"];
Expand Down Expand Up @@ -2016,6 +2045,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder
_everythingDirtyProperties.EverythingDirtyPropertyMapWithObjectValues = [aDecoder decodeIntForKey:@"map_with_object_values_dirty_property"] & 0x1;
_everythingDirtyProperties.EverythingDirtyPropertyMapWithOtherModelValues = [aDecoder decodeIntForKey:@"map_with_other_model_values_dirty_property"] & 0x1;
_everythingDirtyProperties.EverythingDirtyPropertyMapWithPrimitiveValues = [aDecoder decodeIntForKey:@"map_with_primitive_values_dirty_property"] & 0x1;
_everythingDirtyProperties.EverythingDirtyPropertyNestedObject = [aDecoder decodeIntForKey:@"nested_object_dirty_property"] & 0x1;
_everythingDirtyProperties.EverythingDirtyPropertyNsintegerEnum = [aDecoder decodeIntForKey:@"nsinteger_enum_dirty_property"] & 0x1;
_everythingDirtyProperties.EverythingDirtyPropertyNsuintegerEnum = [aDecoder decodeIntForKey:@"nsuinteger_enum_dirty_property"] & 0x1;
_everythingDirtyProperties.EverythingDirtyPropertyNumberProp = [aDecoder decodeIntForKey:@"number_prop_dirty_property"] & 0x1;
Expand Down Expand Up @@ -2059,6 +2089,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeObject:self.mapWithObjectValues forKey:@"map_with_object_values"];
[aCoder encodeObject:self.mapWithOtherModelValues forKey:@"map_with_other_model_values"];
[aCoder encodeObject:self.mapWithPrimitiveValues forKey:@"map_with_primitive_values"];
[aCoder encodeObject:self.nestedObject forKey:@"nested_object"];
[aCoder encodeInteger:self.nsintegerEnum forKey:@"nsinteger_enum"];
[aCoder encodeInteger:self.nsuintegerEnum forKey:@"nsuinteger_enum"];
[aCoder encodeDouble:self.numberProp forKey:@"number_prop"];
Expand Down Expand Up @@ -2095,6 +2126,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeInt:_everythingDirtyProperties.EverythingDirtyPropertyMapWithObjectValues forKey:@"map_with_object_values_dirty_property"];
[aCoder encodeInt:_everythingDirtyProperties.EverythingDirtyPropertyMapWithOtherModelValues forKey:@"map_with_other_model_values_dirty_property"];
[aCoder encodeInt:_everythingDirtyProperties.EverythingDirtyPropertyMapWithPrimitiveValues forKey:@"map_with_primitive_values_dirty_property"];
[aCoder encodeInt:_everythingDirtyProperties.EverythingDirtyPropertyNestedObject forKey:@"nested_object_dirty_property"];
[aCoder encodeInt:_everythingDirtyProperties.EverythingDirtyPropertyNsintegerEnum forKey:@"nsinteger_enum_dirty_property"];
[aCoder encodeInt:_everythingDirtyProperties.EverythingDirtyPropertyNsuintegerEnum forKey:@"nsuinteger_enum_dirty_property"];
[aCoder encodeInt:_everythingDirtyProperties.EverythingDirtyPropertyNumberProp forKey:@"number_prop_dirty_property"];
Expand Down Expand Up @@ -2180,6 +2212,9 @@ - (instancetype)initWithModel:(Everything *)modelObject
if (everythingDirtyProperties.EverythingDirtyPropertyMapWithPrimitiveValues) {
_mapWithPrimitiveValues = modelObject.mapWithPrimitiveValues;
}
if (everythingDirtyProperties.EverythingDirtyPropertyNestedObject) {
_nestedObject = modelObject.nestedObject;
}
if (everythingDirtyProperties.EverythingDirtyPropertyNsintegerEnum) {
_nsintegerEnum = modelObject.nsintegerEnum;
}
Expand Down Expand Up @@ -2299,6 +2334,18 @@ - (void)mergeWithModel:(Everything *)modelObject
if (modelObject.everythingDirtyProperties.EverythingDirtyPropertyMapWithPrimitiveValues) {
builder.mapWithPrimitiveValues = modelObject.mapWithPrimitiveValues;
}
if (modelObject.everythingDirtyProperties.EverythingDirtyPropertyNestedObject) {
id value = modelObject.nestedObject;
if (value != nil) {
if (builder.nestedObject) {
builder.nestedObject = [builder.nestedObject mergeWithModel:value initType:PlankModelInitTypeFromSubmerge];
} else {
builder.nestedObject = value;
}
} else {
builder.nestedObject = nil;
}
}
if (modelObject.everythingDirtyProperties.EverythingDirtyPropertyNsintegerEnum) {
builder.nsintegerEnum = modelObject.nsintegerEnum;
}
Expand Down Expand Up @@ -2455,6 +2502,11 @@ - (void)setMapWithPrimitiveValues:(NSDictionary<NSString *, NSNumber /* Integer
_mapWithPrimitiveValues = mapWithPrimitiveValues;
_everythingDirtyProperties.EverythingDirtyPropertyMapWithPrimitiveValues = 1;
}
- (void)setNestedObject:(Nested *)nestedObject
{
_nestedObject = nestedObject;
_everythingDirtyProperties.EverythingDirtyPropertyNestedObject = 1;
}
- (void)setNsintegerEnum:(EverythingNsintegerEnum)nsintegerEnum
{
_nsintegerEnum = nsintegerEnum;
Expand Down
207 changes: 207 additions & 0 deletions Examples/Cocoa/Sources/Objective_C/Nested.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
//
// Nested.m
// Autogenerated by Plank (https://pinterest.github.io/plank/)
//
// DO NOT EDIT - EDITS WILL BE OVERWRITTEN
// @generated
//

#import "Nested.h"

struct NestedDirtyProperties {
unsigned int NestedDirtyPropertyIdentifier:1;
};

@interface Nested ()
@property (nonatomic, assign, readwrite) struct NestedDirtyProperties nestedDirtyProperties;
@end

@interface NestedBuilder ()
@property (nonatomic, assign, readwrite) struct NestedDirtyProperties nestedDirtyProperties;
@end

@implementation Nested
+ (NSString *)className
{
return @"Nested";
}
+ (NSString *)polymorphicTypeIdentifier
{
return @"nested";
}
+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dictionary
{
return [[self alloc] initWithModelDictionary:dictionary];
}
- (instancetype)init
{
return [self initWithModelDictionary:@{}];
}
- (instancetype)initWithModelDictionary:(NS_VALID_UNTIL_END_OF_SCOPE NSDictionary *)modelDictionary
{
NSParameterAssert(modelDictionary);
if (!modelDictionary) {
return self;
}
if (!(self = [super init])) {
return self;
}
{
__unsafe_unretained id value = modelDictionary[@"id"]; // Collection will retain.
if (value != nil) {
if (value != (id)kCFNull) {
self->_identifier = [value integerValue];
}
self->_nestedDirtyProperties.NestedDirtyPropertyIdentifier = 1;
}
}
if ([self class] == [Nested class]) {
[[NSNotificationCenter defaultCenter] postNotificationName:kPlankDidInitializeNotification object:self userInfo:@{ kPlankInitTypeKey : @(PlankModelInitTypeDefault) }];
}
return self;
}
- (instancetype)initWithBuilder:(NestedBuilder *)builder
{
NSParameterAssert(builder);
return [self initWithBuilder:builder initType:PlankModelInitTypeDefault];
}
- (instancetype)initWithBuilder:(NestedBuilder *)builder initType:(PlankModelInitType)initType
{
NSParameterAssert(builder);
if (!(self = [super init])) {
return self;
}
_identifier = builder.identifier;
_nestedDirtyProperties = builder.nestedDirtyProperties;
if ([self class] == [Nested class]) {
[[NSNotificationCenter defaultCenter] postNotificationName:kPlankDidInitializeNotification object:self userInfo:@{ kPlankInitTypeKey : @(initType) }];
}
return self;
}
- (NSString *)debugDescription
{
NSArray<NSString *> *parentDebugDescription = [[super debugDescription] componentsSeparatedByString:@"\n"];
NSMutableArray *descriptionFields = [NSMutableArray arrayWithCapacity:1];
[descriptionFields addObject:parentDebugDescription];
struct NestedDirtyProperties props = _nestedDirtyProperties;
if (props.NestedDirtyPropertyIdentifier) {
[descriptionFields addObject:[@"_identifier = " stringByAppendingFormat:@"%@", @(_identifier)]];
}
return [NSString stringWithFormat:@"Nested = {\n%@\n}", debugDescriptionForFields(descriptionFields)];
}
- (instancetype)copyWithBlock:(PLANK_NOESCAPE void (^)(NestedBuilder *builder))block
{
NSParameterAssert(block);
NestedBuilder *builder = [[NestedBuilder alloc] initWithModel:self];
block(builder);
return [builder build];
}
- (BOOL)isEqual:(id)anObject
{
if (self == anObject) {
return YES;
}
if ([anObject isKindOfClass:[Nested class]] == NO) {
return NO;
}
return [self isEqualToNested:anObject];
}
- (BOOL)isEqualToNested:(Nested *)anObject
{
return (
(anObject != nil) &&
(_identifier == anObject.identifier)
);
}
- (NSUInteger)hash
{
NSUInteger subhashes[] = {
17,
(NSUInteger)_identifier
};
return PINIntegerArrayHash(subhashes, sizeof(subhashes) / sizeof(subhashes[0]));
}
- (instancetype)mergeWithModel:(Nested *)modelObject
{
return [self mergeWithModel:modelObject initType:PlankModelInitTypeFromMerge];
}
- (instancetype)mergeWithModel:(Nested *)modelObject initType:(PlankModelInitType)initType
{
NSParameterAssert(modelObject);
NestedBuilder *builder = [[NestedBuilder alloc] initWithModel:self];
[builder mergeWithModel:modelObject];
return [[Nested alloc] initWithBuilder:builder initType:initType];
}
- (NSDictionary *)dictionaryObjectRepresentation
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:1];
if (_nestedDirtyProperties.NestedDirtyPropertyIdentifier) {
[dict setObject:@(_identifier) forKey: @"id"];
}
return dict;
}
- (BOOL)isIdentifierSet
{
return _nestedDirtyProperties.NestedDirtyPropertyIdentifier == 1;
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
#pragma mark - NSSecureCoding
+ (BOOL)supportsSecureCoding
{
return YES;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (!(self = [super init])) {
return self;
}
_identifier = [aDecoder decodeIntegerForKey:@"id"];
_nestedDirtyProperties.NestedDirtyPropertyIdentifier = [aDecoder decodeIntForKey:@"id_dirty_property"] & 0x1;
if ([self class] == [Nested class]) {
[[NSNotificationCenter defaultCenter] postNotificationName:kPlankDidInitializeNotification object:self userInfo:@{ kPlankInitTypeKey : @(PlankModelInitTypeDefault) }];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInteger:self.identifier forKey:@"id"];
[aCoder encodeInt:_nestedDirtyProperties.NestedDirtyPropertyIdentifier forKey:@"id_dirty_property"];
}
@end

@implementation NestedBuilder
- (instancetype)initWithModel:(Nested *)modelObject
{
NSParameterAssert(modelObject);
if (!(self = [super init])) {
return self;
}
struct NestedDirtyProperties nestedDirtyProperties = modelObject.nestedDirtyProperties;
if (nestedDirtyProperties.NestedDirtyPropertyIdentifier) {
_identifier = modelObject.identifier;
}
_nestedDirtyProperties = nestedDirtyProperties;
return self;
}
- (Nested *)build
{
return [[Nested alloc] initWithBuilder:self];
}
- (void)mergeWithModel:(Nested *)modelObject
{
NSParameterAssert(modelObject);
NestedBuilder *builder = self;
if (modelObject.nestedDirtyProperties.NestedDirtyPropertyIdentifier) {
builder.identifier = modelObject.identifier;
}
}
- (void)setIdentifier:(NSInteger)identifier
{
_identifier = identifier;
_nestedDirtyProperties.NestedDirtyPropertyIdentifier = 1;
}
@end
Loading