Skip to content
This repository

Adds a nondestructive -attributeTypeName property to attributes #86

Merged
merged 4 commits into from over 2 years ago

3 participants

Rob Rix Jonathan 'Wolf' Rentzsch Joe Osborn
Rob Rix

It is usable as a replacement for both -scalarAttributeType and -objectAttributeType, and pulls from the attributeValueTypeName userInfo field if available.

Rob Rix Adds -attributeTypeName to attributes.
-attributeTypeName uses the “attributeValueTypeName” field in userInfo if available, defaulting to the scalarAttributeType if any, or else the objectAttributeType suffixed with a *.

Intended for use in the templates, deprecating both -scalarAttributeType and -objectAttributeType.

Use case: transformable attributes with “id” as their type (as opposed to the unidiomatic and frankly weird NSObject *).
01e5043
Rob Rix

I should note, this would close #85.

Rob Rix Adds scalar int*_t support.
This is done by adding -scalarAttributeTypeName, -scalarAccessorMethodName, and -scalarFactoryMethodName to NSAttributeDescription.

-scalarAttributeTypeName returns the same thing as -scalarAttributeType, except that where that uses short, int, and long long, the new method uses int16_t, int32_t, and int64_t.

To allow simple boxing and unboxing with NSNumber, -scalarAccessorMethodName and -scalarFactoryMethodName return the appropriate values for the scalar type, e.g. boolValue, longLongValue, and numberWithShort:.
fd2b556
Rob Rix

I did not mean to add the second commit to this pull request! Oh well, it provides a fix for #2. Do with it what you will! I leave my code humbly at your mercy.

Jonathan 'Wolf' Rentzsch rentzsch merged commit c82f11a into from January 06, 2012
Jonathan 'Wolf' Rentzsch rentzsch closed this January 06, 2012
Jonathan 'Wolf' Rentzsch
Owner

I found some other issues examining the test mule MOs, which I fixed up here: 25bc9fc

You may want to take a look, especially since I removed support for attributeValueTypeName attribute userInfo key since I couldn't figure how it differs from the existing attributeValueClassName support. Maybe I'm missing something?

Rob Rix

Main difference with attributeValueTypeName was that it would allow you to specify e.g. id without having the * appended onto it. attributeValueClassName seems to handle that by replacing NSObject * with id.

On the other hand, other type names would also be possible with attributeValueTypeName that aren’t with this handling of attributeValueClassName, for example Class and id<SomeProtocol>.

The intention was basically “type name, verbatim.” I still kind of feel like this is valuable for the protocol case especially (without resorting to NSObject<SomeProtocol> *), which is something I’ve occasionally wanted. What do you think?

Sorry about the NSNumber mistakes. Derp :)

Jonathan 'Wolf' Rentzsch
Owner

Ah, I see.

I don't think we need direct "type name, verbatim" support if we keep the generation code smart enough. For instance, I added NSObject* detection, replacing it with an id. So I think we already have all the bases covered with that simple heuristic.

Let me know if you have any other examples that aren't correctly handled.

Rob Rix
Jonathan 'Wolf' Rentzsch
Owner

Oh, I misunderstood your text. Yup, those aren't covered. I'm more inclined to make attributeValueClassName mean "type name, verbatim" than add another userInfo key, and accept the fact folks will need to tweak their model to add the asterisk.

Rob Rix
Rob Rix
Joe Osborn

Devil's advocate time, I guess: What's wrong with special-casing an understanding of Class and id and id<ProtocolName[,…]> when generating from an attributeValueClassName? It seems irritating to have to throw asterisks all over the place in userInfo fields in the data model.

Rob Rix
Jonathan 'Wolf' Rentzsch
Owner

@JoeOsborn: yup, I was already thinking along those lines myself. Good to hear you're on the same wavelength.

I'm thinking about something like this (in mostly-JavaScript):

if (attributeValueClassName.match(/Class/)) return attributeValueClassName; // Class
if (attributeValueClassName.match(/</)) return attributeValueClassName; // id<Protocol1,Protocol2>
if (attributeValueClassName.match(/NSObject */)) return "id"; // NSObject *
return attributeValueClassName;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 4 unique commits by 1 author.

Jan 06, 2012
Rob Rix Adds -attributeTypeName to attributes.
-attributeTypeName uses the “attributeValueTypeName” field in userInfo if available, defaulting to the scalarAttributeType if any, or else the objectAttributeType suffixed with a *.

Intended for use in the templates, deprecating both -scalarAttributeType and -objectAttributeType.

Use case: transformable attributes with “id” as their type (as opposed to the unidiomatic and frankly weird NSObject *).
01e5043
Rob Rix Adds scalar int*_t support.
This is done by adding -scalarAttributeTypeName, -scalarAccessorMethodName, and -scalarFactoryMethodName to NSAttributeDescription.

-scalarAttributeTypeName returns the same thing as -scalarAttributeType, except that where that uses short, int, and long long, the new method uses int16_t, int32_t, and int64_t.

