Skip to content

Commit 16b2278

Browse files
committed
Rename Flat to FlatCoding
1 parent 31130a3 commit 16b2278

File tree

8 files changed

+23
-17
lines changed

8 files changed

+23
-17
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Main features include:
5252
- Support flexible type conversion between basic data types like `Bool`, `String`, `Double`, `Int`, `CGFloat` through `@FlexibleType`
5353
- Support BigInt `Int128`, `UInt128` on macOS 15+, iOS 13+
5454
- Support encoding/decoding of `Any` through `AnyCodable`, like `var dict = [String: AnyCodable]`
55-
- Flatten nested property into the parent structure during coding using `@Flat`
55+
- Flatten nested property into the parent structure during coding using `@FlatCoding`
5656
- Auto-generate default instances:
5757
Use `@DefaultInstance` to automatically create a default instance of your type,
5858
accessible through `Model.default`
@@ -922,7 +922,7 @@ struct User3: Equatable {
922922
}
923923
```
924924

925-
### 21. Flatten Property with `@Flat`
925+
### 21. Flatten Property with `@FlatCoding`
926926

927927
Flatten a nested property so that its fields are encoded/decoded at the same level as the enclosing type.
928928

@@ -932,7 +932,7 @@ struct User {
932932
var name: String
933933
var age: Int = 0
934934

935-
@Flat
935+
@FlatCoding
936936
var address: Address
937937
}
938938

README_CN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ ReerCodable 框架提供了一系列自定义宏,用于生成动态的 Codable
4747
- 通过 `@FlexibleType` 支持 `Bool`, `String`, `Double`, `Int`, `CGFloat` 等基本数据类型互相转换
4848
- 支持 BigInt `Int128`, `UInt128` (限 macOS 15+, iOS 13+)
4949
- 支持通过 `AnyCodable` 来实现对 `Any` 的编解码, 如 `var dict = [String: AnyCodable]`
50-
- 使用 `@Flat` 在编解码时将嵌套属性“拍平”到父级结构
50+
- 使用 `@FlatCoding` 在编解码时将嵌套属性“拍平”到父级结构
5151
- 支持通过 `@DefaultInstance` 生成一个 `static let default: Model` 实例, `Model.default`
5252
- 支持通过 `@Copyable` 生成 `copy()` 方法, 并且支持部分属性值的 update
5353
- 自动生成默认实例:使用 `@DefaultInstance` 自动创建类型的默认实例, 可通过 Model.default 访问
@@ -917,7 +917,7 @@ struct User3: Equatable {
917917
}
918918
```
919919

920-
### 21. 使用 `@Flat` 扁平化属性
920+
### 21. 使用 `@FlatCoding` 扁平化属性
921921

922922
将某个嵌套属性在编解码时“拍平”,使其字段与父类型位于同一层级进行编码/解码。
923923

@@ -927,7 +927,7 @@ struct User {
927927
var name: String
928928
var age: Int = 0
929929

930-
@Flat
930+
@FlatCoding
931931
var address: Address
932932
}
933933

Sources/ReerCodable/MacroDeclarations/Flat.swift renamed to Sources/ReerCodable/MacroDeclarations/FlatCoding.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
// THE SOFTWARE.
2121

22-
/// The `@Flat` macro flattens a property during encoding and decoding.
22+
/// The `@FlatCoding` macro flattens a property during encoding and decoding.
2323
///
2424
/// When applied to a property:
2525
/// - Decoding: The property's value is decoded from the same decoder as the enclosing type
@@ -35,7 +35,7 @@
3535
/// var name: String
3636
/// var age: Int = 0
3737
///
38-
/// @Flat
38+
/// @FlatCoding
3939
/// var address: Address
4040
/// }
4141
///
@@ -59,6 +59,11 @@
5959
/// - Can only be applied to a stored property.
6060
/// - Cannot be used together with any other macros on the same property; otherwise an error is thrown.
6161
@attached(peer)
62-
public macro Flat() = #externalMacro(module: "ReerCodableMacros", type: "Flat")
62+
public macro FlatCoding() = #externalMacro(module: "ReerCodableMacros", type: "FlatCoding")
63+
64+
/// Deprecated: Use `@FlatCoding` instead.
65+
@available(*, deprecated, renamed: "FlatCoding")
66+
@attached(peer)
67+
public macro Flat() = #externalMacro(module: "ReerCodableMacros", type: "FlatCoding")
6368

6469

Sources/ReerCodableMacros/MacroImplementations/FlatImpl.swift renamed to Sources/ReerCodableMacros/MacroImplementations/FlatCodingImpl.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@
2222
import SwiftSyntax
2323
import SwiftSyntaxMacros
2424

25-
public struct Flat: PeerMacro {
25+
public struct FlatCoding: PeerMacro {
2626
public static func expansion(
2727
of node: AttributeSyntax,
2828
providingPeersOf declaration: some DeclSyntaxProtocol,
2929
in context: some MacroExpansionContext
3030
) throws -> [DeclSyntax] {
3131
guard let variableDecl = declaration.as(VariableDeclSyntax.self) else {
32-
throw MacroError(text: "@Flat macro is only for a stored property.")
32+
throw MacroError(text: "@FlatCoding macro is only for a stored property.")
3333
}
3434
let attributes = variableDecl.attributes.compactMap { attr in
3535
attr.as(AttributeSyntax.self)?.attributeName.as(IdentifierTypeSyntax.self)?.name.text
3636
}
37-
if attributes.contains(where: { $0 != "Flat" }) {
38-
throw MacroError(text: "@Flat cannot be used together with other macros on the same property.")
37+
if attributes.contains(where: { $0 != "FlatCoding" && $0 != "Flat" }) {
38+
throw MacroError(text: "@FlatCoding cannot be used together with other macros on the same property.")
3939
}
4040
return []
4141
}

Sources/ReerCodableMacros/Plugins.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct ReerCodablePlugin: CompilerPlugin {
5757
DecodingDefault.self,
5858
EncodingDefault.self,
5959
CodingDefault.self,
60-
Flat.self,
60+
FlatCoding.self,
6161
Base64Coding.self,
6262
DateCoding.self,
6363
CompactDecoding.self,

Sources/ReerCodableMacros/TypeInfo.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ extension TypeInfo {
321321
}
322322

323323
// flat
324-
if variable.attributes.containsAttribute(named: "Flat") {
324+
if variable.attributes.containsAttribute(named: "FlatCoding")
325+
|| variable.attributes.containsAttribute(named: "Flat") {
325326
property.isFlat = true
326327
}
327328

Tests/ReerCodableTests/PropertyListCodingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct PlistAddress {
8585
struct PlistFlatModel {
8686
var name: String
8787

88-
@Flat
88+
@FlatCoding
8989
var address: PlistAddress
9090
}
9191

Tests/ReerCodableTests/ReerCodableTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ struct FlatUser {
350350
var name: String
351351
var age: Int = 0
352352

353-
@Flat
353+
@FlatCoding
354354
var address: FlatAddress
355355
}
356356

0 commit comments

Comments
 (0)