Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

IMPLEMENT support of custom scalar types (/!\ swift not supported) #207

Merged
merged 2 commits into from

3 participants

QuentinArnault AliSoftware Jonathan 'Wolf' Rentzsch
QuentinArnault

This is my proposition tu resolve issue #162.

To specify a custom scalar type you have to add to your Entity attribute user info a new item with key "attributeValueScalarType" and your custom scalar type as value.

During generation, for int16_t, int32_t, int64_t scalar types, if "attributeValueScalarType" key exists, the custom scalar type is copied into generated file.

If this custom scalar type is not visible from the machine generated header file (i.e. : not present in your pch file), it is possible to define an additional header file name.

It is possible to add to your Entity user info a new item with key "additionalHeaderFileName" and your additional header file name.

During generation, if this key is present, a new import line is added to the generated machine header file.

Warning : Swift is not supported, I do not know (yet) Swift. Feel free to add this support.

AliSoftware

:+1: Waiting for the support of custom type (especially for enums) for a long time!

AliSoftware

Actually your code seems pretty compatible with Swift already.

If the developer uses Swift, it's up to him/her to provide the right type name in the attributeValueScalarType key. It's only if the key is not present that mogenerator itself should choose between int16_t and Int16 for example, but if the developer for the scalar type using the attributeValueScalarType key then it's their responsibility to enter a valid type name.

So probably the only missing thing to fully support this modification in the Swift side is to apply the same modifications that are present for the Objective-C template to the Swift template, to add the import directive when an additionalHeaderFileName is present, and that's it!

Jonathan 'Wolf' Rentzsch rentzsch added this to the 1.28 milestone
Jonathan 'Wolf' Rentzsch rentzsch merged commit 4a57ef9 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 20, 2014
Commits on Jun 23, 2014
  1. IMPLEMENT allows custom scalar type for all scalar types

    Quentin ARNAULT authored
This page is out of date. Refresh to see the latest.
Showing with 45 additions and 21 deletions.
  1. +2 −0  mogenerator.h
  2. +39 −21 mogenerator.m
  3. +4 −0 templates/machine.h.motemplate
2  mogenerator.h
View
@@ -22,8 +22,10 @@
- (BOOL)hasCustomClass;
- (BOOL)hasSuperentity;
- (BOOL)hasCustomSuperentity;
+- (BOOL)hasAdditionalHeaderFile;
- (NSString*)customSuperentity;
- (NSString*)forcedCustomBaseClass;
+- (NSString*)additionalHeaderFileName;
- (void)_processPredicate:(NSPredicate*)predicate_ bindings:(NSMutableArray*)bindings_;
- (NSArray*)prettyFetchRequests;
@end
60 mogenerator.m
View
@@ -12,6 +12,9 @@
NSString *gCustomBaseClassForced;
BOOL gSwift;
+static NSString *const kAttributeValueScalarTypeKey = @"attributeValueScalarType";
+static NSString *const kAdditionalHeaderFileNameKey = @"additionalHeaderFileName";
+
@interface NSEntityDescription (fetchedPropertiesAdditions)
- (NSDictionary*)fetchedPropertiesByName;
@end
@@ -130,6 +133,10 @@ - (BOOL)hasCustomSuperentity {
}
}
+- (BOOL)hasAdditionalHeaderFile {
+ return [[[self userInfo] allKeys] containsObject:kAdditionalHeaderFileNameKey];
+}
+
- (NSString*)customSuperentity {
NSString *forcedBaseClass = [self forcedCustomBaseClass];
if (!forcedBaseClass) {
@@ -159,6 +166,11 @@ - (NSArray*)noninheritedAttributes {
return [[[self attributesByName] allValues] sortedArrayUsingDescriptors:sortDescriptors];
}
}
+
+- (NSString*)additionalHeaderFileName {
+ return [[self userInfo] objectForKey:kAdditionalHeaderFileNameKey];
+}
+
/** @TypeInfo NSAttributeDescription */
- (NSArray*)noninheritedAttributesSansType {
NSArray *attributeDescriptions = [self noninheritedAttributes];
@@ -345,27 +357,33 @@ - (BOOL)hasScalarAttributeType {
}
}
- (NSString*)scalarAttributeType {
- switch ([self attributeType]) {
- case NSInteger16AttributeType:
- return gSwift ? @"Int16" : @"int16_t";
- break;
- case NSInteger32AttributeType:
- return gSwift ? @"Int32" : @"int32_t";
- break;
- case NSInteger64AttributeType:
- return gSwift ? @"Int64" : @"int64_t";
- break;
- case NSDoubleAttributeType:
- return gSwift ? @"Double" : @"double";
- break;
- case NSFloatAttributeType:
- return gSwift ? @"Float" : @"float";
- break;
- case NSBooleanAttributeType:
- return gSwift ? @"Bool" : @"BOOL";
- break;
- default:
- return nil;
+ NSString *attributeValueScalarType = [[self userInfo] objectForKey:kAttributeValueScalarTypeKey];
+
+ if (attributeValueScalarType) {
+ return attributeValueScalarType;
+ } else {
+ switch ([self attributeType]) {
+ case NSInteger16AttributeType:
+ return gSwift ? @"Int16" : @"int16_t";
+ break;
+ case NSInteger32AttributeType:
+ return gSwift ? @"Int32" : @"int32_t";
+ break;
+ case NSInteger64AttributeType:
+ return gSwift ? @"Int64" : @"int64_t";
+ break;
+ case NSDoubleAttributeType:
+ return gSwift ? @"Double" : @"double";
+ break;
+ case NSFloatAttributeType:
+ return gSwift ? @"Float" : @"float";
+ break;
+ case NSBooleanAttributeType:
+ return gSwift ? @"Bool" : @"BOOL";
+ break;
+ default:
+ return nil;
+ }
}
}
- (NSString*)scalarAccessorMethodName {
4 templates/machine.h.motemplate
View
@@ -5,6 +5,10 @@
<$if hasCustomSuperentity$><$if hasSuperentity$>#import "<$customSuperentity$>.h"
<$else$><$if hasCustomBaseCaseImport$>#import <$baseClassImport$><$else$>#import "<$customSuperentity$>.h"<$endif$><$endif$><$endif$>
+<$if hasAdditionalHeaderFile$>
+#import "<$additionalHeaderFileName$>"
+<$endif$>
+
<$if noninheritedAttributes.@count > 0$>
extern const struct <$managedObjectClassName$>Attributes {<$foreach Attribute noninheritedAttributes do$>
<$if TemplateVar.arc$>__unsafe_unretained<$endif$> NSString *<$Attribute.name$>;<$endforeach do$>
Something went wrong with that request. Please try again.