Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/p-x9/aliasmacro

๐ŸŽญ A Swift macros for defining aliases for types, functions, variables, etc.
https://github.com/p-x9/aliasmacro

macro spm swiftmacros swiftpackage swiftpackagemanager

Last synced: 24 days ago
JSON representation

๐ŸŽญ A Swift macros for defining aliases for types, functions, variables, etc.

Awesome Lists containing this project

README

        

# AliasMacro

Swift Macro for defining aliases for types, functions, or variables.

[![Github issues](https://img.shields.io/github/issues/p-x9/AliasMacro)](https://github.com/p-x9/AliasMacro/issues)
[![Github forks](https://img.shields.io/github/forks/p-x9/AliasMacro)](https://github.com/p-x9/AliasMacro/network/members)
[![Github stars](https://img.shields.io/github/stars/p-x9/AliasMacro)](https://github.com/p-x9/AliasMacro/stargazers)
[![Github top language](https://img.shields.io/github/languages/top/p-x9/AliasMacro)](https://github.com/p-x9/AliasMacro/)

## Usage

A macro that can be attached to a type, function or variable.

### Variable

You can define an alias for a variable as follows

```swift
class SomeClass {
@Alias("title")
var text: String = "hello"
}

/* โ†“โ†“โ†“โ†“โ†“ */

let someClass = SomeClass()

print(text) // => "hello"
print(title) // => "hello"
```

### Function/Method

You can define an alias for a function as follows.
In this way, you can call both `hello("aaa", at: Date())` and `ใ“ใ‚“ใซใกใฏ("aaa", at: Date())`.

```swift
class SomeClass {
@Alias("ใ“ใ‚“ใซใกใฏ")
func hello(_ text: String, at date: Date) {
/* --- */
print(text)
}
}

/* โ†“โ†“โ†“โ†“โ†“ */

let someClass = SomeClass()

someClass.hello("aaa", at: Date()) // => "aaa"
someClass.ใ“ใ‚“ใซใกใฏ("aaa", at: Date()) // => "aaa"
```

#### Customize Argument Label

Argument labels can also be customized by separating them with ":".

```swift
class SomeClass {
@Alias("ใ“ใ‚“ใซใกใฏ::ใ„ใค")
func hello(_ text: String, at date: Date) {
/* --- */
print(text)
}

@Alias("ใ“ใ‚“ใซใกใฏ2:_:_") // Omit argument labels
func hello2(_ text: String, at date: Date) {
/* --- */
print(text)
}

@Alias("ใ“ใ‚“ใซใกใฏ3::ๅฎ›") // The first argument label is inherited. The second is customized
func hello3(_ text: String, at date: Date, to: String) {
/* --- */
print(text)
}
}

/* โ†“โ†“โ†“โ†“โ†“ */

let someClass = SomeClass()

someClass.hello("aaa", at: Date()) // => "aaa"
someClass.ใ“ใ‚“ใซใกใฏ("aaa", ใ„ใค: Date()) // => "aaa"

someClass.hello2("aaa", at: Date()) // => "aaa"
someClass.ใ“ใ‚“ใซใกใฏ2("aaa", Date()) // => "aaa"

someClass.hello3("aaa", at: Date(), to: "you") // => "aaa"
someClass.ใ“ใ‚“ใซใกใฏ3("aaa", at: Date(), ๅฎ›: "ใ‚ใชใŸ") // => "aaa"
```

### Enum Case

You can define alias for enum case.

For example, suppose we define the following.

```swift
enum Difficulty {
@Alias("beginner")
case easy

@Alias("normal")
case medium

@Alias("challenge")
case hard

@Alias("extreme")
case expert

@Alias("ultimate")
case master(level: Int)
}
```

At this time, the macro is expanded as follows.

```swift
enum Difficulty {
case easy
case medium
case hard
case expert
case master(level: Int)

static let beginner: Self = .easy
static let normal: Self = .medium
static let challenge: Self = .hard
static let extreme: Self = .expert

static func ultimate(level: Int) -> Self {
.master(level)
}
}
```

### associatedtype

```swift
protocol APIRequest {
@Alias("Reply")
associatedtype Response
}
```

โ†“โ†“โ†“

```swift
protocol APIRequest {
associatedtype Response

typealias Reply = Response
}
```

### Class/Struct/Enum/Actor

For example, the `ViewController` can also be referenced as a `VC` by writing the following.
(If this macro is used for type, it is defined internally simply using `typealias`.)

> **Warning**
> `PeerMacro` with arbitrary specified in `names` cannot be used in global scope.
> [Restrictions on arbitrary names](https://github.com/apple/swift-evolution/blob/main/proposals/0389-attached-macros.md#restrictions-on-arbitrary-names)

```swift
@Alias("VC")
class ViewContoroller: UIViewController {
/* --- */
}

/* โ†“โ†“โ†“โ†“โ†“ */

print(ViewContoroller.self) // => "ViewController"
print(VC.self) // => "ViewController"
```

### Multiple Aliases

Multiple aliases can be defined by adding multiple macros as follows

```swift
@Alias("hello")
@Alias("ใ“ใ‚“ใซใกใฏ")
var text: String = "Hello"
```

### Specify Access Modifier of Alias

You can specify an alias access modifier as follows.

```swift
@Alias("hello", access: .public)
private var text: String = "Hello"
```

If set "inherit", it will inherit from the original definition.

```swift
@Alias("hello", access: .inherit)
private var text: String = "Hello"
```

## License

AliasMacro is released under the MIT License. See [LICENSE](./LICENSE)