To allow simple boxing and unboxing with NSNumber, -scalarAccessorMethodName and -scalarFactoryMethodName return the appropriate values for the scalar type, e.g. boolValue, longLongValue, and numberWithShort:.
fd2b556
Rob Rix The default template uses the new attr methods.
Also defaulted to using int*_t for scalars at the same time.
161fba5
Rob Rix Uses the correct keys for the NSNumber messages. 3f8512b
This page is out of date. Refresh to see the latest.
4  mogenerator.h
@@ -30,8 +30,12 @@
30 30
 @interface NSAttributeDescription (scalarAttributeType)
31 31
 - (BOOL)hasScalarAttributeType;
32 32
 - (NSString*)scalarAttributeType;
  33
+- (NSString*)scalarAttributeTypeName;
  34
+- (NSString*)scalarAccessorMethodName;
  35
+- (NSString*)scalarFactoryMethodName;
33 36
 - (BOOL)hasDefinedAttributeType;
34 37
 - (NSString*)objectAttributeType;
  38
+- (NSString*)attributeTypeName;
35 39
 - (BOOL)hasTransformableAttributeType;
36 40
 @end
37 41
 
78  mogenerator.m
@@ -248,6 +248,78 @@ - (NSString*)scalarAttributeType {
248 248
 			return nil;
249 249
 	}
250 250
 }
  251
+- (NSString*)scalarAttributeTypeName {
  252
+	switch ([self attributeType]) {
  253
+		case NSInteger16AttributeType:
  254
+			return @"int16_t";
  255
+			break;
  256
+		case NSInteger32AttributeType:
  257
+			return @"int32_t";
  258
+			break;
  259
+		case NSInteger64AttributeType:
  260
+			return @"int64_t";
  261
+			break;
  262
+		case NSDoubleAttributeType:
  263
+			return @"double";
  264
+			break;
  265
+		case NSFloatAttributeType:
  266
+			return @"float";
  267
+			break;
  268
+		case NSBooleanAttributeType:
  269
+			return @"BOOL";
  270
+			break;
  271
+		default:
  272
+			return nil;
  273
+	}
  274
+}
  275
+- (NSString*)scalarAccessorMethodName {
  276
+	switch ([self attributeType]) {
  277
+		case NSInteger16AttributeType:
  278
+			return @"shortValue";
  279
+			break;
  280
+		case NSInteger32AttributeType:
  281
+			return @"intValue";
  282
+			break;
  283
+		case NSInteger64AttributeType:
  284
+			return @"longLongValue";
  285
+			break;
  286
+		case NSDoubleAttributeType:
  287
+			return @"doubleValue";
  288
+			break;
  289
+		case NSFloatAttributeType:
  290
+			return @"floatValue";
  291
+			break;
  292
+		case NSBooleanAttributeType:
  293
+			return @"boolValue";
  294
+			break;
  295
+		default:
  296
+			return nil;
  297
+	}
  298
+}
  299
+- (NSString*)scalarFactoryMethodName {
  300
+	switch ([self attributeType]) {
  301
+		case NSInteger16AttributeType:
  302
+			return @"numberWithShortValue:";
  303
+			break;
  304
+		case NSInteger32AttributeType:
  305
+			return @"numberWithIntValue:";
  306
+			break;
  307
+		case NSInteger64AttributeType:
  308
+			return @"numberWithLongLongValue:";
  309
+			break;
  310
+		case NSDoubleAttributeType:
  311
+			return @"numberWithDoubleValue:";
  312
+			break;
  313
+		case NSFloatAttributeType:
  314
+			return @"numberWithFloatValue:";
  315
+			break;
  316
+		case NSBooleanAttributeType:
  317
+			return @"numberWithBoolValue:";
  318
+			break;
  319
+		default:
  320
+			return nil;
  321
+	}
  322
+}
251 323
 - (BOOL)hasDefinedAttributeType {
252 324
 	return [self attributeType] != NSUndefinedAttributeType;
253 325
 }
@@ -259,6 +331,12 @@ - (NSString*)objectAttributeType {
259 331
         return [self attributeValueClassName];
260 332
     }
261 333
 }
  334
+- (NSString*)attributeTypeName {
  335
+	return
  336
+		[[self userInfo] objectForKey:@"attributeValueTypeName"]
  337
+	?:	[self scalarAttributeTypeName]
  338
+	?:	[[self objectAttributeType] stringByAppendingString:@"*"];
  339
+}
262 340
 
