Skip to content

Commit

Permalink
Merge pull request #1 from nixzhu/macro
Browse files Browse the repository at this point in the history
Macro
  • Loading branch information
nixzhu authored Sep 14, 2023
2 parents 474598a + d2cb497 commit 5cacf1f
Show file tree
Hide file tree
Showing 8 changed files with 715 additions and 19 deletions.
13 changes: 11 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@
"version" : "0.1.6"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "74203046135342e4a4a627476dd6caf8b28fe11b",
"version" : "509.0.0"
}
},
{
"identity" : "yyjson",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ibireme/yyjson.git",
"state" : {
"revision" : "09ccaa449e01879e23f68d8be165e78e65334443",
"version" : "0.7.0"
"revision" : "e01ae9d89eaf858894d366169329097171a28d67",
"version" : "0.8.0"
}
}
],
Expand Down
30 changes: 27 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
// swift-tools-version: 5.8
// swift-tools-version: 5.9

import PackageDescription
import CompilerPluginSupport

let package = Package(
name: "Ananda",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6),
.macCatalyst(.v13),
.visionOS(.v1),
],
products: [
.library(
name: "Ananda",
Expand All @@ -13,17 +22,29 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/ibireme/yyjson.git",
from: "0.7.0"
from: "0.8.0"
),
.package(
url: "https://github.com/michaeleisel/JJLISO8601DateFormatter.git",
from: "0.1.6"
),
.package(
url: "https://github.com/apple/swift-syntax.git",
from: "509.0.0"
),
],
targets: [
.macro(
name: "AnandaMacros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
]
),
.target(
name: "Ananda",
dependencies: [
"AnandaMacros",
.product(
name: "yyjson",
package: "yyjson"
Expand All @@ -36,7 +57,10 @@ let package = Package(
),
.testTarget(
name: "AnandaTests",
dependencies: ["Ananda"]
dependencies: [
"Ananda",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
]
),
]
)
23 changes: 9 additions & 14 deletions Sources/Ananda/AnandaJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,21 @@ extension AnandaJSON {
return true
}

if isDictionary {
if yyjson_is_obj(pointer) {
return yyjson_obj_size(pointer) == 0
}

if isArray {
if yyjson_is_arr(pointer) {
return yyjson_arr_size(pointer) == 0
}

return false
}

/// `nil` if self is empty, otherwise `self`.
public var emptyAsNil: AnandaJSON? {
isEmpty ? nil : self
}
}

extension AnandaJSON {
Expand Down Expand Up @@ -176,14 +181,9 @@ extension AnandaJSON {
}

extension AnandaJSON {
/// Whether the value is object.
public var isDictionary: Bool {
yyjson_is_obj(pointer)
}

/// Dictionary if present, or `nil`.
public var dictionary: [String: AnandaJSON]? {
guard isDictionary else {
guard yyjson_is_obj(pointer) && yyjson_obj_size(pointer) > 0 else {
return nil
}

Expand Down Expand Up @@ -223,14 +223,9 @@ extension AnandaJSON {
}

extension AnandaJSON {
/// Whether the value is array.
public var isArray: Bool {
yyjson_is_arr(pointer)
}

/// Array value if present, or `nil`.
public var array: [AnandaJSON]? {
guard isArray else {
guard yyjson_is_arr(pointer) && yyjson_arr_size(pointer) > 0 else {
return nil
}

Expand Down
11 changes: 11 additions & 0 deletions Sources/Ananda/Macros.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@attached(member, names: named(init(json:)))
public macro AnandaInit() = #externalMacro(
module: "AnandaMacros",
type: "AnandaInitMacro"
)

@attached(peer)
public macro AnandaKey(_: String) = #externalMacro(
module: "AnandaMacros",
type: "AnandaKeyMacro"
)
Loading

0 comments on commit 5cacf1f

Please sign in to comment.