Skip to content

Commit

Permalink
Improved isUnsigned to check attribute minimum more carefully
Browse files Browse the repository at this point in the history
The previous implementation checked the validation predicate for a string containing literally ">= 0".  But if an attribute's minimum was, for example, 1 instead of 0, the test would fail because the string would be ">= 1".  But when a minimum is 1 we still want to use `unsigned`, but weren't getting `unsigned` because of this.

Updated the code to instead look for the string "SELF >=" and then inspect the number that comes after (if any). Then we can in fact correctly check that the minimus is >= 0, thus allowing `unsigned` to be used in those cases too.
  • Loading branch information
seanm committed Sep 30, 2024
1 parent 2d7f9b4 commit af066ac
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions mogenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,23 @@ - (NSArray*)prettyFetchRequests {
@implementation NSAttributeDescription (typing)
- (BOOL)isUnsigned
{
BOOL hasMin = NO;
BOOL isUnsigned = NO;
for (NSPredicate *pred in [self validationPredicates]) {
if ([pred.predicateFormat containsString:@">= 0"]) {
hasMin = YES;
NSString* formatString = pred.predicateFormat;
NSRange range = [formatString rangeOfString:@"SELF >="];
if (range.location != NSNotFound) {
NSUInteger startIndex = range.location + range.length;
NSString *minString = [formatString substringFromIndex:startIndex];
NSScanner *scanner = [NSScanner scannerWithString:minString];
NSInteger minValue;
BOOL success = [scanner scanInteger:&minValue];
if (success && (minValue >= 0)) {
isUnsigned = YES;
break;
}
}
}
return hasMin;
return isUnsigned;
}

- (BOOL)hasScalarAttributeType {
Expand Down

0 comments on commit af066ac

Please sign in to comment.