Skip to content

Commit

Permalink
objective-c: run clang-format on Objective-C files (#54)
Browse files Browse the repository at this point in the history
- 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

envoyproxy/envoy-mobile#53

Signed-off-by: Michael Rebello <mrebello@lyft.com>
Signed-off-by: JP Simard <jp@jpsim.com>
  • Loading branch information
rebello95 authored and jpsim committed Nov 28, 2022
1 parent 2d520e5 commit ffee372
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 68 deletions.
1 change: 0 additions & 1 deletion mobile/.clang-format

This file was deleted.

28 changes: 28 additions & 0 deletions mobile/.clang-format
Original file line number Diff line number Diff line change
@@ -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
...
4 changes: 3 additions & 1 deletion mobile/examples/objective-c/hello_world/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

// NOLINT(namespace-envoy)

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIWindow* window;

@end
4 changes: 3 additions & 1 deletion mobile/examples/objective-c/hello_world/ViewController.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#import <UIKit/UIKit.h>

@interface ViewController: UITableViewController
// NOLINT(namespace-envoy)

@interface ViewController : UITableViewController
@end
135 changes: 72 additions & 63 deletions mobile/examples/objective-c/hello_world/ViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,95 +19,104 @@ @implementation ResponseValue
#pragma mark - ViewController

@interface ViewController ()
@property (nonatomic, strong) NSMutableArray<ResponseValue *> *responses;
@property (nonatomic, weak) NSTimer *requestTimer;
@property (nonatomic, strong) NSMutableArray<ResponseValue*>* responses;
@property (nonatomic, weak) NSTimer* requestTimer;
@end

@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
5 changes: 3 additions & 2 deletions mobile/tools/check_format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ffee372

Please sign in to comment.