-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add URITemplate freestanding expression macro
- Loading branch information
Showing
7 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version: 1 | ||
builder: | ||
configs: | ||
- documentation_targets: [ScreamURITemplate] | ||
- documentation_targets: [ScreamURITemplate, ScreamURITemplateMacros] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
Sources/ScreamURITemplateCompilerPlugin/URITemplateCompilerPlugin.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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 SwiftCompilerPlugin | ||
import SwiftSyntaxMacros | ||
|
||
@main | ||
struct URITemplateCompilerPlugin: CompilerPlugin { | ||
var providingMacros: [Macro.Type] = [URITemplateMacro.self] | ||
} |
49 changes: 49 additions & 0 deletions
49
Sources/ScreamURITemplateCompilerPlugin/URITemplateMacro.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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 SwiftCompilerPlugin | ||
import SwiftDiagnostics | ||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
|
||
import ScreamURITemplate | ||
|
||
public struct URITemplateMacro: ExpressionMacro { | ||
public static func expansion( | ||
of node: some FreestandingMacroExpansionSyntax, | ||
in _: some MacroExpansionContext) throws -> ExprSyntax { | ||
guard let argument = node.arguments.first?.expression, | ||
let segments = argument.as(StringLiteralExprSyntax.self)?.segments, | ||
segments.count == 1, | ||
case let .stringSegment(literalSegment)? = segments.first | ||
else { | ||
throw DiagnosticsError(diagnostics: [ | ||
Diagnostic(node: node, | ||
message: MacroExpansionErrorMessage("#URITemplate requires a static string literal")), | ||
]) | ||
} | ||
|
||
let uriTemplateString = literalSegment.content.text | ||
do { | ||
_ = try URITemplate(string: uriTemplateString) | ||
} catch { | ||
throw DiagnosticsError(diagnostics: [ | ||
Diagnostic(node: literalSegment, | ||
message: MacroExpansionErrorMessage("Invalid URI template: \(error.reason) at \"\(uriTemplateString.suffix(from: error.position).prefix(50))\"")), | ||
]) | ||
} | ||
|
||
return "try! URITemplate(string: \(argument))" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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 ScreamURITemplate | ||
|
||
/// Macro providing compile-time validation of a URITemplate represented by a string literal | ||
/// Example: | ||
/// ```swift | ||
/// let template = #URITemplate("https://api.github.com/repos/{owner}") | ||
/// ``` | ||
/// - Parameters: | ||
/// - : A string literal representing the URI Template | ||
/// - Returns: A `URITemplate` constructed from the string literal | ||
@freestanding(expression) | ||
public macro URITemplate(_ stringLiteral: StaticString) -> URITemplate = #externalMacro(module: "ScreamURITemplateCompilerPlugin", type: "URITemplateMacro") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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 ScreamURITemplateCompilerPlugin | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
|
||
import XCTest | ||
|
||
class MacroTests: XCTestCase { | ||
let testMacros: [String: Macro.Type] = [ | ||
"URITemplate": URITemplateMacro.self | ||
] | ||
|
||
func testValidURITemplateMacro() throws { | ||
assertMacroExpansion( | ||
#""" | ||
#URITemplate("https://api.github.com/repos/{owner}") | ||
"""#, | ||
expandedSource: | ||
#""" | ||
try! URITemplate(string: "https://api.github.com/repos/{owner}") | ||
"""#, | ||
diagnostics: [], | ||
macros: testMacros) | ||
} | ||
|
||
func testInvalidURITemplateMacro() throws { | ||
assertMacroExpansion( | ||
#""" | ||
#URITemplate("https://api.github.com/repos/{}/{repo}") | ||
"""#, | ||
expandedSource: | ||
#""" | ||
#URITemplate("https://api.github.com/repos/{}/{repo}") | ||
"""#, | ||
diagnostics: [ | ||
DiagnosticSpec(message: "Invalid URI template: Empty Variable Name at \"}/{repo}\"", line: 1, column: 15) | ||
], | ||
macros: testMacros) | ||
} | ||
|
||
func testMisusedURITemplateMacro() throws { | ||
assertMacroExpansion( | ||
#""" | ||
let s: StaticString = "https://api.github.com/repos/{owner}" | ||
#URITemplate(s) | ||
"""#, | ||
expandedSource: | ||
#""" | ||
let s: StaticString = "https://api.github.com/repos/{owner}" | ||
#URITemplate(s) | ||
"""#, | ||
diagnostics: [ | ||
DiagnosticSpec(message: "#URITemplate requires a static string literal", line: 2, column: 1) | ||
], | ||
macros: testMacros) | ||
} | ||
|
||
} |