Skip to content

Commit

Permalink
Add TypedURITemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdeem committed Jan 1, 2025
1 parent c5876f2 commit 60605b8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
37 changes: 37 additions & 0 deletions Sources/ScreamURITemplate/TypedURITemplate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2018-2024 Alex Deem
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

/// A wrapper around a ``URITemplate`` that limits the type of the variables provided when processing
/// This can be used to provide a strongly-typed interface for processing a template
public struct TypedURITemplate<Variables: VariableProvider> {
private let template: URITemplate

/// Initializes a ``TypedURITemplate`` from a ``URITemplate``
/// - Parameter template: the URI Template
public init(_ template: URITemplate) {
self.template = template
}

/// Process a URI Template specifying variables with an instance of the templated type `Variables`
/// - Parameter variables: A ``Variables`` object that can provide values for the template variables
///
/// - Returns: The result of processing the template
///
/// - Throws: `URITemplate.Error` with `type = .expansionFailure` if an error occurs processing the template
public func process(variables: Variables) throws(URITemplate.Error) -> String {
try template.process(variables: variables)
}
}
4 changes: 4 additions & 0 deletions Sources/ScreamURITemplateExample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ struct GitHubRepoCollaborator {

let expansion = try macroExpansion.process(variables: GitHubRepoCollaborator(owner: "SwiftScream", repo: "URITemplate", username: "alexdeem"))
print(expansion)

let typedTemplate = TypedURITemplate<GitHubRepoCollaborator>(macroExpansion)
let result = try typedTemplate.process(variables: .init(owner: "SwiftScream", repo: "SwiftScream", username: "alexdeem"))
print(result)
2 changes: 1 addition & 1 deletion Tests/ScreamURITemplateTests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import ScreamURITemplate
import XCTest

struct TestVariableProvider: VariableProvider {
private struct TestVariableProvider: VariableProvider {
subscript(_ key: String) -> VariableValue? {
switch key {
case "missing":
Expand Down
32 changes: 32 additions & 0 deletions Tests/ScreamURITemplateTests/TypedURITemplateTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2018-2024 Alex Deem
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest

import ScreamURITemplate

private struct TestVariableProvider: VariableProvider {
subscript(_ key: String) -> VariableValue? {
return "_\(key)_"
}
}

class TypedURITemplateTests: XCTestCase {
func testExpansion() throws {
let template: URITemplate = "https://api.github.com/repos/{owner}/{repo}/collaborators/{username}"
let typedTemplate = TypedURITemplate<TestVariableProvider>(template)
let urlString = try typedTemplate.process(variables: .init())
XCTAssertEqual(urlString, "https://api.github.com/repos/_owner_/_repo_/collaborators/_username_")
}
}

0 comments on commit 60605b8

Please sign in to comment.