Skip to content

Commit

Permalink
Merge pull request #3 from hactar/public-expression
Browse files Browse the repository at this point in the history
Allow setting expression directly
  • Loading branch information
ianthetechie authored May 3, 2024
2 parents 9f15cbb + 121c55b commit d52adbc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Sources/MapLibreSwiftMacrosImpl/StyleExpressionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ private func generateStyleProperty(for attributes: AttributeSyntax, valueType: T
isRawRepresentable: isRawRepresentable
)

let nsExpressionFuncDecl = try FunctionDeclSyntax("public func \(identifier)(expression: NSExpression) -> Self") {
"var copy = self"
"copy.\(identifier) = expression"
"return copy"
}

let getPropFuncDecl =
try FunctionDeclSyntax("public func \(identifier)(featurePropertyNamed keyPath: String) -> Self") {
"var copy = self"
Expand All @@ -41,7 +47,8 @@ private func generateStyleProperty(for attributes: AttributeSyntax, valueType: T
}

guard let constFuncDeclSyntax = DeclSyntax(constantFuncDecl),
let getPropFuncDeclSyntax = DeclSyntax(getPropFuncDecl)
let getPropFuncDeclSyntax = DeclSyntax(getPropFuncDecl),
let nsExpressionFuncDeclSyntax = DeclSyntax(nsExpressionFuncDecl)
else {
fatalError("SwiftSyntax bug or implementation error: unable to construct DeclSyntax")
}
Expand All @@ -65,7 +72,7 @@ private func generateStyleProperty(for attributes: AttributeSyntax, valueType: T
extra.append(interpolationFuncDeclSyntax)
}

return [varDeclSyntax, constFuncDeclSyntax, getPropFuncDeclSyntax] + extra
return [varDeclSyntax, constFuncDeclSyntax, nsExpressionFuncDeclSyntax, getPropFuncDeclSyntax] + extra
}

private func generateFunctionDeclSyntax(identifier: TokenSyntax, valueType: TypeSyntax,
Expand Down
24 changes: 24 additions & 0 deletions Tests/MapLibreSwiftMacrosTests/MapLibreSwiftMacrosTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ final class ExpressionTests: XCTestCase {
return copy
}
public func backgroundColor(expression: NSExpression) -> Self {
var copy = self
copy.backgroundColor = expression
return copy
}
public func backgroundColor(featurePropertyNamed keyPath: String) -> Self {
var copy = self
copy.backgroundColor = NSExpression(forKeyPath: keyPath)
Expand Down Expand Up @@ -69,6 +75,12 @@ final class ExpressionTests: XCTestCase {
return copy
}
public func backgroundColor(expression: NSExpression) -> Self {
var copy = self
copy.backgroundColor = expression
return copy
}
public func backgroundColor(featurePropertyNamed keyPath: String) -> Self {
var copy = self
copy.backgroundColor = NSExpression(forKeyPath: keyPath)
Expand Down Expand Up @@ -102,6 +114,12 @@ final class ExpressionTests: XCTestCase {
return copy
}
public func backgroundColor(expression: NSExpression) -> Self {
var copy = self
copy.backgroundColor = expression
return copy
}
public func backgroundColor(featurePropertyNamed keyPath: String) -> Self {
var copy = self
copy.backgroundColor = NSExpression(forKeyPath: keyPath)
Expand Down Expand Up @@ -141,6 +159,12 @@ final class ExpressionTests: XCTestCase {
return copy
}
public func backgroundColor(expression: NSExpression) -> Self {
var copy = self
copy.backgroundColor = expression
return copy
}
public func backgroundColor(featurePropertyNamed keyPath: String) -> Self {
var copy = self
copy.backgroundColor = NSExpression(forKeyPath: keyPath)
Expand Down

0 comments on commit d52adbc

Please sign in to comment.