Skip to content

Commit

Permalink
Support processing by directly providing TypedVariableValue
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdeem committed Jun 12, 2024
1 parent 28f4c81 commit 8e17728
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
10 changes: 5 additions & 5 deletions Sources/ScreamURITemplate/Internal/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Foundation
typealias ComponentBase = Sendable

protocol Component: ComponentBase {
func expand(variables: VariableProvider) throws -> String
func expand(variables: TypedVariableProvider) throws -> String
var variableNames: [String] { get }
}

Expand All @@ -33,7 +33,7 @@ struct LiteralComponent: Component {
literal = string
}

func expand(variables _: VariableProvider) throws -> String {
func expand(variables _: TypedVariableProvider) throws -> String {
let expansion = String(literal)
guard let encodedExpansion = expansion.addingPercentEncoding(withAllowedCharacters: reservedAndUnreservedCharacterSet) else {
throw URITemplate.Error.expansionFailure(position: literal.startIndex, reason: "Percent Encoding Failed")
Expand All @@ -48,7 +48,7 @@ struct LiteralPercentEncodedTripletComponent: Component {
literal = string
}

func expand(variables _: VariableProvider) throws -> String {
func expand(variables _: TypedVariableProvider) throws -> String {
return String(literal)
}
}
Expand All @@ -64,10 +64,10 @@ struct ExpressionComponent: Component {
self.templatePosition = templatePosition
}

func expand(variables: VariableProvider) throws -> String {
func expand(variables: TypedVariableProvider) throws -> String {
let configuration = expressionOperator.expansionConfiguration()
let expansions = try variableList.compactMap { variableSpec -> String? in
guard let value = variables[String(variableSpec.name)]?.asTypedVariableValue() else {
guard let value = variables[String(variableSpec.name)] else {
return nil
}
do {
Expand Down
14 changes: 13 additions & 1 deletion Sources/ScreamURITemplate/URITemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,26 @@ public struct URITemplate {
self.components = components
}

public func process(variables: VariableProvider) throws -> String {
public func process(variables: TypedVariableProvider) throws -> String {
var result = ""
for component in components {
result += try component.expand(variables: variables)
}
return result
}

public func process(variables: VariableProvider) throws -> String {
struct TypedVariableProviderWrapper: TypedVariableProvider {
let variables: VariableProvider

subscript(_ key: String) -> TypedVariableValue? {
return variables[key]?.asTypedVariableValue()
}
}

return try process(variables: TypedVariableProviderWrapper(variables: variables))
}

public func process(variables: [String: String]) throws -> String {
return try process(variables: variables as VariableDictionary)
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/ScreamURITemplate/VariableProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ public protocol VariableProvider {
subscript(_: String) -> VariableValue? { get }
}

public protocol TypedVariableProvider {
subscript(_: String) -> TypedVariableValue? { get }
}

public typealias VariableDictionary = [String: VariableValue]

extension VariableDictionary: VariableProvider {}

public typealias TypedVariableDictionary = [String: TypedVariableValue]

extension TypedVariableDictionary: TypedVariableProvider {}

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

Expand Down

0 comments on commit 8e17728

Please sign in to comment.