一个基于 Swift 6 实现的强大且高效的 RFC6570 URI 模板。提供完整的 Level 4 支持。
在您的 Package.swift 依赖项中添加 .package(url: "https://github.com/SwiftScream/URITemplate.git", from: "5.0.0")
import ScreamURITemplate
let template = try URITemplate(string:"https://api.github.com/repos/{owner}/{repository}/traffic/views")
let variables = ["owner":"SwiftScream", "repository":"URITemplate"]
let urlString = try template.process(variables)
// https://api.github.com/repos/SwiftScream/URITemplate/traffic/views
模板初始化和处理都可能失败;抛出一个 URITemplate.Error
错误。错误用例包含关联的值,指定错误的字符串原因以及错误发生的模板字符串中的索引。
do {
_ = try URITemplate(string: "https://api.github.com/repos/{}/{repository}")
} catch {
// error.reason = "Empty Variable Name"
// error.position = 29th character
}
let template = try URITemplate(string:"https://api.github.com/repos/{owner}/{repository}/traffic/views")
let variableNames = template.variableNames
// ["owner", "repository"]
URITemplate
实现了 Codable
协议,可以方便地序列化为或从 JSON 对象反序列化。
struct HALObject : Codable {
let _links : [String:URITemplate]
}
该库已经针对 标准测试套件 以及一些针对此实现的特定行为的附加测试进行了测试。目的是尽可能保持较高的测试覆盖率。