-
Notifications
You must be signed in to change notification settings - Fork 5.5k
ios: fix platform key value store #24532
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -403,8 +403,8 @@ static void ios_kv_store_save(envoy_data native_key, envoy_data native_value, co | |
| NSString *key = [[NSString alloc] initWithBytes:native_key.bytes | ||
| length:native_key.length | ||
| encoding:NSUTF8StringEncoding]; | ||
| NSString *value = [[NSString alloc] initWithBytes:native_key.bytes | ||
| length:native_key.length | ||
| NSString *value = [[NSString alloc] initWithBytes:native_value.bytes | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the actual bug fix.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! looks like it was a copy-paste bug. |
||
| length:native_value.length | ||
| encoding:NSUTF8StringEncoding]; | ||
| [keyValueStore saveValue:value toKey:key]; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #import "library/common/types/c_types.h" | ||
| #import "library/objective-c/EnvoyKeyValueStore.h" | ||
|
|
||
| /// Save a value to the key value store that's passed as an opaque context. | ||
| void ios_kv_store_save(envoy_data native_key, envoy_data native_value, const void* context); | ||
|
|
||
| /// Read a value from the key value store that's passed as an opaque context. | ||
| envoy_data ios_kv_store_read(envoy_data native_key, const void* context); | ||
|
|
||
| /// Remove a value from the key value store that's passed as an opaque context. | ||
| void ios_kv_store_remove(envoy_data native_key, const void* context); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #import "library/objective-c/EnvoyKeyValueStoreBridgeImpl.h" | ||
|
|
||
| #import "library/objective-c/EnvoyBridgeUtility.h" | ||
|
|
||
| envoy_data ios_kv_store_read(envoy_data native_key, const void *context) { | ||
| // This code block runs inside the Envoy event loop. Therefore, an explicit autoreleasepool block | ||
| // is necessary to act as a breaker for any Objective-C allocation that happens. | ||
| @autoreleasepool { | ||
| id<EnvoyKeyValueStore> keyValueStore = (__bridge id<EnvoyKeyValueStore>)context; | ||
| NSString *key = [[NSString alloc] initWithBytes:native_key.bytes | ||
| length:native_key.length | ||
| encoding:NSUTF8StringEncoding]; | ||
| NSString *value = [keyValueStore readValueForKey:key]; | ||
| return value != nil ? toManagedNativeString(value) : envoy_nodata; | ||
| } | ||
| } | ||
|
|
||
| void ios_kv_store_save(envoy_data native_key, envoy_data native_value, const void *context) { | ||
| // This code block runs inside the Envoy event loop. Therefore, an explicit autoreleasepool block | ||
| // is necessary to act as a breaker for any Objective-C allocation that happens. | ||
| @autoreleasepool { | ||
| id<EnvoyKeyValueStore> keyValueStore = (__bridge id<EnvoyKeyValueStore>)context; | ||
| NSString *key = [[NSString alloc] initWithBytes:native_key.bytes | ||
| length:native_key.length | ||
| encoding:NSUTF8StringEncoding]; | ||
| NSString *value = [[NSString alloc] initWithBytes:native_value.bytes | ||
| length:native_value.length | ||
| encoding:NSUTF8StringEncoding]; | ||
| [keyValueStore saveValue:value toKey:key]; | ||
| } | ||
| } | ||
|
|
||
| void ios_kv_store_remove(envoy_data native_key, const void *context) { | ||
| // This code block runs inside the Envoy event loop. Therefore, an explicit autoreleasepool block | ||
| // is necessary to act as a breaker for any Objective-C allocation that happens. | ||
| @autoreleasepool { | ||
| id<EnvoyKeyValueStore> keyValueStore = (__bridge id<EnvoyKeyValueStore>)context; | ||
| NSString *key = [[NSString alloc] initWithBytes:native_key.bytes | ||
| length:native_key.length | ||
| encoding:NSUTF8StringEncoding]; | ||
| [keyValueStore removeKey:key]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
mobile/test/objective-c/EnvoyKeyValueStoreBridgeImplTest.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #import <XCTest/XCTest.h> | ||
|
|
||
| #import "library/objective-c/EnvoyKeyValueStoreBridgeImpl.h" | ||
| #import "library/objective-c/EnvoyBridgeUtility.h" | ||
|
|
||
| @interface TestKeyValueStore : NSObject <EnvoyKeyValueStore> | ||
|
|
||
| @property (nonatomic, copy) NSString * (^readValueBlock)(NSString *key); | ||
| @property (nonatomic, copy) void (^saveValueBlock)(NSString *key, NSString *value); | ||
| @property (nonatomic, copy) void (^removeValueBlock)(NSString *key); | ||
|
|
||
| @end | ||
|
|
||
| @implementation TestKeyValueStore | ||
|
|
||
| - (NSString *_Nullable)readValueForKey:(NSString *)key { | ||
| return self.readValueBlock(key); | ||
| } | ||
|
|
||
| - (void)saveValue:(NSString *)value toKey:(NSString *)key { | ||
| self.saveValueBlock(key, value); | ||
| } | ||
|
|
||
| - (void)removeKey:(NSString *)key { | ||
| self.removeValueBlock(key); | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| @interface EnvoyEngineImplTest : XCTestCase | ||
| @end | ||
|
|
||
| @implementation EnvoyEngineImplTest | ||
|
|
||
| - (void)testReadValue { | ||
| NSString *expectedKey = @"key"; | ||
| NSString *expectedValue = @"value"; | ||
|
|
||
| TestKeyValueStore *store = [TestKeyValueStore new]; | ||
| __block NSString *actualKey = @""; | ||
| store.readValueBlock = ^NSString *(NSString *key) { | ||
| actualKey = key; | ||
| return expectedValue; | ||
| }; | ||
|
|
||
| NSData *keyData = [expectedKey dataUsingEncoding:NSUTF8StringEncoding]; | ||
| envoy_data data = ios_kv_store_read(toNativeData(keyData), CFBridgingRetain(store)); | ||
| NSData *valueData = to_ios_data(data); | ||
| NSString *actualValue = [[NSString alloc] initWithBytes:valueData.bytes | ||
| length:valueData.length | ||
| encoding:NSUTF8StringEncoding]; | ||
|
|
||
| XCTAssertEqualObjects(expectedKey, actualKey); | ||
| XCTAssertEqualObjects(expectedValue, actualValue); | ||
| } | ||
|
|
||
| - (void)testSaveValue { | ||
| NSString *expectedKey = @"key"; | ||
| NSString *expectedValue = @"value"; | ||
|
|
||
| TestKeyValueStore *store = [TestKeyValueStore new]; | ||
| __block NSString *actualKey = @""; | ||
| __block NSString *actualValue = @""; | ||
| store.saveValueBlock = ^void(NSString *key, NSString *value) { | ||
| actualKey = key; | ||
| actualValue = value; | ||
| }; | ||
|
|
||
| NSData *keyData = [expectedKey dataUsingEncoding:NSUTF8StringEncoding]; | ||
| NSData *valueData = [expectedValue dataUsingEncoding:NSUTF8StringEncoding]; | ||
| ios_kv_store_save(toNativeData(keyData), toNativeData(valueData), CFBridgingRetain(store)); | ||
|
|
||
| XCTAssertEqualObjects(expectedKey, actualKey); | ||
| XCTAssertEqualObjects(expectedValue, actualValue); | ||
| } | ||
|
|
||
| - (void)testRemoveValue { | ||
| NSString *expectedKey = @"key"; | ||
|
|
||
| TestKeyValueStore *store = [TestKeyValueStore new]; | ||
| __block NSString *actualKey = @""; | ||
| store.removeValueBlock = ^void(NSString *key) { | ||
| actualKey = key; | ||
| }; | ||
|
|
||
| NSData *keyData = [expectedKey dataUsingEncoding:NSUTF8StringEncoding]; | ||
| ios_kv_store_remove(toNativeData(keyData), CFBridgingRetain(store)); | ||
|
|
||
| XCTAssertEqualObjects(expectedKey, actualKey); | ||
| } | ||
|
|
||
| @end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this dep need to go in
envoy_objc_bridge_libso that the header is available when building it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see it's in both now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a part of
envoy_objc_bridge_libalready - but you are right, it's redundant here I think. Removing