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

Add dictionary representation to Objc models #67

Merged
merged 2 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 69 additions & 0 deletions Examples/Cocoa/Sources/objc/Board.m
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,75 @@ - (instancetype)mergeWithModel:(Board *)modelObject initType:(PlankModelInitType
[builder mergeWithModel:modelObject];
return [[Board alloc] initWithBuilder:builder initType:initType];
}
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:8];
if (_boardDirtyProperties.BoardDirtyPropertyName) {
if (_name != nil) {
[dict setObject:_name forKey:@"name"];
}
else {
[dict setObject:[NSNull null] forKey:@"name"];
}
}
if (_boardDirtyProperties.BoardDirtyPropertyIdentifier) {
if (_identifier != nil) {
[dict setObject:_identifier forKey:@"id"];
}
else {
[dict setObject:[NSNull null] forKey:@"id"];
}
}
if (_boardDirtyProperties.BoardDirtyPropertyImage) {
if (_image != nil) {
[dict setObject:[_image dictionaryRepresentation] forKey:@"image"];
}
else {
[dict setObject:[NSNull null] forKey:@"image"];
}
}
if (_boardDirtyProperties.BoardDirtyPropertyCounts) {
if (_counts != nil) {
[dict setObject:_counts forKey:@"counts"];
}
else {
[dict setObject:[NSNull null] forKey:@"counts"];
}
}
if (_boardDirtyProperties.BoardDirtyPropertyCreatedAt) {
if (_createdAt != nil && [NSValueTransformer allowsReverseTransformation]) {
[dict setObject:[[NSValueTransformer valueTransformerForName:kPlankDateValueTransformerKey] reverseTransformedValue:_createdAt] forKey:@"created_at"];
}
else {
[dict setObject:[NSNull null] forKey:@"created_at"];
}
}
if (_boardDirtyProperties.BoardDirtyPropertyDescriptionText) {
if (_descriptionText != nil) {
[dict setObject:_descriptionText forKey:@"description"];
}
else {
[dict setObject:[NSNull null] forKey:@"description"];
}
}
if (_boardDirtyProperties.BoardDirtyPropertyCreator) {
if (_creator != nil) {
[dict setObject:_creator forKey:@"creator"];
}
else {
[dict setObject:[NSNull null] forKey:@"creator"];
}
}
if (_boardDirtyProperties.BoardDirtyPropertyUrl) {
if (_url != nil) {
[dict setObject:[_url absoluteString] forKey:@"url"];
}
else {
[dict setObject:[NSNull null] forKey:@"url"];
}
}
return dict;
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone
{
Expand Down
19 changes: 19 additions & 0 deletions Examples/Cocoa/Sources/objc/Image.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ - (instancetype)mergeWithModel:(Image *)modelObject initType:(PlankModelInitType
[builder mergeWithModel:modelObject];
return [[Image alloc] initWithBuilder:builder initType:initType];
}
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:3];
if (_imageDirtyProperties.ImageDirtyPropertyHeight) {
[dict setObject:@(_height) forKey: @"height" ];
}
if (_imageDirtyProperties.ImageDirtyPropertyUrl) {
if (_url != nil) {
[dict setObject:[_url absoluteString] forKey:@"url"];
}
else {
[dict setObject:[NSNull null] forKey:@"url"];
}
}
if (_imageDirtyProperties.ImageDirtyPropertyWidth) {
[dict setObject:@(_width) forKey: @"width" ];
}
return dict;
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone
{
Expand Down
110 changes: 110 additions & 0 deletions Examples/Cocoa/Sources/objc/Pin.m
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,116 @@ - (instancetype)mergeWithModel:(Pin *)modelObject initType:(PlankModelInitType)i
[builder mergeWithModel:modelObject];
return [[Pin alloc] initWithBuilder:builder initType:initType];
}
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:13];
if (_pinDirtyProperties.PinDirtyPropertyNote) {
if (_note != nil) {
[dict setObject:_note forKey:@"note"];
}
else {
[dict setObject:[NSNull null] forKey:@"note"];
}
}
if (_pinDirtyProperties.PinDirtyPropertyMedia) {
if (_media != nil) {
[dict setObject:_media forKey:@"media"];
}
else {
[dict setObject:[NSNull null] forKey:@"media"];
}
}
if (_pinDirtyProperties.PinDirtyPropertyCounts) {
if (_counts != nil) {
[dict setObject:_counts forKey:@"counts"];
}
else {
[dict setObject:[NSNull null] forKey:@"counts"];
}
}
if (_pinDirtyProperties.PinDirtyPropertyDescriptionText) {
if (_descriptionText != nil) {
[dict setObject:_descriptionText forKey:@"description"];
}
else {
[dict setObject:[NSNull null] forKey:@"description"];
}
}
if (_pinDirtyProperties.PinDirtyPropertyCreator) {
NSMutableDictionary *items0 = [NSMutableDictionary new];
for (id key in _creator) {
if ([_creator objectForKey:key] != (id)kCFNull) {
[items0 setObject:[[_creator objectForKey:key] dictionaryRepresentation] forKey:key];
}
}
[dict setObject:items0 forKey: @"_creator" ];
}
if (_pinDirtyProperties.PinDirtyPropertyAttribution) {
if (_attribution != nil) {
[dict setObject:_attribution forKey:@"attribution"];
}
else {
[dict setObject:[NSNull null] forKey:@"attribution"];
}
}
if (_pinDirtyProperties.PinDirtyPropertyBoard) {
if (_board != nil) {
[dict setObject:[_board dictionaryRepresentation] forKey:@"board"];
}
else {
[dict setObject:[NSNull null] forKey:@"board"];
}
}
if (_pinDirtyProperties.PinDirtyPropertyColor) {
if (_color != nil) {
[dict setObject:_color forKey:@"color"];
}
else {
[dict setObject:[NSNull null] forKey:@"color"];
}
}
if (_pinDirtyProperties.PinDirtyPropertyLink) {
if (_link != nil) {
[dict setObject:[_link absoluteString] forKey:@"link"];
}
else {
[dict setObject:[NSNull null] forKey:@"link"];
}
}
if (_pinDirtyProperties.PinDirtyPropertyIdentifier) {
if (_identifier != nil) {
[dict setObject:_identifier forKey:@"id"];
}
else {
[dict setObject:[NSNull null] forKey:@"id"];
}
}
if (_pinDirtyProperties.PinDirtyPropertyImage) {
if (_image != nil) {
[dict setObject:[_image dictionaryRepresentation] forKey:@"image"];
}
else {
[dict setObject:[NSNull null] forKey:@"image"];
}
}
if (_pinDirtyProperties.PinDirtyPropertyCreatedAt) {
if (_createdAt != nil && [NSValueTransformer allowsReverseTransformation]) {
[dict setObject:[[NSValueTransformer valueTransformerForName:kPlankDateValueTransformerKey] reverseTransformedValue:_createdAt] forKey:@"created_at"];
}
else {
[dict setObject:[NSNull null] forKey:@"created_at"];
}
}
if (_pinDirtyProperties.PinDirtyPropertyUrl) {
if (_url != nil) {
[dict setObject:[_url absoluteString] forKey:@"url"];
}
else {
[dict setObject:[NSNull null] forKey:@"url"];
}
}
return dict;
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone
{
Expand Down
69 changes: 69 additions & 0 deletions Examples/Cocoa/Sources/objc/User.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,75 @@ - (instancetype)mergeWithModel:(User *)modelObject initType:(PlankModelInitType)
[builder mergeWithModel:modelObject];
return [[User alloc] initWithBuilder:builder initType:initType];
}
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:8];
if (_userDirtyProperties.UserDirtyPropertyLastName) {
if (_lastName != nil) {
[dict setObject:_lastName forKey:@"last_name"];
}
else {
[dict setObject:[NSNull null] forKey:@"last_name"];
}
}
if (_userDirtyProperties.UserDirtyPropertyIdentifier) {
if (_identifier != nil) {
[dict setObject:_identifier forKey:@"id"];
}
else {
[dict setObject:[NSNull null] forKey:@"id"];
}
}
if (_userDirtyProperties.UserDirtyPropertyFirstName) {
if (_firstName != nil) {
[dict setObject:_firstName forKey:@"first_name"];
}
else {
[dict setObject:[NSNull null] forKey:@"first_name"];
}
}
if (_userDirtyProperties.UserDirtyPropertyImage) {
if (_image != nil) {
[dict setObject:[_image dictionaryRepresentation] forKey:@"image"];
}
else {
[dict setObject:[NSNull null] forKey:@"image"];
}
}
if (_userDirtyProperties.UserDirtyPropertyCounts) {
if (_counts != nil) {
[dict setObject:_counts forKey:@"counts"];
}
else {
[dict setObject:[NSNull null] forKey:@"counts"];
}
}
if (_userDirtyProperties.UserDirtyPropertyCreatedAt) {
if (_createdAt != nil && [NSValueTransformer allowsReverseTransformation]) {
[dict setObject:[[NSValueTransformer valueTransformerForName:kPlankDateValueTransformerKey] reverseTransformedValue:_createdAt] forKey:@"created_at"];
}
else {
[dict setObject:[NSNull null] forKey:@"created_at"];
}
}
if (_userDirtyProperties.UserDirtyPropertyUsername) {
if (_username != nil) {
[dict setObject:_username forKey:@"username"];
}
else {
[dict setObject:[NSNull null] forKey:@"username"];
}
}
if (_userDirtyProperties.UserDirtyPropertyBio) {
if (_bio != nil) {
[dict setObject:_bio forKey:@"bio"];
}
else {
[dict setObject:[NSNull null] forKey:@"bio"];
}
}
return dict;
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone
{
Expand Down
1 change: 1 addition & 0 deletions Examples/Cocoa/Sources/objc/include/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)isEqualToBoard:(Board *)anObject;
- (instancetype)mergeWithModel:(Board *)modelObject;
- (instancetype)mergeWithModel:(Board *)modelObject initType:(PlankModelInitType)initType;
- (NSDictionary *)dictionaryRepresentation;
@end

