Skip to content

iAmMccc/SmartCodableMacro

Repository files navigation

SmartCodableMacro

English | 中文

The official inheritance extension for SmartCodable. Provides the @SmartSubclass macro that auto-generates CodingKeys, init(from:), encode(to:), and required init() for subclasses.

This macro depends on swift-syntax (a large first-time download). Shipping it as a separate package keeps the core SmartCodable library lightweight for projects that don't need class inheritance.

Installation

Requirements: Swift 5.9+ / Xcode 15+.

// Package.swift
dependencies: [
    .package(url: "https://github.com/iAmMccc/SmartCodableMacro.git", branch: "main")
],
targets: [
    .target(
        name: "YourApp",
        dependencies: [
            .product(name: "SmartCodableMacro", package: "SmartCodableMacro")
        ]
    )
]

SmartCodableMacro automatically depends on SmartCodable — no need to declare it separately.

Usage

import SmartCodable
import SmartCodableMacro

class BaseModel: SmartCodableX {
    var name: String = ""
    required init() {}
}

@SmartSubclass
class StudentModel: BaseModel {
    var age: Int = 0
}

At compile time, the macro auto-generates:

  • A CodingKeys enum (only the subclass's own fields)
  • init(from: Decoder) — calls super first, then decodes the subclass's fields
  • encode(to: Encoder) — calls super first, then encodes the subclass's fields
  • required init() — only if the subclass doesn't define one

Key/value mapping in both base and subclass

class BaseModel: SmartCodableX {
    var name: String = ""
    required init() {}
    class func mappingForKey() -> [SmartKeyTransformer]? {
        [CodingKeys.name <--- "stu_name"]
    }
}

@SmartSubclass
class StudentModel: BaseModel {
    var age: Int = 0
    override static func mappingForKey() -> [SmartKeyTransformer]? {
        let trans = [CodingKeys.age <--- "stu_age"]
        if let superTrans = super.mappingForKey() {
            return trans + superTrans
        }
        return trans
    }
}

Example

The repo includes a full iOS demo project covering parent/child encode-decode, key/value mapping, and the didFinishMapping callback:

ExampleApp/ExampleApp.xcodeproj

Open it directly in Xcode. The project references this package locally via XCLocalSwiftPackageReference.

Tests

swift test

About the Core Library

If you only need parsing (no inheritance), use SmartCodable directly — it's lighter and has no swift-syntax dependency.

License

MIT

About

SmartCodable的继承库

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages