Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Generation of Entity User Info Key/Value pairs as const Structs #131

Merged
merged 5 commits into from

3 participants

@echoz

Allows generation of the Key Value pairs for each entity as const structs within the machine.h and machine.m files allow easy access of such constants in code.

Changed:
1. Added methods to generate the user info key value pairs as a method that Misc Merge can understand.
2. Modified machine.h and machine.m templates to allow generation of user info key/value pair const structs.

@echoz echoz commented on the diff
mogenerator.m
@@ -126,6 +142,18 @@ - (NSArray*)noninheritedRelationships {
return [[[self relationshipsByName] allValues] sortedArrayUsingDescriptors:sortDescriptors];
}
}
+/** @TypeInfo NSEntityUserInfoDescription */
@echoz
echoz added a note

Not too sure what to name this but to keep with convention, this was decided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@rentzsch
Owner

Interesting. I'd accept this Pull Request except it sounds like empty structs trigger warnings in pedantic mode (see #133). Could you enhance the template to conditionally emit the userinfo struct only if the dictionary isn't empty?

echoz added some commits
@echoz echoz - Detects if the user info dictionary is empty and provides a way for…
… Misc Merge to use as a conditional.

- Updated templates to not emit structs when the user info dictionary is empty.
8281ff3
@echoz echoz Forgotten to include in the @interface section 4ca4405
@rentzsch
Owner

So I added a UserInfo entry to my ParentMO, and it generated this code:

const struct ParentMOUserInfo ParentMOUserInfo = {
    .MyUserInfoKey = @"MyUserInfoValue",
};

Is this correct? I expected a one-to-one key name mapping, not the value. For example, here's are the generated attributes:

const struct ParentMOAttributes ParentMOAttributes = {
    .myBinaryData = @"myBinaryData",
    .myBoolean = @"myBoolean",
    .myDate = @"myDate",
    .myDecimal = @"myDecimal",
    .myDouble = @"myDouble",
    .myFloat = @"myFloat",
    .myInt16 = @"myInt16",
    .myInt16Transient = @"myInt16Transient",
    .myInt32 = @"myInt32",
    .myInt64 = @"myInt64",
    .myString = @"myString",
    .myTransformableSansClassName = @"myTransformableSansClassName",
    .myTransformableWithClassName = @"myTransformableWithClassName",
    .myTransformableWithProtocol = @"myTransformableWithProtocol",
    .parentName = @"parentName",
};

Notice how the struct names are pretty much just symbols for the string constant.

@echoz

Hi rentzsch, that is the correct behavior. The assumption here being that the values in the model's user info dictionary is pretty much constant (can't change it at runtime anyways). This allows one to access those key value pairs without needing to spin up the managed model or a managed object connected to a context and in turn a model in order to access those key value pairs.

@rentzsch
Owner

Thanks for the clarification. What are you using entity userinfos for, anyway?

@echoz

In my use case it was for contextual configuration information about entities that other systems will read (ie. parsing json dictionaries to managed objects based on a configured primary key that is set to a property of that entity).

Another use case is to set the REST based URL that an entity is related to which once again a networking stack can use to automatically persist data to or from the REST resource.

@rentzsch
Owner

Thanks, good ideas.

@rentzsch rentzsch merged commit 03bac52 into from
@rentzsch
Owner

Thanks! Merged. Sorry for the previous delay, somehow I lost track of this Pull Request.

@echoz

No worries. Just glad to see it merged. :)

@batkuip

Can you fix this to properly handle keys like "com.apple.syncservices.Syncable"? CoreData added this one to all my entities automatically unfortunately this causes a complication failure since the dots are not parse out.

This was referenced
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 2, 2012
  1. @echoz

    get user info key value pairs

    echoz authored
  2. @echoz
  3. @echoz

    forgotten a simple =

    echoz authored
Commits on Jan 16, 2013
  1. @echoz

    - Detects if the user info dictionary is empty and provides a way for…

    echoz authored
    … Misc Merge to use as a conditional.
    
    - Updated templates to not emit structs when the user info dictionary is empty.
  2. @echoz