@interface BoardBuilder : NSObject
Expand Down
1 change: 1 addition & 0 deletions Examples/Cocoa/Sources/objc/include/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)isEqualToImage:(Image *)anObject;
- (instancetype)mergeWithModel:(Image *)modelObject;
- (instancetype)mergeWithModel:(Image *)modelObject initType:(PlankModelInitType)initType;
- (NSDictionary *)dictionaryRepresentation;
@end

@interface ImageBuilder : NSObject
Expand Down
1 change: 1 addition & 0 deletions Examples/Cocoa/Sources/objc/include/Pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)isEqualToPin:(Pin *)anObject;
- (instancetype)mergeWithModel:(Pin *)modelObject;
- (instancetype)mergeWithModel:(Pin *)modelObject initType:(PlankModelInitType)initType;
- (NSDictionary *)dictionaryRepresentation;
@end

@interface PinBuilder : NSObject
Expand Down
1 change: 1 addition & 0 deletions Examples/Cocoa/Sources/objc/include/User.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)isEqualToUser:(User *)anObject;
- (instancetype)mergeWithModel:(User *)modelObject;
- (instancetype)mergeWithModel:(User *)modelObject initType:(PlankModelInitType)initType;
- (NSDictionary *)dictionaryRepresentation;
@end

@interface UserBuilder : NSObject
Expand Down
Loading