SafeTypesMacros 是一个 Swift 包,通过为其类型添加宏字面量初始化器,扩展了 SafeTypes。
通过确保编译时满足条件,SafeTypes 允许开发者编写更安全、更健壮和更具表现力的代码,同时减少样板代码,提高性能,并通过其约束类型改进文档。
SafeTypes 非常棒,而 Macros 使其更出色。
宏,用于通过字面量(值在编译时检查)为 SafeTypes 自定义数据类型提供非可选初始化器。
类型安全的容器,可防止无效状态
在编译时强制执行运行时约束
增强代码可读性和可维护性
简化方法接口和 API
通过消除冗余的不愉快路径检查,简化单元测试
将以下内容添加到您的 Package.swift
文件的 dependencies 中
.package(url: "https://github.com/lucaswkuipers/SafeTypesMacros.git", from: "1.0.0")
或者 File
> Add Package Dependencies
,并输入以下 URL
https://github.com/lucaswkuipers/SafeTypesMacros.git
如果您要操作结果数据类型,建议也安装 SafeTypes 基础库
.package(url: "https://github.com/lucaswkuipers/SafeTypes.git", from: "1.0.0")
或者 File
> Add Package Dependencies
,并输入以下 URL
https://github.com/lucaswkuipers/SafeTypes.git
然后根据需要在任何地方导入
import SafeTypesMacros
import SafeTypes // Optionally
以下是为 SafeTypes
提供的一些宏及其用法的简要示例
宏,用于保证包含至少一个字符的字符串(可以是空白或不可见字符)。
// ✅ Compiles
#NonEmptyString("Alice") // NonEmptyString
#NonEmptyString(" Bob ") // NonEmptyString
#NonEmptyString(" ") // NonEmptyString
// ❌ Fails
#NonEmptyString("") // String can't be empty
#NonEmptyString() // No argument
其中数字是
typealias Number = Numeric & Comparable
宏,用于保证大于零的数字 (值 > 0)
// ✅ Compiles
#Positive(123) // Positive<Int>
#Positive(42.69) // Positive<Double>
// ❌ Fails
#Positive(-1) // Can't be negative
#Positive(0) // Can't be zero
#Positive() // No argument
宏,用于保证小于零的数字 (值 < 0)
// ✅ Compiles
#Negative(-123) // Negative<Int>
#Negative(-42.69) // Negative<Double>
// ❌ Fails
#Negative(1) // Can't be positive
#Negative(0) // Can't be zero
#Negative() // No argument
宏,用于保证小于或等于零的数字 (值 <= 0)
// ✅ Compiles
#NonPositive(-123) // NonPositive<Int>
#NonPositive(-42.69) // NonPositive<Double>
#NonPositive(0) // NonPositive<Int>
#NonPositive(0.0) // NonPositive<Double>
// ❌ Fails
#NonPositive(1) // Can't be positive
#NonPositive() // No argument
宏,用于保证大于或等于零的数字 (值 >= 0)
// ✅ Compiles
#NonNegative(123) // NonNegative<Int>
#NonNegative(42.69) // NonNegative<Double>
#NonNegative(0) // NonNegative<Int>
#NonNegative(0.0) // NonNegative<Double>
// ❌ Fails
#NonNegative(-273.15) // Can't be negative
#NonNegative() // No argument
宏,用于保证不等于零的数字 (值 != 0)
// ✅ Non Optional Initializers
#NonZero(123) // NonZero<Int>
#NonZero(42.69) // NonZero<Double>
#NonZero(-123) // NonZero<Int>
#NonZero(-42.69) // NonZero<Double>
// ❌ Fails to compile
#NonZero(0) // Can't be zero
#NonZero(0.0) // Can't be zero
#NonZero() // No argument
宏,用于范围在 -1 到 1 之间(包括 -1 和 1)的数字 ([-1, 1])
// ✅ Compiles
#MinusOneToOne(-1) // MinusOneToOne<Int>
#MinusOneToOne(-1.0) // MinusOneToOne<Double>
#MinusOneToOne(-0.314159) // MinusOneToOne<Double>
#MinusOneToOne(0) // MinusOneToOne<Int>
#MinusOneToOne(0.0) // MinusOneToOne<Double>
#MinusOneToOne(0.1234) // MinusOneToOne<Double>
#MinusOneToOne(1) // MinusOneToOne<Double>
// ❌ Fails
#MinusOneToOne(-1.1) // Can't be less than -1
#MinusOneToOne(42.1) // Can't be greater than 1
#MinusOneToOne() // No Argument
宏,用于 0 到 1 之间的数字,包括 0 和 1。
// ✅ Compiles
#ZeroToOne(0) // ZeroToOne<Int>
#ZeroToOne(0.0) // ZeroToOne<Double>
#ZeroToOne(0.1234) // ZeroToOne<Double>
#ZeroToOne(1) // ZeroToOne<Double>
// ❌ Fails
#ZeroToOne(-0.5) // Can't be less than 0
#ZeroToOne(42.1) // Can't be greater than 1
#ZeroToOne() // No argument
每种类型都保证符合其声明的约束,因此您的函数和方法可以依赖这些特性并传递它们(保留信息)。
贡献是使开源社区成为学习、启发和创造的绝佳场所的原因。 您所做的任何贡献都非常感谢。
如果您想开始贡献但不知道该做什么,请尝试查看打开的 Issues Tab
Fork 该项目
创建您的分支(git checkout -b your_branch_name
)
提交您的更改(git commit -m 'Add some amazing feature'
)
推送到分支(git push origin your_branch_name
)
打开 Pull Request
哦,别忘了在适用的情况下添加或更新测试! :D
非常感谢您的贡献 <3
根据 MIT 许可证分发。 有关更多信息,请参见 LICENSE。
请随时与我联系
SafeTypes 可以在 Swift Package Index 上找到
一些相关的灵感来源
非常感谢您考虑将 SafeTypes 和 SafeTypesMacros 用于您的下一个 Swift 项目 – 我希望您发现它像我发现编写它一样有趣!