-
Notifications
You must be signed in to change notification settings - Fork 18
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
feat: bindings for EndpointsRuleEngine #79
Merged
Merged
Changes from 9 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
1cd5291
feat: bindings for EndpointsRuleEngine
7763a05
throw ser error
5718103
add perf tests
5128aa7
fix linter
fa80602
update submodules
6bf16fc
fix linter
997e958
try to fix config read
6622d7b
try removing strict
afdbcd9
remove strict
5b12865
adapt partitions change
797577e
update submodules
2081961
update submodules
fa3b9ed
use raw string
596f345
add deinit
e83b0fa
remove AWSCommonRuntimeError
43522c1
more error fix
c96107d
use bytecursor directly
b5ebed3
deallocate
edbda57
make init internal
4ca87f8
deallocate
362df91
linter
2d695e3
add test case for prop and hdrs
18d5466
fix test case
9fd5e9b
replace JSONSerialization with custom Decoder.
a5a6191
add test cases
9846421
mixed test case
6089aad
renames
ed3118e
use old syntax
14a2a88
simplify access
9aa07ab
update subm
148cc17
fix breaking change
5df96f0
release ruleset and partitions manually
b4d7fc5
update sdkutils
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 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 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
55 changes: 55 additions & 0 deletions
55
Source/AwsCommonRuntimeKit/sdkutils/CRTAWSEndpointsRequestContext.swift
This file contains 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,55 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0. | ||
|
||
import AwsCSdkUtils | ||
|
||
/// Request context used for resolving endpoint | ||
public class CRTAWSEndpointsRequestContext { | ||
let rawValue: OpaquePointer | ||
|
||
/// Initialize a new request context | ||
/// - Parameter allocator: Allocator to use for request context creation | ||
public init(allocator: Allocator = defaultAllocator) throws { | ||
guard let rawValue = aws_endpoints_request_context_new(allocator.rawValue) else { | ||
throw CRTError.awsError(AWSCommonRuntimeError()) | ||
} | ||
|
||
self.rawValue = rawValue | ||
} | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Add a string endpoint parameter to the request context | ||
/// - Parameters: | ||
/// - name: The name of the parameter | ||
/// - value: The value of the parameter | ||
/// - allocator: The allocator to use for the parameter | ||
public func add(name: String, value: String?, allocator: Allocator = defaultAllocator) throws { | ||
guard let value = value else { | ||
return | ||
} | ||
waahm7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let namePtr: UnsafeMutablePointer<aws_byte_cursor> = fromPointer(ptr: name.awsByteCursor) | ||
let valuePtr: UnsafeMutablePointer<aws_byte_cursor> = fromPointer(ptr: value.awsByteCursor) | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let success = aws_endpoints_request_context_add_string(allocator.rawValue, | ||
rawValue, | ||
namePtr.pointee, | ||
valuePtr.pointee) | ||
if success != 0 { | ||
throw CRTError.awsError(AWSCommonRuntimeError()) | ||
} | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Add a bool endpoint parameter to the request context | ||
/// - Parameters: | ||
/// - name: The name of the parameter | ||
/// - value: The value of the parameter | ||
/// - allocator: The allocator to use for the parameter | ||
public func add(name: String, value: Bool?, allocator: Allocator = defaultAllocator) throws { | ||
guard let value = value else { | ||
return | ||
} | ||
let namePtr: UnsafeMutablePointer<aws_byte_cursor> = fromPointer(ptr: name.awsByteCursor) | ||
let success = aws_endpoints_request_context_add_boolean(allocator.rawValue, rawValue, namePtr.pointee, value) | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if success != 0 { | ||
throw CRTError.awsError(AWSCommonRuntimeError()) | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
Source/AwsCommonRuntimeKit/sdkutils/CRTAWSEndpointsResolvedEndpoint.swift
This file contains 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,88 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0. | ||
|
||
import Foundation | ||
import AwsCSdkUtils | ||
|
||
/// Resolved endpoint | ||
public class CRTAWSEndpointResolvedEndpoint { | ||
let rawValue: OpaquePointer | ||
|
||
/// Initialize a new resolved endpoint | ||
/// - Parameter rawValue: The raw value of the resolved endpoint | ||
public init(rawValue: OpaquePointer) { | ||
self.rawValue = rawValue | ||
|
||
aws_endpoints_resolved_endpoint_acquire(rawValue) | ||
} | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Get the type of the resolved endpoint | ||
/// - Returns: The type of the resolved endpoint | ||
public func getType() -> CRTAWSEndpointsResolvedEndpointType { | ||
let type = aws_endpoints_resolved_endpoint_get_type(rawValue) | ||
return CRTAWSEndpointsResolvedEndpointType(rawValue: type) | ||
} | ||
|
||
/// Get the URL of the resolved endpoint | ||
/// - Returns: The URL of the resolved endpoint | ||
public func getURL() throws -> String? { | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let urlOut = UnsafeMutablePointer<aws_byte_cursor>.allocate(capacity: 1) | ||
let success = aws_endpoints_resolved_endpoint_get_url(rawValue, urlOut) | ||
if success != 0 { | ||
throw CRTError.awsError(AWSCommonRuntimeError()) | ||
} | ||
return urlOut.pointee.toString() | ||
} | ||
|
||
/// Get the properties of the resolved endpoint | ||
/// - Returns: The properties of the resolved endpoint | ||
public func getProperties() throws -> [String: AnyHashable]? { | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let propsOut = UnsafeMutablePointer<aws_byte_cursor>.allocate(capacity: 1) | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let success = aws_endpoints_resolved_endpoint_get_properties(rawValue, propsOut) | ||
if success != 0 { | ||
throw CRTError.awsError(AWSCommonRuntimeError()) | ||
} | ||
guard let data = propsOut.pointee.toString()?.data(using: .utf8) else { | ||
return nil | ||
} | ||
return try JSONSerialization.jsonObject(with: data) as? [String: AnyHashable] | ||
} | ||
|
||
/// Get the error of the resolved endpoint | ||
/// - Parameter allocator: The allocator to use for the error | ||
public func getError() throws -> String? { | ||
let errorOut = UnsafeMutablePointer<aws_byte_cursor>.allocate(capacity: 1) | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let success = aws_endpoints_resolved_endpoint_get_error(rawValue, errorOut) | ||
if success != 0 { | ||
throw CRTError.awsError(AWSCommonRuntimeError()) | ||
} | ||
return errorOut.pointee.toString() | ||
} | ||
|
||
/// Get headers of the resolved endpoint | ||
/// - Returns: The headers of the resolved endpoint | ||
public func getHeaders() throws -> [String: [String]]? { | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let headersOut: UnsafeMutablePointer<UnsafePointer<aws_hash_table>?> | ||
= UnsafeMutablePointer<UnsafePointer<aws_hash_table>?>.allocate(capacity: 1) | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let success = aws_endpoints_resolved_endpoint_get_headers(rawValue, headersOut) | ||
if success != 0 { | ||
throw CRTError.awsError(AWSCommonRuntimeError()) | ||
} | ||
|
||
var headers: [String: [String]] = [:] | ||
var iter = aws_hash_iter_begin(headersOut.pointee) | ||
|
||
while !aws_hash_iter_done(&iter) { | ||
let key = iter.element.key.bindMemory(to: String.self, capacity: 1).pointee | ||
let value = iter.element.value.bindMemory(to: aws_array_list.self, capacity: 1).pointee.toStringArray() | ||
headers[key] = value | ||
aws_hash_iter_next(&iter) | ||
} | ||
|
||
return headers | ||
} | ||
|
||
deinit { | ||
aws_endpoints_resolved_endpoint_release(rawValue) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Source/AwsCommonRuntimeKit/sdkutils/CRTAWSEndpointsResolvedEndpointType.swift
This file contains 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,28 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0. | ||
|
||
import AwsCSdkUtils | ||
|
||
/// Resolved endpoint type | ||
public enum CRTAWSEndpointsResolvedEndpointType { | ||
/// Used for endpoints that are resolved successfully | ||
case endpoint | ||
/// Used for endpoints that resolve to an error | ||
case error | ||
} | ||
|
||
extension CRTAWSEndpointsResolvedEndpointType: RawRepresentable, CaseIterable { | ||
public init(rawValue: aws_endpoints_resolved_endpoint_type) { | ||
let value = Self.allCases.first(where: {$0.rawValue == rawValue}) | ||
self = value ?? .endpoint | ||
} | ||
|
||
public var rawValue: aws_endpoints_resolved_endpoint_type { | ||
switch self { | ||
case .endpoint: | ||
return AWS_ENDPOINTS_RESOLVED_ENDPOINT | ||
case .error: | ||
return AWS_ENDPOINTS_RESOLVED_ERROR | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Source/AwsCommonRuntimeKit/sdkutils/CRTAWSEndpointsRuleEngine.swift
This file contains 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 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0. | ||
|
||
import AwsCSdkUtils | ||
|
||
/// Rule engine for matching endpoint rules | ||
public class CRTAWSEndpointsRuleEngine { | ||
let rawValue: OpaquePointer | ||
|
||
/// Initialize a new rule engine | ||
/// - Parameters: | ||
/// - ruleSetString: The rule set string to use for the rule engine | ||
/// - allocator: The allocator to use for creating rule engine | ||
public init(ruleSetString: String, allocator: Allocator = defaultAllocator) throws { | ||
guard let ruleSet = aws_endpoints_ruleset_new_from_string(allocator.rawValue, ruleSetString.newByteCursor().rawValue), | ||
let rawValue = aws_endpoints_rule_engine_new(allocator.rawValue, ruleSet) else { | ||
throw CRTError.awsError(AWSCommonRuntimeError()) | ||
} | ||
|
||
self.rawValue = rawValue | ||
} | ||
|
||
/// Resolve an endpoint from the rule engine using the provided request context | ||
/// - Parameter context: The request context to use for endpoint resolution | ||
/// - Returns: The resolved endpoint | ||
public func resolve(context: CRTAWSEndpointsRequestContext) throws -> CRTAWSEndpointResolvedEndpoint? { | ||
let resolvedEndpoint: UnsafeMutablePointer<OpaquePointer?>? = UnsafeMutablePointer.allocate(capacity: 1) | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let success = aws_endpoints_rule_engine_resolve(rawValue, context.rawValue, resolvedEndpoint) | ||
if success != 0 { | ||
throw CRTError.awsError(AWSCommonRuntimeError()) | ||
} | ||
|
||
guard let pointee = resolvedEndpoint?.pointee else { | ||
return nil | ||
} | ||
|
||
return CRTAWSEndpointResolvedEndpoint(rawValue: pointee) | ||
} | ||
|
||
deinit { | ||
aws_endpoints_rule_engine_release(rawValue) | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
Test/AwsCommonRuntimeKitTests/sdkutils/CRTAWSEndpointsRuleEngineTests.swift
This file contains 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,106 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0. | ||
|
||
import XCTest | ||
import Foundation | ||
@testable import AwsCommonRuntimeKit | ||
|
||
class CRTAWSEndpointsRuleEngineTests: CrtXCBaseTestCase { | ||
ganeshnj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let ruleSetString = | ||
""" | ||
{ | ||
"version": "1.0", | ||
"serviceId": "example", | ||
"parameters": { | ||
"Region": { | ||
"type": "string", | ||
"builtIn": "AWS::Region", | ||
"documentation": "The region to dispatch the request to" | ||
} | ||
}, | ||
"rules": [ | ||
{ | ||
"documentation": "rules for when region isSet", | ||
"type": "tree", | ||
"conditions": [ | ||
{ | ||
"fn": "isSet", | ||
"argv": [ | ||
{ | ||
"ref": "Region" | ||
} | ||
] | ||
} | ||
], | ||
"rules": [ | ||
{ | ||
"type": "endpoint", | ||
"conditions": [ | ||
{ | ||
"fn": "aws.partition", | ||
"argv": [ | ||
{ | ||
"ref": "Region" | ||
} | ||
], | ||
"assign": "partitionResult" | ||
} | ||
], | ||
"endpoint": { | ||
"url": "https://example.{Region}.{partitionResult#dnsSuffix}" | ||
} | ||
}, | ||
{ | ||
"type": "error", | ||
"documentation": "invalid region value", | ||
"conditions": [], | ||
"error": "unable to determine endpoint for region: {Region}" | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "endpoint", | ||
"documentation": "the single service global endpoint", | ||
"conditions": [], | ||
"endpoint": { | ||
"url": "https://example.amazonaws.com" | ||
} | ||
} | ||
] | ||
} | ||
""" | ||
|
||
func testResolve() throws { | ||
let engine = try CRTAWSEndpointsRuleEngine(ruleSetString: ruleSetString) | ||
let context = try CRTAWSEndpointsRequestContext() | ||
try context.add(name: "Region", value: "us-west-2") | ||
let endpoint = try engine.resolve(context: context) | ||
let url = try endpoint?.getURL() | ||
XCTAssertNotNil(url) | ||
XCTAssertEqual("https://example.us-west-2.amazonaws.com", url!) | ||
} | ||
|
||
func testRuleSetParsingPerformance() { | ||
measure { | ||
_ = try! CRTAWSEndpointsRuleEngine(ruleSetString: ruleSetString) | ||
} | ||
} | ||
|
||
func testRuleSetEvaluationPerformance() { | ||
let engine = try! CRTAWSEndpointsRuleEngine(ruleSetString: ruleSetString) | ||
let context = try! CRTAWSEndpointsRequestContext() | ||
try! context.add(name: "Region", value: "us-west-2") | ||
measure { | ||
let _ = try! engine.resolve(context: context) | ||
} | ||
} | ||
|
||
func testResolvePerformance() { | ||
measure { | ||
let engine = try! CRTAWSEndpointsRuleEngine(ruleSetString: ruleSetString) | ||
let context = try! CRTAWSEndpointsRequestContext() | ||
try! context.add(name: "Region", value: "us-west-2") | ||
let _ = try! engine.resolve(context: context) | ||
} | ||
} | ||
} |
Submodule aws-c-common
updated
8 files
+10 −0 | include/aws/common/json.h | |
+1 −0 | include/aws/common/logging.h | |
+162 −0 | include/aws/common/uri.h | |
+6 −0 | source/json.c | |
+599 −0 | source/uri.c | |
+23 −0 | tests/CMakeLists.txt | |
+7 −0 | tests/json_test.c | |
+857 −0 | tests/uri_test.c |
Submodule aws-c-io
updated
6 files
+11 −0 | .github/workflows/ci.yml | |
+1 −151 | include/aws/io/uri.h | |
+2 −0 | source/s2n/s2n_tls_channel_handler.c | |
+0 −583 | source/uri.c | |
+0 −21 | tests/CMakeLists.txt | |
+0 −767 | tests/uri_test.c |
Submodule aws-c-sdkutils
updated
47 files
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.
strict is ignoring the
.swiftlint
config file and redundant to the line 20 after removing strict.