From ab74e666b226ad648f1c04da4107cdc59ef606ae Mon Sep 17 00:00:00 2001 From: Michael Rebello Date: Fri, 7 Jun 2019 14:41:20 -0700 Subject: [PATCH] objective-c: run clang-format on Objective-C files (#54) - Replace the `.clang-format` symlink to upstream Envoy with a custom file for this repository - Rules in this `.clang-format` file mirror upstream and add Objective-C rules. 2 spaces were selected in order to match upstream's C++ formatting - Update the format check script to ignore `Envoy.framework` and start linting Objective-C - Run the formatter against Objective-C files https://github.com/lyft/envoy-mobile/issues/53 Signed-off-by: Michael Rebello Signed-off-by: JP Simard --- mobile/.clang-format | 29 +++- .../objective-c/hello_world/AppDelegate.h | 4 +- .../objective-c/hello_world/ViewController.h | 4 +- .../objective-c/hello_world/ViewController.mm | 135 ++++++++++-------- mobile/tools/check_format.sh | 5 +- 5 files changed, 109 insertions(+), 68 deletions(-) mode change 120000 => 100644 mobile/.clang-format diff --git a/mobile/.clang-format b/mobile/.clang-format deleted file mode 120000 index 30fec7da9d92..000000000000 --- a/mobile/.clang-format +++ /dev/null @@ -1 +0,0 @@ -envoy/.clang-format \ No newline at end of file diff --git a/mobile/.clang-format b/mobile/.clang-format new file mode 100644 index 000000000000..2e4ab5b4479e --- /dev/null +++ b/mobile/.clang-format @@ -0,0 +1,28 @@ +--- +Language: Cpp +AccessModifierOffset: -2 +ColumnLimit: 100 +DerivePointerAlignment: false +PointerAlignment: Left +SortIncludes: false +... + +--- +Language: ObjC +AccessModifierOffset: -2 +ColumnLimit: 100 +DerivePointerAlignment: false +IndentWidth: 2 +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true +PointerAlignment: Left +SortIncludes: false +... + +--- +Language: Proto +ColumnLimit: 100 +SpacesInContainerLiterals: false +AllowShortFunctionsOnASingleLine: false +... diff --git a/mobile/examples/objective-c/hello_world/AppDelegate.h b/mobile/examples/objective-c/hello_world/AppDelegate.h index e1fe1bf304bb..0eb7c9e802a9 100644 --- a/mobile/examples/objective-c/hello_world/AppDelegate.h +++ b/mobile/examples/objective-c/hello_world/AppDelegate.h @@ -1,8 +1,10 @@ #import #import +// NOLINT(namespace-envoy) + @interface AppDelegate : UIResponder -@property (strong, nonatomic) UIWindow *window; +@property (strong, nonatomic) UIWindow* window; @end diff --git a/mobile/examples/objective-c/hello_world/ViewController.h b/mobile/examples/objective-c/hello_world/ViewController.h index 922e5721e8f2..be0821377249 100644 --- a/mobile/examples/objective-c/hello_world/ViewController.h +++ b/mobile/examples/objective-c/hello_world/ViewController.h @@ -1,4 +1,6 @@ #import -@interface ViewController: UITableViewController +// NOLINT(namespace-envoy) + +@interface ViewController : UITableViewController @end diff --git a/mobile/examples/objective-c/hello_world/ViewController.mm b/mobile/examples/objective-c/hello_world/ViewController.mm index b4027ecf06e2..4dacc054bcb0 100644 --- a/mobile/examples/objective-c/hello_world/ViewController.mm +++ b/mobile/examples/objective-c/hello_world/ViewController.mm @@ -3,14 +3,14 @@ #pragma mark - Constants -NSString *_CELL_ID = @"cell-id"; -NSString *_ENDPOINT = @"http://0.0.0.0:9001/api.lyft.com/static/demo/hello_world.txt"; +NSString* _CELL_ID = @"cell-id"; +NSString* _ENDPOINT = @"http://0.0.0.0:9001/api.lyft.com/static/demo/hello_world.txt"; #pragma mark - ResponseValue -@interface ResponseValue: NSObject -@property (nonatomic, strong) NSString *body; -@property (nonatomic, strong) NSString *serverHeader; +@interface ResponseValue : NSObject +@property (nonatomic, strong) NSString* body; +@property (nonatomic, strong) NSString* serverHeader; @end @implementation ResponseValue @@ -19,8 +19,8 @@ @implementation ResponseValue #pragma mark - ViewController @interface ViewController () -@property (nonatomic, strong) NSMutableArray *responses; -@property (nonatomic, weak) NSTimer *requestTimer; +@property (nonatomic, strong) NSMutableArray* responses; +@property (nonatomic, weak) NSTimer* requestTimer; @end @implementation ViewController @@ -28,86 +28,95 @@ @implementation ViewController #pragma mark - Lifecycle - (instancetype)init { - self = [super init]; - if (self) { - self.responses = [NSMutableArray new]; - self.tableView.allowsSelection = FALSE; - } - return self; + self = [super init]; + if (self) { + self.responses = [NSMutableArray new]; + self.tableView.allowsSelection = FALSE; + } + return self; } - (void)dealloc { - [self.requestTimer invalidate]; - self.requestTimer = nil; + [self.requestTimer invalidate]; + self.requestTimer = nil; } - (void)viewDidLoad { - [super viewDidLoad]; - [self startRequests]; + [super viewDidLoad]; + [self startRequests]; } #pragma mark - Requests - (void)startRequests { - // Note that the first delay will give Envoy time to start up. - self.requestTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(performRequest) userInfo:nil repeats:true]; + // Note that the first delay will give Envoy time to start up. + self.requestTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 + target:self + selector:@selector(performRequest) + userInfo:nil + repeats:true]; } - (void)performRequest { - NSURLSession *session = [NSURLSession sharedSession]; - NSURL *url = [NSURL URLWithString:_ENDPOINT]; - NSURLRequest *request = [NSURLRequest requestWithURL:url]; - NSLog(@"Starting request to '%@'", url.path); - - __weak ViewController *weakSelf = self; - NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { - if (error == nil && [(NSHTTPURLResponse *)response statusCode] == 200) { - [weakSelf handleResponse:(NSHTTPURLResponse *)response data:data]; - } else { - NSLog(@"Received error: %@", error); - } - }]; - [task resume]; + NSURLSession* session = [NSURLSession sharedSession]; + NSURL* url = [NSURL URLWithString:_ENDPOINT]; + NSURLRequest* request = [NSURLRequest requestWithURL:url]; + NSLog(@"Starting request to '%@'", url.path); + + __weak ViewController* weakSelf = self; + NSURLSessionDataTask* task = + [session dataTaskWithRequest:request + completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { + if (error == nil && [(NSHTTPURLResponse*)response statusCode] == 200) { + [weakSelf handleResponse:(NSHTTPURLResponse*)response data:data]; + } else { + NSLog(@"Received error: %@", error); + } + }]; + [task resume]; } -- (void)handleResponse:(NSHTTPURLResponse *)response data:(NSData *)data { - NSString *body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; - if (body == nil || response == nil) { - NSLog(@"Failed to deserialize response string"); - return; - } - - ResponseValue *value = [ResponseValue new]; - value.body = body; - value.serverHeader = [[response allHeaderFields] valueForKey:@"Server"]; - - NSLog(@"Response:\n%ld bytes\n%@\n%@", data.length, body, [response allHeaderFields]); - dispatch_async(dispatch_get_main_queue(), ^{ - [self.responses addObject:value]; - [self.tableView reloadData]; - }); +- (void)handleResponse:(NSHTTPURLResponse*)response data:(NSData*)data { + NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + if (body == nil || response == nil) { + NSLog(@"Failed to deserialize response string"); + return; + } + + ResponseValue* value = [ResponseValue new]; + value.body = body; + value.serverHeader = [[response allHeaderFields] valueForKey:@"Server"]; + + NSLog(@"Response:\n%ld bytes\n%@\n%@", data.length, body, [response allHeaderFields]); + dispatch_async(dispatch_get_main_queue(), ^{ + [self.responses addObject:value]; + [self.tableView reloadData]; + }); } #pragma mark - UITableView -- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { - return 1; +- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView { + return 1; } -- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return self.responses.count; +- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { + return self.responses.count; } -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_CELL_ID]; - if (cell == nil) { - cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:_CELL_ID]; - } - - ResponseValue *response = self.responses[indexPath.row]; - cell.textLabel.text = [NSString stringWithFormat:@"Response: %@", response.body]; - cell.detailTextLabel.text = [NSString stringWithFormat:@"'Server' header: %@", response.serverHeader]; - return cell; +- (UITableViewCell*)tableView:(UITableView*)tableView + cellForRowAtIndexPath:(NSIndexPath*)indexPath { + UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:_CELL_ID]; + if (cell == nil) { + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle + reuseIdentifier:_CELL_ID]; + } + + ResponseValue* response = self.responses[indexPath.row]; + cell.textLabel.text = [NSString stringWithFormat:@"Response: %@", response.body]; + cell.detailTextLabel.text = + [NSString stringWithFormat:@"'Server' header: %@", response.serverHeader]; + return cell; } @end diff --git a/mobile/tools/check_format.sh b/mobile/tools/check_format.sh index 5fe4ca27231f..cd195d49942f 100755 --- a/mobile/tools/check_format.sh +++ b/mobile/tools/check_format.sh @@ -12,10 +12,11 @@ fi # TODO(mattklein123): WORKSPACE is excluded due to warning about @bazel_tools reference. Fix here # or in the upstream checker. -# TODO(mattklein123): Objective-C is excluded because the clang-format setup is not correct. Fix. +# TODO(mattklein123): Add support upstream for whitelisting paths that don't need to have +# NOLINT(namespace-envoy), such as Objective-C. # TODO(mattklein123): We don't need envoy_package() in various files. Somehow fix in upstream # checker. envoy/tools/check_format.py \ - --add-excluded-prefixes ./envoy/ ./envoy_build_config/extensions_build_config.bzl ./WORKSPACE ./examples/objective-c/ \ + --add-excluded-prefixes ./envoy/ ./envoy_build_config/extensions_build_config.bzl ./WORKSPACE ./dist/Envoy.framework/ \ --skip_envoy_build_rule_check "$ENVOY_FORMAT_ACTION" envoy/tools/format_python_tools.sh check