Skip to content

Commit 11fb238

Browse files
committed
Merge AddDocc into main
2 parents f1d71f8 + e38f630 commit 11fb238

File tree

9 files changed

+518
-6
lines changed

9 files changed

+518
-6
lines changed

Package.resolved

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
{
22
"pins" : [
3+
{
4+
"identity" : "swift-docc-plugin",
5+
"kind" : "remoteSourceControl",
6+
"location" : "https://github.com/apple/swift-docc-plugin",
7+
"state" : {
8+
"revision" : "26ac5758409154cc448d7ab82389c520fa8a8247",
9+
"version" : "1.3.0"
10+
}
11+
},
12+
{
13+
"identity" : "swift-docc-symbolkit",
14+
"kind" : "remoteSourceControl",
15+
"location" : "https://github.com/apple/swift-docc-symbolkit",
16+
"state" : {
17+
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
18+
"version" : "1.0.0"
19+
}
20+
},
321
{
422
"identity" : "swift-syntax",
523
"kind" : "remoteSourceControl",

Package.swift

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let package = Package(
1414
],
1515
dependencies: [
1616
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-25-b"),
17+
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.1.0"),
1718
],
1819
targets: [
1920
.macro(

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ A practical collection of Swift Macros that help code correctly and swiftly.
66

77
.package(url: "https://github.com/ShenghaiWang/SwiftMacros.git", from: "1.0.0")
88

9-
## Macros
9+
## Macros [API Doc](https://shenghaiwang.github.io/swiftmacros/documentation/swiftmacros/)
1010

1111
| Macro | Description |
1212
|------------|------------------------------------------------------------|
1313
| @Access |An easy API to access UserDefaults, NSCache and NSMapTable. |
1414
| |<pre>struct TestAccess {<br> static let cache = NSCache<NSString, AnyObject>()<br><br> // Please make sure the generic type is the same as the type of the variable<br> // Without defalut value<br> @Access<Bool?>(.userDefaults())<br> var isPaidUser: Bool?<br><br> // With default value<br> @Access< Bool>(.userDefaults())<br> var isPaidUser2: Bool = false<br><br> @Access<NSObject?>(.nsCache(TestAccess.cache))<br> var hasPaid: NSObject?<br><br> @Access<NSObject?>(.nsMapTable(TestAccess.mapTable))<br> var hasPaid2: NSObject?<br>}</pre>|
1515
|@AddAssociatedValueVariable|Add variables to retrieve the associated values|
1616
| |<pre>@AddAssociatedValueVariable<br>enum MyEnum {<br> case first<br> case second(Int)<br> case third(String, Int)<br> case forth(a: String, b: Int), forth2(String, Int)<br> case fifth(() -> Void)<br>}</pre>|
17-
| @AddPublisher |Generate a Combine publisher to a Combine subject so that we can have a limited ACL for the subject |
17+
| @AddInit |Generate initialiser for the class/struct/actor. The variables with optional types will have nil as default values. Using `withMock: true` if want to generate mock data. <br> For custmoised data type, it will use `Type.mock`. In case there is no this value, need to define this yourself or use `@Mock` or `@AddInit(withMock: true)` to generate this variable. |
18+
| @AddPublisher |Generate a Combine publisher to a Combine subject in order to avoid overexposing subject variable |
1819
| |<pre>@AddPublisher<br>private let mySubject = PassthroughSubject<Void, Never>()</pre>|
19-
| @AddInit |Generate initialiser for the class/struct/actor. the variables with optional types will have nil as default values. Using `withMock: true` if want to generate mock data. <br> For custmoised data type, it will use `Type.mock`. In case there is no this value, need to define this yourself or use `@Mock` or `@AddInit(withMock: true)` to generate this variable. |
2020
| |<pre>@AddInit<br>struct InitStruct {<br> let a: Int<br> let b: Int?<br> let c: (Int?) -> Void<br> let d: ((Int?) -> Void)?<br>}<br>@AddInit(withMock: true)<br>class AStruct {<br> let a: Float<br>}</pre>|
2121
| #buildDate |Build a Date from components<br>This solution addes in a resultBulder `DateBuilder`, which can be used directly if prefer builder pattern.<br>Note: this is for a simpler API. Please use it with caution in places that require efficiency.|
2222
| |<pre>let date = #buildDate(DateString("03/05/2003", dateFormat: "MM/dd/yyyy"),<br> Date(),<br> Month(10),<br> Year(1909),<br> YearForWeekOfYear(2025))</pre>|

Sources/Client/main.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ struct TestStruct: Codable {
2222
var name = "Tim Wang"
2323
}
2424

25+
@Singleton
26+
struct A {
27+
28+
}
29+
2530
let data = try #encode(TestStruct(), dateEncodingStrategy: .iso8601, dataEncodingStrategy: .base64)
2631
let value = try #decode(TestStruct.self, from: data, dateDecodingStrategy: .deferredToDate)
2732

@@ -48,7 +53,7 @@ struct InitStruct {
4853
let d: ((Int?) -> Void)?
4954
}
5055

51-
@AddInit
56+
@AddInit(withMock: true)
5257
actor InitActor {
5358
let a: Int
5459
let b: Int?
@@ -65,6 +70,11 @@ enum MyEnum {
6570
case seventh(() -> Void)
6671
}
6772

73+
@AddAssociatedValueVariable
74+
enum TestEnum {
75+
case test(Int)
76+
}
77+
6878
assert(MyEnum.first.forth2Value == nil)
6979

7080
let url = #buildURL("http://google.com",

Sources/Macros/AddInit.swift

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ extension SimpleTypeIdentifierSyntax {
8989
guard let type = self.as(SimpleTypeIdentifierSyntax.self)?.name.text else { return nil }
9090
if let fun = mockFunctions[type] {
9191
return fun(randomValue)
92+
} else if name.text == "Void" {
93+
return "return"
9294
} else if name.text != "Set" {
9395
return "\(type).mock"
9496
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import Foundation
22

33
public enum AccessContentType {
4+
/// Retrieve value from or store value to [UserDefault](https://developer.apple.com/documentation/foundation/userdefaults).
5+
/// Default to [.standard](https://developer.apple.com/documentation/foundation/userdefaults/1416603-standard).
46
case userDefaults(UserDefaults = .standard)
7+
/// Retrieve value from or store value to [NSCache](https://developer.apple.com/documentation/foundation/nscache).
8+
/// Have to provide an instance of **<NSString, AnyObject>**.
59
case nsCache(NSCache<NSString, AnyObject>)
10+
/// Retrieve value from or store value to [NSMAPTable](https://developer.apple.com/documentation/foundation/nsmaptable)
11+
/// Have to provide an instance of **NSMapTable<NSString, AnyObject>**.
612
case nsMapTable(NSMapTable<NSString, AnyObject>)
713
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# ``SwiftMacros``
2+
3+
A practical collection of Swift Macros that help code correctly and swiftly.
4+
5+
@Metadata {
6+
@PageImage(purpose: icon, source: "macro-icon", alt: "Swift-Macros")
7+
@PageColor(green)
8+
}
9+
10+
## Overview
11+
12+
This collection of Swift Macros aims to remove boilerplate code by automatically generating needed code for a rich set of purposes.
13+
14+
## Topics
15+
16+
### Macros
17+
18+
- ``Access(_:)``
19+
- ``AddAssociatedValueVariable``
20+
- ``AddInit(withMock:randomMockValue:)``
21+
- ``AddPublisher``
22+
- ``buildDate(_:)``
23+
- ``buildURL(_:)``
24+
- ``buildURLRequest(_:)``
25+
- ``encode(_:outputFormatting:dateEncodingStrategy:dataEncodingStrategy:nonConformingFloatEncodingStrategy:keyEncodingStrategy:userInfo:)``
26+
- ``decode(_:from:dateDecodingStrategy:dataDecodingStrategy:nonConformingFloatDecodingStrategy:keyDecodingStrategy:userInfo:allowsJSON5:assumesTopLevelDictionary:)``
27+
- ``formatDate(_:dateStyle:timeStyle:formattingContext:formatterBehavior:doesRelativeDateFormatting:amSymbol:pmSymbol:weekdaySymbols:shortWeekdaySymbols:veryShortWeekdaySymbols:standaloneWeekdaySymbols:shortStandaloneWeekdaySymbols:veryShortStandaloneWeekdaySymbols:monthSymbols:shortMonthSymbols:veryShortMonthSymbols:standaloneMonthSymbols:shortStandaloneMonthSymbols:veryShortStandaloneMonthSymbols:quarterSymbols:shortQuarterSymbols:standaloneQuarterSymbols:shortStandaloneQuarterSymbols:eraSymbols:longEraSymbols:)``
28+
- ``formatDateComponents(fromComponents:allowedUnits:allowsFractionalUnits:calendar:collapsesLargestUnit:includesApproximationPhrase:includesTimeRemainingPhrase:maximumUnitCount:unitsStyle:zeroFormattingBehavior:formattingContext:referenceDate:)``
29+
- ``formatDateComponents(from:to:allowedUnits:allowsFractionalUnits:calendar:collapsesLargestUnit:includesApproximationPhrase:includesTimeRemainingPhrase:maximumUnitCount:unitsStyle:zeroFormattingBehavior:formattingContext:referenceDate:)``
30+
- ``formatDateComponents(fromInterval:allowedUnits:allowsFractionalUnits:calendar:collapsesLargestUnit:includesApproximationPhrase:includesTimeRemainingPhrase:maximumUnitCount:unitsStyle:zeroFormattingBehavior:formattingContext:referenceDate:)``
31+
- ``formatDateInterval(from:to:dateStyle:timeStyle:dateTemplate:calendar:locale:timeZone:)``
32+
- ``Mock(type:randomMockValue:)``
33+
- ``postNotification(_:object:userInfo:from:)``
34+
- ``Singleton``
35+

0 commit comments

Comments
 (0)