Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

objective-c: run clang-format on Objective-C files #54

Merged
merged 4 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .clang-format

This file was deleted.

28 changes: 28 additions & 0 deletions .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 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should fix the upstream formatter to make this configurable so we can ignore this check on particular paths. Can you add a TODO in check_format.sh?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, done


@interface AppDelegate : UIResponder <UIApplicationDelegate>

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

@end
4 changes: 3 additions & 1 deletion 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 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 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