Skip to content

Commit

Permalink
Annotate deprecated Objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvl committed Sep 6, 2023
1 parent 5181580 commit e2941fa
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
51 changes: 49 additions & 2 deletions Tools/ServiceGenerator/SGGenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
static NSString *kReturnsSchemaParameterKey = @"returnsSchema";
static NSString *kAllMethodObjectParametersKey = @"allMethodObjectParameters";
static NSString *kAllMethodObjectParameterRefsKey = @"allMethodObjectParameterRefs";
static NSString *kHasDeprecatedSchemaKey = @"hasDeprecatedSchemaKey";
static NSString *kCleanedRootURLStringKey = @"cleanedRootURLString";
static NSString *kResumableUploadPathKey = @"resumableUploadPath";
static NSString *kSimpleUploadPathKey = @"simpleUploadPath";
Expand Down Expand Up @@ -96,6 +97,7 @@ @interface GTLRDiscovery_RestDescription (SGGeneratorAdditions)
@property(readonly) NSDictionary *sg_objectEnumsMap;
@property(readonly) NSArray *sg_allSchemas;
@property(readonly) NSArray *sg_topLevelObjectSchemas;
@property(readonly) BOOL sg_hasDeprecatedSchema;
@property(readonly) NSArray *sg_allMethodObjectParameterReferences;
@property(readonly) NSString *sg_resumableUploadPath;
@property(readonly) NSString *sg_simpleUploadPath;
Expand Down Expand Up @@ -1287,6 +1289,7 @@ - (NSString *)objectsHeader {
}

SGClangDirectives *clangDirectives = [SGClangDirectives disabledDocumentation];
clangDirectives.disableDeprecatedDeclarations = self.api.sg_hasDeprecatedSchema;
[parts addObject:clangDirectives.start];

[parts addObject:@"NS_ASSUME_NONNULL_BEGIN\n"];
Expand Down Expand Up @@ -1339,6 +1342,14 @@ - (NSString *)objectsSource {
[parts addObjectsFromArray:blocks];
}

SGClangDirectives *clangDirectives = [[SGClangDirectives alloc] init];
BOOL hasDeprecatedSchema = self.api.sg_hasDeprecatedSchema;
clangDirectives.disableDeprecatedImplementations = hasDeprecatedSchema;
clangDirectives.disableDeprecatedDeclarations = hasDeprecatedSchema;
if (clangDirectives.hasDirectives) {
[parts addObject:clangDirectives.start];
}

NSMutableArray *classParts = [NSMutableArray array];
for (GTLRDiscovery_JsonSchema *schema in self.api.sg_topLevelObjectSchemas) {
NSString *objectClassStr = [self generateObjectClassForSchema:schema
Expand All @@ -1354,6 +1365,10 @@ - (NSString *)objectsSource {
// Two blank lines between classes.
[parts addObject:[classParts componentsJoinedByString:@"\n\n"]];

if (clangDirectives.hasDirectives) {
[parts addObject:clangDirectives.end];
}

return [parts componentsJoinedByString:@"\n"];
}

Expand Down Expand Up @@ -2403,8 +2418,9 @@ - (NSString *)generateObjectClassForSchema:(GTLRDiscovery_JsonSchema *)schema
} else if (isTopLevelArrayResult) {
baseClass = kResultArrayClass;
}
atBlock = [NSString stringWithFormat:@"@interface %@ : %@\n",
schemaClassName, baseClass];
NSString *maybeDeprecated = schema.deprecated.boolValue ? kDeprecatedWithNewline : @"";
atBlock = [NSString stringWithFormat:@"%@@interface %@ : %@\n",
maybeDeprecated, schemaClassName, baseClass];
} else {
atBlock = [NSString stringWithFormat:@"@implementation %@\n",
schemaClassName];
Expand Down Expand Up @@ -3313,6 +3329,37 @@ - (NSArray *)sg_topLevelObjectSchemas {
return result;
}

- (BOOL)sg_hasDeprecatedSchema {
// This could be expanded to deal with if there are referenced as types
// on other schema vs. queries to only add the guards when needed, but
// for now just generally insert them when any schema was deprecated.
NSNumber *result = [self sg_propertyForKey:kHasDeprecatedSchemaKey];
if (result == nil) {
BOOL hasDeprecated = NO;

for (GTLRDiscovery_JsonSchema *schema in self.sg_topLevelObjectSchemas) {
if (schema.deprecated.boolValue) {
hasDeprecated = YES;
break;
}

for (GTLRDiscovery_JsonSchema *subSchema in schema.sg_childObjectSchemas) {
if (subSchema.deprecated.boolValue) {
hasDeprecated = YES;
break;
}
}
if (hasDeprecated) {
break;
}
}

result = [NSNumber numberWithBool:hasDeprecated];
[self sg_setProperty:result forKey:kHasDeprecatedSchemaKey];
}
return [result boolValue];
}

// These are resolved schema references in the method parameters (refs or
// inline).
- (NSArray *)sg_allMethodObjectParameterReferences {
Expand Down
2 changes: 2 additions & 0 deletions Tools/ServiceGenerator/SGUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@

// -Wdeprecated-implementations
@property BOOL disableDeprecatedImplementations;
// -Wdeprecated-declarations
@property BOOL disableDeprecatedDeclarations;
// -Wdocumentation
@property BOOL disableDocumentation;

Expand Down
14 changes: 14 additions & 0 deletions Tools/ServiceGenerator/SGUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ typedef NS_OPTIONS(NSInteger, SGCDFlags) {
SGCDFlagsNone = 0,
SGCDFlagsDocumentation = 1 << 0,
SGCDFlagsDeprecatedImplementations = 1 << 1,
SGCDFlagsDeprecatedDeclarations = 1 << 2,
};

@implementation SGClangDirectives {
Expand All @@ -700,6 +701,14 @@ - (void)setDisableDeprecatedImplementations:(BOOL)value {
SET_FLAG(SGCDFlagsDeprecatedImplementations, value);
}

- (BOOL)disableDeprecatedDeclarations {
return IS_FLAG_SET(SGCDFlagsDeprecatedDeclarations);
}

- (void)setDisableDeprecatedDeclarations:(BOOL)value {
SET_FLAG(SGCDFlagsDeprecatedDeclarations, value);
}

- (BOOL)disableDocumentation {
return IS_FLAG_SET(SGCDFlagsDocumentation);
}
Expand Down Expand Up @@ -740,6 +749,11 @@ - (NSString *)start {
[result appendString:@"#pragma clang diagnostic push\n"];
}

if (self.disableDeprecatedDeclarations) {
[result appendString:
@"#pragma clang diagnostic ignored \"-Wdeprecated-declarations\"\n"];
}

if (self.disableDeprecatedImplementations) {
[result appendString:
@"#pragma clang diagnostic ignored \"-Wdeprecated-implementations\"\n"];
Expand Down

0 comments on commit e2941fa

Please sign in to comment.