Giter VIP home page Giter VIP logo

swiftmacros's Introduction

SwiftMacros

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

Install

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

Macros API Doc

Macro Description
@Access An easy API to access UserDefaults, Keychain, NSCache and NSMapTable.
struct TestAccess {
static let cache = NSCache<NSString, AnyObject>()

// Please make sure the generic type is the same as the type of the variable
// Without defalut value
@Access<Bool?>(.userDefaults())
var isPaidUser: Bool?

// With default value
@Access< Bool>(.userDefaults())
var isPaidUser2: Bool = false

@Access<NSObject?>(.nsCache(TestAccess.cache))
var hasPaid: NSObject?

@Access<NSObject?>(.nsMapTable(TestAccess.mapTable))
var hasPaid2: NSObject?

@Access<TestStruct?>(.keychain)
var keychainValue: TestStruct?
}
@AddAssociatedValueVariable Add variables to retrieve the associated values
@AddAssociatedValueVariable
enum MyEnum {
case first
case second(Int)
case third(String, Int)
case forth(a: String, b: Int), forth2(String, Int)
case fifth(() -> Void)
}
@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.
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
@AddPublisher
private let mySubject = PassthroughSubject<Void, Never>()
@AddInit
struct InitStruct {
let a: Int
let b: Int?
let c: (Int?) -> Void
let d: ((Int?) -> Void)?
}
@AddInit(withMock: true)
class AStruct {
let a: Float
}
#buildDate Build a Date from components
This solution addes in a resultBulder DateBuilder, which can be used directly if prefer builder pattern.
Note: this is for a simpler API. Please use it with caution in places that require efficiency.
let date = #buildDate(DateString("03/05/2003", dateFormat: "MM/dd/yyyy"),
Date(),
Month(10),
Year(1909),
YearForWeekOfYear(2025))
#buildURL Build a url from components.
This solution addes in a resultBulder URLBuilder, which can be used directly if prefer builder pattern.
let url = #buildURL("http://google.com",
URLScheme.https,
URLQueryItems([.init(name: "q1", value: "q1v"), .init(name: "q2", value: "q2v")]))
let url2 = buildURL {
"http://google.com"
URLScheme.https
URLQueryItems([.init(name: "q1", value: "q1v"), .init(name: "q2", value: "q2v")])
}
#buildURLRequest Build a URLRequest from components.
This solution addes in a resultBulder URLRequestBuilder, which can be used directly if prefer builder pattern.
let urlrequest = #buildURLRequest(url!, RequestTimeOutInterval(100))
let urlRequest2 = buildURLRequest {
url!
RequestTimeOutInterval(100)
}
@ConformToEquatable Add Equtable conformance to a class type
Use it with caution per https://github.com/apple/swift-evolution/blob/main/proposals/0185-synthesize-equatable-hashable.md#synthesis-for-class-types-and-tuples
@AddInit
@ConformToEquatable
class AClass {
let a: Int?
let b: () -> Void
}
@ConformToHashable Add Hashable conformance to a class type
Use it with caution per https://github.com/apple/swift-evolution/blob/main/proposals/0185-synthesize-equatable-hashable.md#synthesis-for-class-types-and-tuples
@AddInit
@ConformToHashable
class AClass {
let a: Int?
let b: () -> Void
}
#encode Encode an Encodable to data using JSONEncoder
#encode(value)
#decode Decode a Decodable to a typed value using JSONDecoder
#decode(TestStruct.self, from: data)
#formatDate Format date to a string
#formatDate(Date(), dateStyle: .full)
#formatDateComponents Format date differences/timeinterval/date components to a string
#formatDateComponents(from: Date(), to: Date(), allowedUnits: [.day, .hour, .minute, .second])
#formatDateComponents(fromInterval: 100, allowedUnits: [.day, .hour, .minute, .second])
#formatDateComponents(fromComponents: DateComponents(hour: 10), allowedUnits: [.day, .hour, .minute, .second])
#formatDateInterval Format two dates into interval string
#formatDateInterval(from: Date(), to: Date(), dateStyle: .short)
@Mock Generate a static variable mock using the attached initializer.
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.
class AStruct {
let a: Float
@Mock(type: AStruct.self)
init(a: Float) {
self.a = a
}
}
#postNotification An easy way to post notifications
#postNotification(.NSCalendarDayChanged)
@Singleton Generate Swift singleton code for struct and class types
@Singleton
struct A {}

To be added

Please feel free to add the macros you want here. All PRs(with or without implementations) are welcome.

Macro Description
@ Your new macro ideas

swiftmacros's People

Contributors

shenghaiwang avatar

Stargazers

Dave Coleman avatar Igor Popov avatar 宗仁 avatar Sebastian Fekete avatar Dmitry Kotenko avatar James Phipps avatar ✪ vi4m Marcin Kliks avatar  avatar jinhu avatar 徐山 avatar  avatar Łukasz Stachnik avatar  avatar Noah Pikielny avatar Patrick Lafleur avatar Kaijie Yu avatar  avatar Aron Cedercrantz avatar  avatar Anzor avatar Serhii avatar Zack avatar Konstantinos Nikoloutsos avatar NF avatar Eugene Filipkov avatar Jakub Olejník avatar Petr Sima avatar houseme avatar Frank Foster avatar Alfayed Baksh avatar Erik Flores avatar Pedro Rojas avatar Yash avatar Konstantin Pavlikhin avatar  avatar Javi avatar Patrizio Tagliani avatar Ngoc Luu avatar Ahn Jung Min avatar João Mourato avatar merlinscrypt avatar Alex Antonyuk avatar Tim Kersey avatar Stephan Michels avatar David Penaskovic avatar Roy avatar derekz avatar  avatar  avatar  avatar  avatar Albert Varaksin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.