Skip to content

Commit 26be737

Browse files
Add string array as an option for request context params for endpoint resolution (#276)
Co-authored-by: Waqar Ahmed Khan <waahm7@gmail.com>
1 parent 72e9f47 commit 26be737

12 files changed

Lines changed: 78 additions & 10 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
lint:
2020
runs-on: ubuntu-latest
2121
steps:
22-
- uses: actions/checkout@v3
22+
- uses: actions/checkout@v4
2323
- name: GitHub Action for SwiftLint
2424
uses: norio-nomura/action-swiftlint@3.2.1
2525

@@ -125,7 +125,7 @@ jobs:
125125
runs-on: ubuntu-22.04 # latest
126126
steps:
127127
- name: Checkout Source
128-
uses: actions/checkout@v3
128+
uses: actions/checkout@v4
129129
with:
130130
submodules: true
131131
fetch-depth: 0

Source/AwsCommonRuntimeKit/crt/Utilities.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,19 @@ func withByteCursorFromStrings<Result>(
272272
}
273273
}
274274
}
275+
276+
extension Array where Element == String {
277+
func withByteCursorArray<R>(_ body: (UnsafePointer<aws_byte_cursor>, Int) -> R) -> R {
278+
let len = self.count
279+
let cStrings = self.map { strdup($0) }
280+
let cursors = cStrings.map { aws_byte_cursor_from_c_str($0) }
281+
282+
defer {
283+
cStrings.forEach { free($0) }
284+
}
285+
286+
return cursors.withUnsafeBufferPointer { cursorsPtr in
287+
return body(cursorsPtr.baseAddress!, len)
288+
}
289+
}
290+
}

Source/AwsCommonRuntimeKit/sdkutils/endpoint/EndpointsRequestContext.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ public class EndpointsRequestContext {
4848
}
4949
}
5050

51+
/// Add a string array endpoint parameter to the request context
52+
/// - Parameters:
53+
/// - name: The name of the parameter
54+
/// - value: The value of the parameter
55+
public func add(name: String, value: [String]?) throws {
56+
guard let value = value else {
57+
return
58+
}
59+
if (name.withByteCursor { nameCursor in
60+
value.withByteCursorArray { ptr, len in
61+
aws_endpoints_request_context_add_string_array(allocator.rawValue, rawValue, nameCursor, ptr, len)
62+
}
63+
}) != AWS_OP_SUCCESS {
64+
throw CommonRunTimeError.crtError(.makeFromLastError())
65+
}
66+
}
67+
5168
deinit {
5269
aws_endpoints_request_context_release(rawValue)
5370
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0.
3+
4+
import XCTest
5+
@testable import AwsCommonRuntimeKit
6+
import AwsCCommon
7+
8+
class UtilitiesTests: XCBaseTestCase {
9+
10+
func testStringArrayToByteCursorArray() {
11+
let strings1 = ["a", "b"]
12+
strings1.withByteCursorArray { cursors, len in
13+
XCTAssertEqual(len, 2)
14+
for i in 0..<len {
15+
withUnsafePointer(to: cursors[i]) { pointer in
16+
XCTAssertTrue(aws_byte_cursor_is_valid(pointer))
17+
}
18+
}
19+
}
20+
21+
let strings2: [String] = []
22+
strings2.withByteCursorArray { cursors, len in
23+
XCTAssertEqual(len, 0)
24+
}
25+
}
26+
}
27+

Test/AwsCommonRuntimeKitTests/sdkutils/EndpointsRuleEngineTests.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class EndpointsRuleEngineTests: XCBaseTestCase {
1313
"partitions": [
1414
{
1515
"id": "aws",
16-
"regionRegex": "^(us|eu|ap|sa|ca|me|af)-\\w+-\\d+$",
16+
"regionRegex": "^(us|eu|ap|sa|ca|me|af)\\-\\w+\\-\\d+$",
1717
"regions": {
1818
"af-south-1": {},
1919
"af-east-1": {},
@@ -282,4 +282,12 @@ class EndpointsRuleEngineTests: XCBaseTestCase {
282282
let _ = try! engine.resolve(context: context)
283283
}
284284
}
285+
286+
func testRequestContext() {
287+
let context = try! EndpointsRequestContext()
288+
try! context.add(name: "Region", value: "us-west-2")
289+
try! context.add(name: "Boolean", value: true)
290+
try! context.add(name: "StringArray", value: ["a", "b"])
291+
try! context.add(name: "StringArray", value: [])
292+
}
285293
}

0 commit comments

Comments
 (0)