This page is out of date. Refresh to see the latest.
View
33 mogenerator.m
@@ -30,6 +30,27 @@ - (NSDictionary *)fetchedPropertiesByName
}
@end
+@interface NSEntityDescription (userInfoAdditions)
+- (BOOL)hasUserInfoKeys;
+- (NSDictionary *)userInfoByKeys;
+@end
+
+@implementation NSEntityDescription (userInfoAdditions)
+- (BOOL)hasUserInfoKeys {
+ return ([self.userInfo count] > 0);
+}
+
+- (NSDictionary *)userInfoByKeys
+{
+ NSMutableDictionary *userInfoByKeys = [NSMutableDictionary dictionary];
+
+ for (NSString *key in self.userInfo)
+ [userInfoByKeys setObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"key", [self.userInfo objectForKey:key], @"value", nil] forKey:key];
+
+ return userInfoByKeys;
+}
+@end
+
@implementation NSManagedObjectModel (entitiesWithACustomSubclassVerbose)
- (NSArray*)entitiesWithACustomSubclassInConfiguration:(NSString *)configuration_ verbose:(BOOL)verbose_ {
NSMutableArray *result = [NSMutableArray array];
@@ -126,6 +147,18 @@ - (NSArray*)noninheritedRelationships {
return [[[self relationshipsByName] allValues] sortedArrayUsingDescriptors:sortDescriptors];
}
}
+/** @TypeInfo NSEntityUserInfoDescription */
@echoz
echoz added a note

Not too sure what to name this but to keep with convention, this was decided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+- (NSArray*)userInfoKeyValues {
+ NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"key" ascending:YES]];
+ NSEntityDescription *superentity = [self superentity];
+ if (superentity) {
+ NSMutableArray *result = [[[[self userInfoByKeys] allValues] mutableCopy] autorelease];
+ [result removeObjectsInArray:[[superentity userInfoByKeys] allValues]];
+ return [result sortedArrayUsingDescriptors:sortDescriptors];
+ } else {
+ return [[[self userInfoByKeys] allValues] sortedArrayUsingDescriptors:sortDescriptors];
+ }
+}
/** @TypeInfo NSFetchedPropertyDescription */
- (NSArray*)noninheritedFetchedProperties {
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
View
6 templates/machine.h.motemplate
@@ -16,6 +16,12 @@ extern const struct <$managedObjectClassName$>FetchedProperties {<$foreach Fetch
<$if TemplateVar.arc$>__unsafe_unretained<$endif$> NSString *<$FetchedProperty.name$>;<$endforeach do$>
} <$managedObjectClassName$>FetchedProperties;
+<$if hasUserInfoKeys$>
+extern const struct <$managedObjectClassName$>UserInfo {<$foreach UserInfo userInfoKeyValues do$>
+ <$if TemplateVar.arc$>__unsafe_unretained<$endif$> NSString *<$UserInfo.key$>;<$endforeach do$>
+} <$managedObjectClassName$>UserInfo;
+<$endif$>
+
<$foreach Relationship noninheritedRelationships do$>@class <$Relationship.destinationEntity.managedObjectClassName$>;
<$endforeach do$>
<$foreach Attribute noninheritedAttributes do$><$if Attribute.hasTransformableAttributeType$>@class <$Attribute.objectAttributeClassName$>;<$endif$>
View
6 templates/machine.m.motemplate
@@ -15,6 +15,12 @@ const struct <$managedObjectClassName$>FetchedProperties <$managedObjectClassNam
.<$FetchedProperty.name$> = @"<$FetchedProperty.name$>",<$endforeach do$>
};
+<$if hasUserInfoKeys$>
+const struct <$managedObjectClassName$>UserInfo <$managedObjectClassName$>UserInfo = {<$foreach UserInfo userInfoKeyValues do$>
+ .<$UserInfo.key$> = @"<$UserInfo.value$>",<$endforeach do$>
+};
+<$endif$>
+
@implementation <$managedObjectClassName$>ID
@end
Something went wrong with that request. Please try again.