Skip to content

Commit

Permalink
Add support for fallback object value of nil
Browse files Browse the repository at this point in the history
Summary: This lets callers provide a fallback object value (e.g. `[NSNull null]`) for safety when the returned object is required to be non-null.

Reviewed By: natestedman

Differential Revision: D10504059

fbshipit-source-id: 1217755e84a927fe8cfbd0e944af55613f1f7c75
  • Loading branch information
chritto authored and facebook-github-bot committed Oct 23, 2018
1 parent a30e87f commit 2b4ee9b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions remodel-plugin/src/plugins/iglistdiffable-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ function formattedStringValueForIvarWithFormatSpecifier(iVarString:string, strin
return "[NSString stringWithFormat:@\"" + stringFormatSpecifier + "\", " + castString + iVarString + "]";
}

function objectValueForAttribute(attribute:ObjectSpec.Attribute):string {
function nullableObjectValueWithFallback(objectValue:string, optionalFallback:string=null) {
return (optionalFallback === null) ? objectValue : `${objectValue} ?: ${optionalFallback}`;
}

function objectValueForAttribute(attribute:ObjectSpec.Attribute, optionalFallback:string=null):string {
const iVarString:string = ObjectSpecCodeUtils.ivarForAttribute(attribute);
const type:ObjC.Type = ObjectSpecCodeUtils.computeTypeOfAttribute(attribute);

Expand All @@ -49,7 +53,7 @@ function objectValueForAttribute(attribute:ObjectSpec.Attribute):string {
return formattedStringValueForIvarWithFormatSpecifier(iVarString, "%@");
},
NSObject: function() {
return iVarString;
return nullableObjectValueWithFallback(iVarString, optionalFallback);
},
BOOL: function() {
return iVarString + " ? @\"YES\" : @\"NO\"";
Expand Down Expand Up @@ -112,7 +116,7 @@ function objectValueForAttribute(attribute:ObjectSpec.Attribute):string {
return formattedStringValueForIvarWithFormatSpecifier(iVarString, "%@");
},
unmatchedType: function() {
return "self";
return nullableObjectValueWithFallback('self', optionalFallback);
}
}, type);
}
Expand Down

0 comments on commit 2b4ee9b

Please sign in to comment.