Skip to content

Commit

Permalink
feat: simple and attributegroup parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
elihwyma committed Nov 1, 2024
1 parent 7dd78b8 commit 1db6d28
Show file tree
Hide file tree
Showing 11 changed files with 465 additions and 19 deletions.
10 changes: 2 additions & 8 deletions Sources/PugiSwiftDemo/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
import Foundation
import PugiSwift

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

@Restriction struct ExampleIntType: NumericRestrictions {

static let maxExclusive = 5
Expand Down Expand Up @@ -48,7 +43,7 @@ import PugiSwift
let color: Colours
}

@Node struct OptionalRecord {
@Node struct Records {
@Attribute let value: String?
@Element(childrenCodingKey: "record") let records: [Record]?
}
Expand All @@ -58,7 +53,6 @@ import PugiSwift
case red = "red"
case green = "green"
case blue = "blue"

}

let str =
Expand All @@ -83,7 +77,7 @@ let str =
"""

do {
let records = try OptionalRecord(from: str)
let records = try Records(from: str)
print(records)
} catch {
print("Error: \(error.localizedDescription)")
Expand Down
20 changes: 10 additions & 10 deletions Sources/PugiSwiftMacros/NodeMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public struct NodeMacro: ExtensionMacro {
guard let wrappedArry = ArrayTypeSyntax(optionalArrayType._baseSyntax.wrappedType) else {
throw MacroError("\(propertyName) must be an array")
}
let elementType = wrappedArry.elementType.description
let elementType = wrappedArry.element.description
let code = CodeBlockItemSyntax(
"""
var _\(raw: nodeHelperName) = [\(raw: elementType)]()
Expand Down Expand Up @@ -196,22 +196,22 @@ public struct NodeMacro: ExtensionMacro {
guard let rawType = enumDecl.inheritedTypes.first?.normalizedDescription else {
throw MacroError("Enum must have a raw type.")
}

var functionDecl = Self.createFunction(with: enumDecl.accessLevel,
name: "node",
type: "PugiSwift.XMLNode")
let syntax = CodeBlockItemSyntax(
var functionDeclAttr = Self.createFunction(with: enumDecl.accessLevel,
name: "attribute",
type: "(any AttributeProtocol)?")
let syntaxAttribute = CodeBlockItemSyntax(
"""
let rawValue = try \(raw: rawType).init(from: node)
let rawValue = try \(raw: rawType).init(from: attribute)
guard let enumType = \(raw: enumDecl.identifier).init(rawValue: rawValue) else {
throw .invalidCase
}
self = enumType
"""
)
functionDecl.body = CodeBlockSyntax(statements: CodeBlockItemListSyntax([syntax]))
let memberBlockItemSyntax = MemberBlockItemSyntax(decl: functionDecl)
extensionDecl.memberBlock.members.append(memberBlockItemSyntax)
functionDeclAttr.body = CodeBlockSyntax(statements: CodeBlockItemListSyntax([syntaxAttribute]))
let memberBlockItemSyntaxAttr = MemberBlockItemSyntax(decl: functionDeclAttr)
extensionDecl.memberBlock.members.append(memberBlockItemSyntaxAttr)

return [extensionDecl]
}

Expand Down
14 changes: 14 additions & 0 deletions Sources/swiftxsd/Types/Annotation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Annotation.swift
// PugiSwift
//
// Created by Amy on 01/11/2024.
//
import Foundation
import PugiSwift

@Node public struct Annotation {

@Element(codingKey: "xs:documentation") public let documentation: String?

}
22 changes: 22 additions & 0 deletions Sources/swiftxsd/Types/Attribute.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Attribute.swift
// PugiSwift
//
// Created by Amy on 01/11/2024.
//

import Foundation
import PugiSwift

@Node public struct Attribute {

@Attribute public let name: String

@Attribute public let type: String

@Attribute public let use: String?

@Element(codingKey: "xs:annotation") public let annotation: Annotation?

}

20 changes: 20 additions & 0 deletions Sources/swiftxsd/Types/AttributeGroup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// AttributeGroup.swift
// PugiSwift
//
// Created by Amy on 01/11/2024.
//

import Foundation
import PugiSwift

@Node public struct AttributeGroup {

@Attribute public let name: String

@Element(childrenCodingKey: "xs:attribute") public let attributes: [Attribute]?

@Element(codingKey: "xs:annotation") public let annotation: Annotation?

}

62 changes: 62 additions & 0 deletions Sources/swiftxsd/Types/BaseType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// BaseTypes.swift
// PugiSwift
//
// Created by Amy on 01/11/2024.
//

import Foundation
import PugiSwift

@Node public enum BaseType: String {

case string = "xs:string"

case integer = "xs:integer"

case unsignedInt = "xs:unsignedInt"

case unsignedShort = "xs:unsignedShort"

case short = "xs:short"

case decimal = "xs:decimal"

case boolean = "xs:boolean"

case date = "xs:date"

case time = "xs:time"

case dateTime = "xs:dateTime"

case anyURI = "xs:anyURI"

public var rawType: XMLDecodable.Type {
switch self {
case .string:
return String.self
case .integer:
return Int.self
case .unsignedInt:
return UInt32.self
case .unsignedShort:
return UInt16.self
case .short:
return Int16.self
case .decimal:
return Double.self
case .boolean:
return Bool.self
case .date:
return String.self
case .time:
return String.self
case .dateTime:
return String.self
case .anyURI:
return URL.self
}
}

}
15 changes: 15 additions & 0 deletions Sources/swiftxsd/Types/Enumeration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Enumeration.swift
// PugiSwift
//
// Created by Amy on 01/11/2024.
//

import Foundation
import PugiSwift

@Node public struct BasicType {

@Attribute public let value: String

}
35 changes: 35 additions & 0 deletions Sources/swiftxsd/Types/Restriction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Restriction.swift
// PugiSwift
//
// Created by Amy on 01/11/2024.
//

import Foundation
import PugiSwift

@Node public struct Restriction {

@Attribute public var base: BaseType

@Element(childrenCodingKey: "xs:enumeration") public let enumeration: [BasicType]?

@Element(codingKey: "xs:length") public let length: BasicType?

@Element(codingKey: "xs:maxExclusive") public let maxExclusive: BasicType?

@Element(codingKey: "xs:maxInclusive") public let maxInclusive: BasicType?

@Element(codingKey: "xs:maxLength") public let maxLength: BasicType?

@Element(codingKey: "xs:minExclusive") public let minExclusive: BasicType?

@Element(codingKey: "xs:minInclusive") public let minInclusive: BasicType?

@Element(codingKey: "xs:minLength") public let minLength: BasicType?

@Element(codingKey: "xs:pattern") public let pattern: BasicType?

@Element(codingKey: "xs:totalDigits") public let totalDigits: BasicType?

}
18 changes: 18 additions & 0 deletions Sources/swiftxsd/Types/Schema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Schema.swift
// PugiSwift
//
// Created by Amy on 01/11/2024.
//

import Foundation
import PugiSwift

@Node public struct Schema {

@Element(childrenCodingKey: "xs:simpleType") public let simpleTypes: [SimpleType]

@Element(childrenCodingKey: "xs:attributeGroup") public let attributeGroups: [AttributeGroup]


}
19 changes: 19 additions & 0 deletions Sources/swiftxsd/Types/SimpleType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// SimpleType.swift
// PugiSwift
//
// Created by Amy on 01/11/2024.
//

import Foundation
import PugiSwift

@Node public struct SimpleType {

@Attribute public let name: String

@Element(codingKey: "xs:annotation") public let annotation: Annotation?

@Element(codingKey: "xs:restriction") public let restriction: Restriction

}
Loading

0 comments on commit 1db6d28

Please sign in to comment.