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

Added support for NSSet properties #91

Merged
merged 6 commits into from
Nov 3, 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
57 changes: 55 additions & 2 deletions Examples/Cocoa/Sources/Objective_C/Board.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

#import "Board.h"
#import "Image.h"
#import "User.h"

struct BoardDirtyProperties {
unsigned int BoardDirtyPropertyContributors:1;
unsigned int BoardDirtyPropertyCounts:1;
unsigned int BoardDirtyPropertyCreatedAt:1;
unsigned int BoardDirtyPropertyCreator:1;
Expand Down Expand Up @@ -99,6 +101,26 @@ - (instancetype)initWithModelDictionary:(NS_VALID_UNTIL_END_OF_SCOPE NSDictionar
self->_boardDirtyProperties.BoardDirtyPropertyCreatedAt = 1;
}
}
{
__unsafe_unretained id value = modelDictionary[@"contributors"]; // Collection will retain.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need to be converted to NSSet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I just added it.

if (value != nil) {
if (value != (id)kCFNull) {
NSArray *items = value;
NSMutableSet *result0 = [NSMutableSet setWithCapacity:items.count];
for (id obj0 in items) {
if (obj0 != (id)kCFNull) {
id tmp0 = nil;
tmp0 = [User modelObjectWithDictionary:obj0];
if (tmp0 != nil) {
[result0 addObject:tmp0];
}
}
}
self->_contributors = result0;
}
self->_boardDirtyProperties.BoardDirtyPropertyContributors = 1;
}
}
{
__unsafe_unretained id value = modelDictionary[@"description"]; // Collection will retain.
if (value != nil) {
Expand Down Expand Up @@ -154,6 +176,7 @@ - (instancetype)initWithBuilder:(BoardBuilder *)builder initType:(PlankModelInit
_image = builder.image;
_counts = builder.counts;
_createdAt = builder.createdAt;
_contributors = builder.contributors;
_descriptionText = builder.descriptionText;
_creator = builder.creator;
_url = builder.url;
Expand All @@ -166,7 +189,7 @@ - (instancetype)initWithBuilder:(BoardBuilder *)builder initType:(PlankModelInit
- (NSString *)debugDescription
{
NSArray<NSString *> *parentDebugDescription = [[super debugDescription] componentsSeparatedByString:@"\n"];
NSMutableArray *descriptionFields = [NSMutableArray arrayWithCapacity:8];
NSMutableArray *descriptionFields = [NSMutableArray arrayWithCapacity:9];
[descriptionFields addObject:parentDebugDescription];
struct BoardDirtyProperties props = _boardDirtyProperties;
if (props.BoardDirtyPropertyName) {
Expand All @@ -184,6 +207,9 @@ - (NSString *)debugDescription
if (props.BoardDirtyPropertyCreatedAt) {
[descriptionFields addObject:[@"_createdAt = " stringByAppendingFormat:@"%@", _createdAt]];
}
if (props.BoardDirtyPropertyContributors) {
[descriptionFields addObject:[@"_contributors = " stringByAppendingFormat:@"%@", _contributors]];
}
if (props.BoardDirtyPropertyDescriptionText) {
[descriptionFields addObject:[@"_descriptionText = " stringByAppendingFormat:@"%@", _descriptionText]];
}
Expand Down Expand Up @@ -221,6 +247,7 @@ - (BOOL)isEqualToBoard:(Board *)anObject
(_image == anObject.image || [_image isEqual:anObject.image]) &&
(_counts == anObject.counts || [_counts isEqualToDictionary:anObject.counts]) &&
(_createdAt == anObject.createdAt || [_createdAt isEqualToDate:anObject.createdAt]) &&
(_contributors == anObject.contributors || [_contributors isEqualToSet:anObject.contributors]) &&
(_descriptionText == anObject.descriptionText || [_descriptionText isEqualToString:anObject.descriptionText]) &&
(_creator == anObject.creator || [_creator isEqualToDictionary:anObject.creator]) &&
(_url == anObject.url || [_url isEqual:anObject.url])
Expand All @@ -235,6 +262,7 @@ - (NSUInteger)hash
[_image hash],
[_counts hash],
[_createdAt hash],
[_contributors hash],
[_descriptionText hash],
[_creator hash],
[_url hash]
Expand All @@ -254,7 +282,7 @@ - (instancetype)mergeWithModel:(Board *)modelObject initType:(PlankModelInitType
}
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:8];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:9];
if (_boardDirtyProperties.BoardDirtyPropertyName) {
if (_name != nil) {
[dict setObject:_name forKey:@"name"];
Expand Down Expand Up @@ -291,6 +319,16 @@ - (NSDictionary *)dictionaryRepresentation
[dict setObject:[NSNull null] forKey:@"created_at"];
}
}
if (_boardDirtyProperties.BoardDirtyPropertyContributors) {
NSSet *items0 = _contributors;
NSMutableSet *result0 = [NSMutableSet setWithCapacity:items0.count];
for (id obj0 in items0) {
if (obj0 != (id)kCFNull) {
[result0 addObject:[obj0 dictionaryRepresentation]];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're going to have to add a cast here to avoid hitting warnings for strict selector matching. Added an issue here #90

}
}
[dict setObject:result0 forKey:@"contributors"];
}
if (_boardDirtyProperties.BoardDirtyPropertyDescriptionText) {
if (_descriptionText != nil) {
[dict setObject:_descriptionText forKey:@"description"];
Expand Down Expand Up @@ -334,6 +372,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder
_image = [aDecoder decodeObjectOfClass:[Image class] forKey:@"image"];
_counts = [aDecoder decodeObjectOfClasses:[NSSet setWithArray:@[[NSDictionary class], [NSNumber class]]] forKey:@"counts"];
_createdAt = [aDecoder decodeObjectOfClass:[NSDate class] forKey:@"created_at"];
_contributors = [aDecoder decodeObjectOfClasses:[NSSet setWithArray:@[[NSSet class], [User class]]] forKey:@"contributors"];
_descriptionText = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"description"];
_creator = [aDecoder decodeObjectOfClasses:[NSSet setWithArray:@[[NSDictionary class], [NSString class]]] forKey:@"creator"];
_url = [aDecoder decodeObjectOfClass:[NSURL class] forKey:@"url"];
Expand All @@ -342,6 +381,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder
_boardDirtyProperties.BoardDirtyPropertyImage = [aDecoder decodeIntForKey:@"image_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyCounts = [aDecoder decodeIntForKey:@"counts_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyCreatedAt = [aDecoder decodeIntForKey:@"created_at_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyContributors = [aDecoder decodeIntForKey:@"contributors_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyDescriptionText = [aDecoder decodeIntForKey:@"description_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyCreator = [aDecoder decodeIntForKey:@"creator_dirty_property"] & 0x1;
_boardDirtyProperties.BoardDirtyPropertyUrl = [aDecoder decodeIntForKey:@"url_dirty_property"] & 0x1;
Expand All @@ -357,6 +397,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeObject:self.image forKey:@"image"];
[aCoder encodeObject:self.counts forKey:@"counts"];
[aCoder encodeObject:self.createdAt forKey:@"created_at"];
[aCoder encodeObject:self.contributors forKey:@"contributors"];
[aCoder encodeObject:self.descriptionText forKey:@"description"];
[aCoder encodeObject:self.creator forKey:@"creator"];
[aCoder encodeObject:self.url forKey:@"url"];
Expand All @@ -365,6 +406,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyImage forKey:@"image_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyCounts forKey:@"counts_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyCreatedAt forKey:@"created_at_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyContributors forKey:@"contributors_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyDescriptionText forKey:@"description_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyCreator forKey:@"creator_dirty_property"];
[aCoder encodeInt:_boardDirtyProperties.BoardDirtyPropertyUrl forKey:@"url_dirty_property"];
Expand Down Expand Up @@ -394,6 +436,9 @@ - (instancetype)initWithModel:(Board *)modelObject
if (boardDirtyProperties.BoardDirtyPropertyCreatedAt) {
_createdAt = modelObject.createdAt;
}
if (boardDirtyProperties.BoardDirtyPropertyContributors) {
_contributors = modelObject.contributors;
}
if (boardDirtyProperties.BoardDirtyPropertyDescriptionText) {
_descriptionText = modelObject.descriptionText;
}
Expand Down Expand Up @@ -434,6 +479,9 @@ - (void)mergeWithModel:(Board *)modelObject
if (modelObject.boardDirtyProperties.BoardDirtyPropertyCreatedAt) {
builder.createdAt = modelObject.createdAt;
}
if (modelObject.boardDirtyProperties.BoardDirtyPropertyContributors) {
builder.contributors = modelObject.contributors;
}
if (modelObject.boardDirtyProperties.BoardDirtyPropertyDescriptionText) {
builder.descriptionText = modelObject.descriptionText;
}
Expand Down Expand Up @@ -469,6 +517,11 @@ - (void)setCreatedAt:(NSDate *)createdAt
_createdAt = createdAt;
_boardDirtyProperties.BoardDirtyPropertyCreatedAt = 1;
}
- (void)setContributors:(NSSet<User *> *)contributors
{
_contributors = contributors;
_boardDirtyProperties.BoardDirtyPropertyContributors = 1;
}
- (void)setDescriptionText:(NSString *)descriptionText
{
_descriptionText = descriptionText;
Expand Down
3 changes: 3 additions & 0 deletions Examples/Cocoa/Sources/Objective_C/include/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "PlankModelRuntime.h"
@class BoardBuilder;
@class Image;
@class User;

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -19,6 +20,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonnull, nonatomic, strong, readonly) Image * image;
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, NSNumber /* Integer */ *> * counts;
@property (nullable, nonatomic, strong, readonly) NSDate * createdAt;
@property (nullable, nonatomic, strong, readonly) NSSet<User *> * contributors;
@property (nullable, nonatomic, copy, readonly) NSString * descriptionText;
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, NSString *> * creator;
@property (nullable, nonatomic, strong, readonly) NSURL * url;
Expand All @@ -41,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonnull, nonatomic, strong, readwrite) Image * image;
@property (nullable, nonatomic, strong, readwrite) NSDictionary<NSString *, NSNumber /* Integer */ *> * counts;
@property (nullable, nonatomic, strong, readwrite) NSDate * createdAt;
@property (nullable, nonatomic, strong, readwrite) NSSet<User *> * contributors;
@property (nullable, nonatomic, copy, readwrite) NSString * descriptionText;
@property (nullable, nonatomic, strong, readwrite) NSDictionary<NSString *, NSString *> * creator;
@property (nullable, nonatomic, strong, readwrite) NSURL * url;
Expand Down
4 changes: 3 additions & 1 deletion Examples/JS/flow/BoardType.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

import type { PlankDate, PlankURI } from './runtime.flow.js';
import type { ImageType } from './ImageType.js';
import type { UserType } from './UserType.js';

export type BoardType = $Shape<{|
+name: ?string,
+id: ?string,
+image: ?ImageType,
+image: ImageType,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this change? Seems not related to this change though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maicki I don't know exactly. This was all generated code.

+counts: ?{ +[string]: number } /* Integer */,
+created_at: ?PlankDate,
+contributors: ?Set<UserType>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maicki - Can you sign off on the flow changes?

+description: ?string,
+creator: ?{ +[string]: string },
+url: ?PlankURI,
Expand Down
5 changes: 5 additions & 0 deletions Examples/PDK/board.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"type": "string",
"format": "date-time"
},
"contributors": {
"type": "array",
"unique": "true",
"items": { "$ref": "user.json" }
},
"counts": {
"type": "object",
"additionalProperties": { "type": "integer" }
Expand Down
1 change: 1 addition & 0 deletions Sources/Core/JSADTExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extension JSModelRenderer {
case .enumT(.integer): return "" // TODO: Not supported at the moment
case .boolean: return "boolean"
case .array(itemType: _): return "Array<*>"
case .set(itemType: _): return "Set<*>"
case .map(valueType: .none): return "{}"
case .map(valueType: .some(let valueType)) where valueType.isObjCPrimitiveType:
return "{ +[string]: number } /* \(valueType.debugDescription) */"
Expand Down
8 changes: 7 additions & 1 deletion Sources/Core/JSFileRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension JSFileRenderer {
return [schemaRoot.className(with: self.params)]
case .map(valueType: .some(let valueType)):
return referencedClassNames(schema: valueType)
case .array(itemType: .some(let itemType)):
case .array(itemType: .some(let itemType)), .set(itemType: .some(let itemType)):
return referencedClassNames(schema: itemType)
case .oneOf(types: let itemTypes):
return itemTypes.flatMap(referencedClassNames)
Expand All @@ -72,6 +72,12 @@ extension JSFileRenderer {
return "Array<number /* \(itemType.debugDescription) */>"
case .array(itemType: .some(let itemType)):
return "Array<\(flowTypeName(param, itemType))>"
case .set(itemType: .none):
return "Set<*>"
case .set(itemType: .some(let itemType)) where itemType.isObjCPrimitiveType:
return "Set<number /* \(itemType.debugDescription)> */>"
case .set(itemType: .some(let itemType)):
return "Set<\(flowTypeName(param, itemType))>"
case .map(valueType: .none):
return "{}"
case .map(valueType: .some(let valueType)) where valueType.isObjCPrimitiveType:
Expand Down
5 changes: 5 additions & 0 deletions Sources/Core/ObjCADTRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct ObjCADTRenderer: ObjCFileRenderer {
case .enumT(.integer): return "IntegerEnum" // TODO: Allow custom names
case .boolean: return "Boolean"
case .array(itemType: _): return "Array"
case .set(itemType: _): return "Set"
case .map(valueType: _): return "Dictionary"
case .string(.some(.uri)): return "URL"
case .string(.some(.dateTime)): return "Date"
Expand Down Expand Up @@ -138,6 +139,10 @@ struct ObjCADTRenderer: ObjCFileRenderer {
return ObjCIR.caseStmt(self.internalTypeEnumName + ObjCADTRenderer.objectName(schemaObj.schema)) {[
ObjCIR.stmt("return [[NSDictionary alloc]initWithDictionary:[self.value\(index) dictionaryRepresentation]]")
]}
case .set(itemType: _):
return ObjCIR.caseStmt(self.internalTypeEnumName + ObjCADTRenderer.objectName(schemaObj.schema)) {[
ObjCIR.stmt("return [[NSDictionary alloc]initWithDictionary:[self.value\(index) dictionaryRepresentation]]")
]}
case .map(valueType: _):
return ObjCIR.caseStmt(self.internalTypeEnumName + ObjCADTRenderer.objectName(schemaObj.schema)) {[
ObjCIR.stmt("return [[NSDictionary alloc]initWithDictionary:[self.value\(index) absoluteString] ]")
Expand Down
8 changes: 7 additions & 1 deletion Sources/Core/ObjCFileRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension ObjCFileRenderer {
return [schemaRoot.className(with: self.params)]
case .map(valueType: .some(let valueType)):
return referencedClassNames(schema: valueType)
case .array(itemType: .some(let itemType)):
case .array(itemType: .some(let itemType)), .set(itemType: .some(let itemType)):
return referencedClassNames(schema: itemType)
case .oneOf(types: let itemTypes):
return itemTypes.flatMap(referencedClassNames)
Expand All @@ -73,6 +73,12 @@ extension ObjCFileRenderer {
return "NSArray<NSNumber /* \(itemType.debugDescription) */ *> *"
case .array(itemType: .some(let itemType)):
return "NSArray<\(objcClassFromSchema(param, itemType))> *"
case .set(itemType: .none):
return "NSSet *"
case .set(itemType: .some(let itemType)) where itemType.isObjCPrimitiveType:
return "NSSet<NSNumber /*> \(itemType.debugDescription) */ *> *"
case .set(itemType: .some(let itemType)):
return "NSSet<\(objcClassFromSchema(param, itemType))> *"
case .map(valueType: .none):
return "NSDictionary *"
case .map(valueType: .some(let valueType)) where valueType.isObjCPrimitiveType:
Expand Down
2 changes: 2 additions & 0 deletions Sources/Core/ObjectiveCDebugExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ extension ObjCFileRenderer {
return propIVarName
case .array(itemType: _):
return propIVarName
case .set(itemType: _):
return propIVarName
case .map(valueType: _):
return propIVarName
case .object:
Expand Down
Loading