Skip to content

Commit

Permalink
Merge AddDocc into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenghaiWang committed Jul 1, 2023
2 parents f1d71f8 + e38f630 commit 11fb238
Show file tree
Hide file tree
Showing 9 changed files with 518 additions and 6 deletions.
18 changes: 18 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
{
"pins" : [
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin",
"state" : {
"revision" : "26ac5758409154cc448d7ab82389c520fa8a8247",
"version" : "1.3.0"
}
},
{
"identity" : "swift-docc-symbolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-symbolkit",
"state" : {
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
"version" : "1.0.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
Expand Down
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-25-b"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.1.0"),
],
targets: [
.macro(
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ A practical collection of Swift Macros that help code correctly and swiftly.

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

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

| Macro | Description |
|------------|------------------------------------------------------------|
| @Access |An easy API to access UserDefaults, NSCache and NSMapTable. |
| |<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>|
|@AddAssociatedValueVariable|Add variables to retrieve the associated values|
| |<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>|
| @AddPublisher |Generate a Combine publisher to a Combine subject so that we can have a limited ACL for the subject |
| @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. |
| @AddPublisher |Generate a Combine publisher to a Combine subject in order to avoid overexposing subject variable |
| |<pre>@AddPublisher<br>private let mySubject = PassthroughSubject<Void, Never>()</pre>|
| @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. |
| |<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>|
| #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.|
| |<pre>let date = #buildDate(DateString("03/05/2003", dateFormat: "MM/dd/yyyy"),<br> Date(),<br> Month(10),<br> Year(1909),<br> YearForWeekOfYear(2025))</pre>|
Expand Down
12 changes: 11 additions & 1 deletion Sources/Client/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ struct TestStruct: Codable {
var name = "Tim Wang"
}

@Singleton
struct A {

}

let data = try #encode(TestStruct(), dateEncodingStrategy: .iso8601, dataEncodingStrategy: .base64)
let value = try #decode(TestStruct.self, from: data, dateDecodingStrategy: .deferredToDate)

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

@AddInit
@AddInit(withMock: true)
actor InitActor {
let a: Int
let b: Int?
Expand All @@ -65,6 +70,11 @@ enum MyEnum {
case seventh(() -> Void)
}

@AddAssociatedValueVariable
enum TestEnum {
case test(Int)
}

assert(MyEnum.first.forth2Value == nil)

let url = #buildURL("http://google.com",
Expand Down
2 changes: 2 additions & 0 deletions Sources/Macros/AddInit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ extension SimpleTypeIdentifierSyntax {
guard let type = self.as(SimpleTypeIdentifierSyntax.self)?.name.text else { return nil }
if let fun = mockFunctions[type] {
return fun(randomValue)
} else if name.text == "Void" {
return "return"
} else if name.text != "Set" {
return "\(type).mock"
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/SwiftMacros/AccessContentType.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import Foundation

public enum AccessContentType {
/// Retrieve value from or store value to [UserDefault](https://developer.apple.com/documentation/foundation/userdefaults).
/// Default to [.standard](https://developer.apple.com/documentation/foundation/userdefaults/1416603-standard).
case userDefaults(UserDefaults = .standard)
/// Retrieve value from or store value to [NSCache](https://developer.apple.com/documentation/foundation/nscache).
/// Have to provide an instance of **<NSString, AnyObject>**.
case nsCache(NSCache<NSString, AnyObject>)
/// Retrieve value from or store value to [NSMAPTable](https://developer.apple.com/documentation/foundation/nsmaptable)
/// Have to provide an instance of **NSMapTable<NSString, AnyObject>**.
case nsMapTable(NSMapTable<NSString, AnyObject>)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions Sources/SwiftMacros/SwiftMacros.docc/SwiftMacros.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ``SwiftMacros``

A practical collection of Swift Macros that help code correctly and swiftly.

@Metadata {
@PageImage(purpose: icon, source: "macro-icon", alt: "Swift-Macros")
@PageColor(green)
}

## Overview

This collection of Swift Macros aims to remove boilerplate code by automatically generating needed code for a rich set of purposes.

## Topics

### Macros

- ``Access(_:)``
- ``AddAssociatedValueVariable``
- ``AddInit(withMock:randomMockValue:)``
- ``AddPublisher``
- ``buildDate(_:)``
- ``buildURL(_:)``
- ``buildURLRequest(_:)``
- ``encode(_:outputFormatting:dateEncodingStrategy:dataEncodingStrategy:nonConformingFloatEncodingStrategy:keyEncodingStrategy:userInfo:)``
- ``decode(_:from:dateDecodingStrategy:dataDecodingStrategy:nonConformingFloatDecodingStrategy:keyDecodingStrategy:userInfo:allowsJSON5:assumesTopLevelDictionary:)``
- ``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:)``
- ``formatDateComponents(fromComponents:allowedUnits:allowsFractionalUnits:calendar:collapsesLargestUnit:includesApproximationPhrase:includesTimeRemainingPhrase:maximumUnitCount:unitsStyle:zeroFormattingBehavior:formattingContext:referenceDate:)``
- ``formatDateComponents(from:to:allowedUnits:allowsFractionalUnits:calendar:collapsesLargestUnit:includesApproximationPhrase:includesTimeRemainingPhrase:maximumUnitCount:unitsStyle:zeroFormattingBehavior:formattingContext:referenceDate:)``
- ``formatDateComponents(fromInterval:allowedUnits:allowsFractionalUnits:calendar:collapsesLargestUnit:includesApproximationPhrase:includesTimeRemainingPhrase:maximumUnitCount:unitsStyle:zeroFormattingBehavior:formattingContext:referenceDate:)``
- ``formatDateInterval(from:to:dateStyle:timeStyle:dateTemplate:calendar:locale:timeZone:)``
- ``Mock(type:randomMockValue:)``
- ``postNotification(_:object:userInfo:from:)``
- ``Singleton``

Loading

0 comments on commit 11fb238

Please sign in to comment.