Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/SWONMacros/Decoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,15 @@ struct SWONDecodeMacro: MemberMacro {
""")
}
}
let accessLevel = declaration.modifiers.accessLevel
return [
DeclSyntax(stringLiteral: """
init(fromSWON root: swon_t) throws {
\(accessLevel) init(fromSWON root: swon_t) throws {
\(assignments.joined(separator: "\n"))
}
"""),
DeclSyntax(stringLiteral: """
init(fromJSON json: String) throws {
\(accessLevel) init(fromJSON json: String) throws {
var root = swon_t()
defer { swon_free(&root) }
guard swon_parse(&root, json) == SWONResultValid else {
Expand Down
5 changes: 3 additions & 2 deletions Sources/SWONMacros/Encoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ struct SWONEncodeMacro: MemberMacro {
// message: SWONMessage(message: "Assignments: \(assignments)")
// )
// )
let accessLevel = declaration.modifiers.accessLevel
return [
DeclSyntax(stringLiteral: """
func toSWON() throws -> swon_t {
\(accessLevel) func toSWON() throws -> swon_t {
var root = swon_t()
guard swon_create_object(&root) else {
throw SWONError.invalid("Unable to create root")
Expand All @@ -223,7 +224,7 @@ struct SWONEncodeMacro: MemberMacro {
}
"""),
DeclSyntax(stringLiteral: """
func toJSON() throws -> String {
\(accessLevel) func toJSON() throws -> String {
var item = try toSWON()
defer { swon_free(&item) }
guard let cjson = swon_encode(item) else {
Expand Down
20 changes: 20 additions & 0 deletions Sources/SWONMacros/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ extension EnumDeclSyntax {
}
}

extension DeclModifierListSyntax {
var accessLevel: String {
for modifier in self {
switch modifier.name.tokenKind {
case .keyword(.public):
return "public"
case .keyword(.internal):
return "internal"
case .keyword(.fileprivate):
return "fileprivate"
case .keyword(.private):
return "private"
default:
continue
}
}
return ""
}
}

extension PatternBindingSyntax {
var isComputed: Bool {
accessorBlock != nil
Expand Down