263 341
 - (BOOL)hasTransformableAttributeType {
264 342
 	return ([self attributeType] == NSTransformableAttributeType);
18  templates/machine.h.motemplate
@@ -32,14 +32,14 @@ extern const struct <$managedObjectClassName$>FetchedProperties {<$foreach Fetch
32 32
 <$foreach Attribute noninheritedAttributes do$>
33 33
 <$if Attribute.hasDefinedAttributeType$>
34 34
 <$if TemplateVar.arc$>
35  
-@property (nonatomic, strong) <$Attribute.objectAttributeType$> *<$Attribute.name$>;
  35
+@property (nonatomic, strong) <$Attribute.attributeTypeName$> <$Attribute.name$>;
36 36
 <$else$>
37  
-@property (nonatomic, retain) <$Attribute.objectAttributeType$> *<$Attribute.name$>;
  37
+@property (nonatomic, retain) <$Attribute.attributeTypeName$> <$Attribute.name$>;
38 38
 <$endif$>
39 39
 <$if Attribute.hasScalarAttributeType$>
40  
-@property <$Attribute.scalarAttributeType$> <$Attribute.name$>Value;
41  
-- (<$Attribute.scalarAttributeType$>)<$Attribute.name$>Value;
42  
-- (void)set<$Attribute.name.initialCapitalString$>Value:(<$Attribute.scalarAttributeType$>)value_;
  40
+@property <$Attribute.attributeTypeName$> <$Attribute.name$>Value;
  41
+- (<$Attribute.attributeTypeName$>)<$Attribute.name$>Value;
  42
+- (void)set<$Attribute.name.initialCapitalString$>Value:(<$Attribute.attributeTypeName$>)value_;
43 43
 <$endif$>
44 44
 //- (BOOL)validate<$Attribute.name.initialCapitalString$>:(id*)value_ error:(NSError**)error_;
45 45
 <$endif$>
@@ -87,11 +87,11 @@ extern const struct <$managedObjectClassName$>FetchedProperties {<$foreach Fetch
87 87
 @interface _<$managedObjectClassName$> (CoreDataGeneratedPrimitiveAccessors)
88 88
 <$foreach Attribute noninheritedAttributes do$>
89 89
 <$if Attribute.hasDefinedAttributeType$>
90  
-- (<$Attribute.objectAttributeType$>*)primitive<$Attribute.name.initialCapitalString$>;
91  
-- (void)setPrimitive<$Attribute.name.initialCapitalString$>:(<$Attribute.objectAttributeType$>*)value;
  90
+- (<$Attribute.attributeTypeName$>)primitive<$Attribute.name.initialCapitalString$>;
  91
+- (void)setPrimitive<$Attribute.name.initialCapitalString$>:(<$Attribute.attributeTypeName$>)value;
92 92
 <$if Attribute.hasScalarAttributeType$>
93  
-- (<$Attribute.scalarAttributeType$>)primitive<$Attribute.name.initialCapitalString$>Value;
94  
-- (void)setPrimitive<$Attribute.name.initialCapitalString$>Value:(<$Attribute.scalarAttributeType$>)value_;
  93
+- (<$Attribute.attributeTypeName$>)primitive<$Attribute.name.initialCapitalString$>Value;
  94
+- (void)setPrimitive<$Attribute.name.initialCapitalString$>Value:(<$Attribute.attributeTypeName$>)value_;
95 95
 <$endif$>
96 96
 <$endif$>
97 97
 <$endforeach do$>
14  templates/machine.m.motemplate
@@ -56,22 +56,22 @@ const struct <$managedObjectClassName$>FetchedProperties <$managedObjectClassNam
56 56
 
57 57
 <$if Attribute.hasScalarAttributeType$>
58 58
 
59  
-- (<$Attribute.scalarAttributeType$>)<$Attribute.name$>Value {
  59
+- (<$Attribute.attributeTypeName$>)<$Attribute.name$>Value {
60 60
 	NSNumber *result = [self <$Attribute.name$>];
61  
-	return [result <$Attribute.scalarAttributeType.camelCaseString$>Value];
  61
+	return [result <$Attribute.scalarAccessorMethodName$>];
62 62
 }
63 63
 
64  
-- (void)set<$Attribute.name.initialCapitalString$>Value:(<$Attribute.scalarAttributeType$>)value_ {
65  
-	[self set<$Attribute.name.initialCapitalString$>:[NSNumber numberWith<$Attribute.scalarAttributeType.camelCaseString.initialCapitalString$>:value_]];
  64
+- (void)set<$Attribute.name.initialCapitalString$>Value:(<$Attribute.attributeTypeName$>)value_ {
  65
+	[self set<$Attribute.name.initialCapitalString$>:[NSNumber <$Attribute.scalarFactoryMethodName$>value_]];
66 66
 }
67 67
 
68  
-- (<$Attribute.scalarAttributeType$>)primitive<$Attribute.name.initialCapitalString$>Value {
  68
+- (<$Attribute.attributeTypeName$>)primitive<$Attribute.name.initialCapitalString$>Value {
69 69
 	NSNumber *result = [self primitive<$Attribute.name.initialCapitalString$>];
70  
-	return [result <$Attribute.scalarAttributeType.camelCaseString$>Value];
  70
+	return [result <$Attribute.scalarAccessorMethodName$>];
71 71
 }
72 72
 
73 73
 - (void)setPrimitive<$Attribute.name.initialCapitalString$>Value:(<$Attribute.scalarAttributeType$>)value_ {
74  
-	[self setPrimitive<$Attribute.name.initialCapitalString$>:[NSNumber numberWith<$Attribute.scalarAttributeType.camelCaseString.initialCapitalString$>:value_]];
  74
+	[self setPrimitive<$Attribute.name.initialCapitalString$>:[NSNumber <$Attribute.scalarFactoryMethodName$>value_]];
75 75
 }
76 76
 <$endif$>
77 77
 <$endif$>
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.