Skip to content

Commit

Permalink
fix: optional type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
elihwyma committed Nov 1, 2024
1 parent 246017e commit 835d175
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
21 changes: 19 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ let package = Package(
.library(
name: "PugiSwift",
targets: ["PugiSwift"]),
.executable(name: "PugiSwiftDemo",
targets: ["PugiSwiftDemo"])
.executable(
name: "PugiSwiftDemo",
targets: ["PugiSwiftDemo"]),
.executable(
name: "swiftxsd",
targets: ["swiftxsd"])
],
dependencies: [
.package(url: "https://github.com/swiftlang/swift-syntax.git", from: "600.0.0-latest"),
Expand All @@ -42,6 +46,19 @@ let package = Package(
])
]
),
.executableTarget(
name: "swiftxsd",
dependencies: [
"PugiSwift"
],
swiftSettings: [
.interoperabilityMode(.Cxx),
.swiftLanguageMode(.v6),
.unsafeFlags([
"-Xcc", "-std=c++20"
])
]
),
.target(
name: "PugiSwift",
dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion Sources/PugiSwift/Restrictions/NumericRestrictions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public protocol NumericRestrictions: RestrictionsProtocol {
public protocol NumericRestrictions: RestrictionsProtocol, RawRepresentable {

associatedtype T: Numeric

Expand Down
13 changes: 11 additions & 2 deletions Sources/PugiSwiftDemo/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ import PugiSwift

@Node struct Record {
let name: String
let list: Int
let list: Int?
let color: Colours
}

@Node struct OptionalRecord {
@Attribute let value: String?
@Element(childrenCodingKey: "record") let records: [Record?]
}

@Node enum Colours: String {

case red = "red"
Expand All @@ -70,11 +75,15 @@ let str =
<list>423</list>
<color>green</color>
</record>
<record>
<name>John Appleseed</name>
<color>green</color>
</record>
</records>
"""

do {
let records = try Records(from: str)
let records = try OptionalRecord(from: str)
print(records)
} catch {
print("Error: \(error.localizedDescription)")
Expand Down
11 changes: 7 additions & 4 deletions Sources/PugiSwiftMacros/NodeMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,16 @@ public struct NodeMacro: ExtensionMacro {
return [code]
} else {
if let childrenCodingKey {
guard case let .array(arrayType) = type else {
guard case let .optional(optionalArrayType) = type else {
throw MacroError("\(propertyName) optional error??")
}
guard let wrappedArry = ArrayTypeSyntax(optionalArrayType._baseSyntax.wrappedType) else {
throw MacroError("\(propertyName) must be an array")
}
let elementType = arrayType._baseSyntax.element.description
let elementType = wrappedArry.elementType.description
let code = CodeBlockItemSyntax(
"""
var _\(raw: nodeHelperName) = \(raw: type.description)()
var _\(raw: nodeHelperName) = [\(raw: elementType)]()
for child in node.iterateNodes() {
if child.name != "\(raw: childrenCodingKey)" {
continue
Expand All @@ -127,7 +130,7 @@ public struct NodeMacro: ExtensionMacro {
} else {
let code = CodeBlockItemSyntax(
"""
if let \(raw: nodeHelperName) = node.child(name: \(raw: codingKey) {
if let \(raw: nodeHelperName) = node.child(name: "\(raw: codingKey)") {
self.\(raw: propertyName) = try? .init(from: \(raw: nodeHelperName))
} else {
self.\(raw: propertyName) = nil
Expand Down
1 change: 1 addition & 0 deletions Sources/PugiSwiftMacros/RestrictionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public struct RestrictionMacro: ExtensionMacro {
if Self.contains(variable: "pattern", structDecl) {
genericList.append(
"""
// On supported platforms this check is compiled out
if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, macCatalyst 16.0, visionOS 1.0, *) {
if (try? Self.pattern.wholeMatch(in: rawValue)) == nil {
throw .restrictionError(error: StringRestrictionsError.pattern)
Expand Down
8 changes: 8 additions & 0 deletions Sources/swiftxsd/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// main.swift
// PugiSwift
//
// Created by Amy on 01/11/2024.
//

print("Hello World")

0 comments on commit 835d175

Please sign in to comment.