Skip to content

Commit

Permalink
Add SequenceVariableProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdeem committed Jun 9, 2024
1 parent fa2dcf5 commit c63916d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Sources/ScreamURITemplate/VariableProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,20 @@ public protocol VariableProvider {
public typealias VariableDictionary = [String: VariableValue]

extension VariableDictionary: VariableProvider {}

public struct SequenceVariableProvider: VariableProvider, ExpressibleByArrayLiteral {
let sequence: any Sequence<VariableProvider>

public init(arrayLiteral elements: VariableProvider...) {
sequence = elements
}

public subscript(_ key: String) -> VariableValue? {
for provider in sequence {
if let value = provider[key] {
return value
}
}
return nil
}
}
16 changes: 15 additions & 1 deletion Tests/ScreamURITemplateTests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import XCTest

struct TestVariableProvider: VariableProvider {
subscript(_ key: String) -> VariableValue? {
return "_\(key)_"
switch key {
case "missing":
return nil
default:
return "_\(key)_"
}
}
}

Expand All @@ -28,6 +33,15 @@ class Tests: XCTestCase {
XCTAssertEqual(urlString, "https://api.github.com/repos/_owner_/_repo_/collaborators/_username_")
}

func testSequenceVariableProvider() throws {
let template: URITemplate = "https://api.github.com/repos/{owner}/{repo}/collaborators/{username}{missing}"
let urlString = try template.process(variables: [
["owner": "SwiftScream"],
TestVariableProvider(),
] as SequenceVariableProvider)
XCTAssertEqual(urlString, "https://api.github.com/repos/SwiftScream/_repo_/collaborators/_username_")
}

func testStringStringDictionary() throws {
let template: URITemplate = "https://api.github.com/repos/{owner}/{repo}/collaborators/{username}"
let variables = ["owner": "SwiftScream",
Expand Down

0 comments on commit c63916d

Please sign in to comment.