SwiftyLab / MetaCodable

Supercharge Swift's Codable implementations with macros meta-programming.
https://swiftpackageindex.com/SwiftyLab/MetaCodable/main/documentation/metacodable
MIT License
629 stars 23 forks source link

Set default values ​​for mutable variables via initialization syntax. #74

Open SouHanaQiao opened 7 months ago

SouHanaQiao commented 7 months ago

1. Mutable variables can have default values ​​added by adding initialization syntax.

We can get the default value whether it is a surface value or an expression from binding.initializer?.value. Like this:

var int1: Int = 10
var int2: Int = Int()
var ini3: Int = int1

int1 default value is an integer literal. int2 default value is a function expression. int3 default value is a variable reference.

2. Usage:

let globalVariable: Int = 0

@Codable
@MemberInit
struct Example {
    @Default(10)
    let int: Int   /// defaul value is 10
    var int2: Int = 10 /// default value is 10
    @Default(10)
    var int3: Int = 20 /// default value is 10 when apply `Default` attribute
    var int4: Int = 4, int5: Int = 5 /// `int4` default value is 4, `int5` default value is 5
    var int6: Int = globalVariable
}

3. The expanded results are as follows:

init(int: Int = 10) {
    self.int = int
}

init(int: Int = 10, int2: Int) {
    self.int = int
    self.int2 = int2
}

init(int: Int = 10, int3: Int) {
    self.int = int
    self.int3 = int3
}

init(int: Int = 10, int2: Int, int3: Int) {
    self.int = int
    self.int2 = int2
    self.int3 = int3
}

init(int: Int = 10, int4: Int) {
    self.int = int
    self.int4 = int4
}

init(int: Int = 10, int2: Int, int4: Int) {
    self.int = int
    self.int2 = int2
    self.int4 = int4
}

init(int: Int = 10, int3: Int, int4: Int) {
    self.int = int
    self.int3 = int3
    self.int4 = int4
}

init(int: Int = 10, int2: Int, int3: Int, int4: Int) {
    self.int = int
    self.int2 = int2
    self.int3 = int3
    self.int4 = int4
}

init(int: Int = 10, int5: Int) {
    self.int = int
    self.int5 = int5
}

init(int: Int = 10, int2: Int, int5: Int) {
    self.int = int
    self.int2 = int2
    self.int5 = int5
}

init(int: Int = 10, int3: Int, int5: Int) {
    self.int = int
    self.int3 = int3
    self.int5 = int5
}

init(int: Int = 10, int2: Int, int3: Int, int5: Int) {
    self.int = int
    self.int2 = int2
    self.int3 = int3
    self.int5 = int5
}

init(int: Int = 10, int4: Int, int5: Int) {
    self.int = int
    self.int4 = int4
    self.int5 = int5
}

init(int: Int = 10, int2: Int, int4: Int, int5: Int) {
    self.int = int
    self.int2 = int2
    self.int4 = int4
    self.int5 = int5
}

init(int: Int = 10, int3: Int, int4: Int, int5: Int) {
    self.int = int
    self.int3 = int3
    self.int4 = int4
    self.int5 = int5
}

init(int: Int = 10, int2: Int, int3: Int, int4: Int, int5: Int) {
    self.int = int
    self.int2 = int2
    self.int3 = int3
    self.int4 = int4
    self.int5 = int5
}

init(int: Int = 10, int6: Int) {
    self.int = int
    self.int6 = int6
}

init(int: Int = 10, int2: Int, int6: Int) {
    self.int = int
    self.int2 = int2
    self.int6 = int6
}

init(int: Int = 10, int3: Int, int6: Int) {
    self.int = int
    self.int3 = int3
    self.int6 = int6
}

init(int: Int = 10, int2: Int, int3: Int, int6: Int) {
    self.int = int
    self.int2 = int2
    self.int3 = int3
    self.int6 = int6
}

init(int: Int = 10, int4: Int, int6: Int) {
    self.int = int
    self.int4 = int4
    self.int6 = int6
}

init(int: Int = 10, int2: Int, int4: Int, int6: Int) {
    self.int = int
    self.int2 = int2
    self.int4 = int4
    self.int6 = int6
}

init(int: Int = 10, int3: Int, int4: Int, int6: Int) {
    self.int = int
    self.int3 = int3
    self.int4 = int4
    self.int6 = int6
}

init(int: Int = 10, int2: Int, int3: Int, int4: Int, int6: Int) {
    self.int = int
    self.int2 = int2
    self.int3 = int3
    self.int4 = int4
    self.int6 = int6
}

init(int: Int = 10, int5: Int, int6: Int) {
    self.int = int
    self.int5 = int5
    self.int6 = int6
}

init(int: Int = 10, int2: Int, int5: Int, int6: Int) {
    self.int = int
    self.int2 = int2
    self.int5 = int5
    self.int6 = int6
}

init(int: Int = 10, int3: Int, int5: Int, int6: Int) {
    self.int = int
    self.int3 = int3
    self.int5 = int5
    self.int6 = int6
}

init(int: Int = 10, int2: Int, int3: Int, int5: Int, int6: Int) {
    self.int = int
    self.int2 = int2
    self.int3 = int3
    self.int5 = int5
    self.int6 = int6
}

init(int: Int = 10, int4: Int, int5: Int, int6: Int) {
    self.int = int
    self.int4 = int4
    self.int5 = int5
    self.int6 = int6
}

init(int: Int = 10, int2: Int, int4: Int, int5: Int, int6: Int) {
    self.int = int
    self.int2 = int2
    self.int4 = int4
    self.int5 = int5
    self.int6 = int6
}

init(int: Int = 10, int3: Int, int4: Int, int5: Int, int6: Int) {
    self.int = int
    self.int3 = int3
    self.int4 = int4
    self.int5 = int5
    self.int6 = int6
}

init(int: Int = 10, int2: Int, int3: Int, int4: Int, int5: Int, int6: Int) {
    self.int = int
    self.int2 = int2
    self.int3 = int3
    self.int4 = int4
    self.int5 = int5
    self.int6 = int6
}

extension Example: Decodable {
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        do {
            self.int = try container.decodeIfPresent(Int.self, forKey: CodingKeys.int) ?? 10
        } catch {
            self.int = 10
        }
        do {
            self.int2 = try container.decodeIfPresent(Int.self, forKey: CodingKeys.int2) ?? 10 /// default value is 10
        } catch {
            self.int2 = 10 /// default value is 10
        }
        do {
            self.int3 = try container.decodeIfPresent(Int.self, forKey: CodingKeys.int3) ?? 10
        } catch {
            self.int3 = 10
        }
        do {
            self.int4 = try container.decodeIfPresent(Int.self, forKey: CodingKeys.int4) ?? 4
        } catch {
            self.int4 = 4
        }
        do {
            self.int5 = try container.decodeIfPresent(Int.self, forKey: CodingKeys.int5) ?? 5 /// `int4` default value is 4, `int5` default value is 5
        } catch {
            self.int5 = 5 /// `int4` default value is 4, `int5` default value is 5
        }
        do {
            self.int6 = try container.decodeIfPresent(Int.self, forKey: CodingKeys.int6) ?? globalVariable
        } catch {
            self.int6 = globalVariable
        }
    }
}

extension Example: Encodable {
    func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(self.int, forKey: CodingKeys.int)
        try container.encode(self.int2, forKey: CodingKeys.int2)
        try container.encode(self.int3, forKey: CodingKeys.int3)
        try container.encode(self.int4, forKey: CodingKeys.int4)
        try container.encode(self.int5, forKey: CodingKeys.int5)
        try container.encode(self.int6, forKey: CodingKeys.int6)
    }
}

extension Example {
    enum CodingKeys: String, CodingKey {
        case int = "int"
        case int2 = "int2"
        case int3 = "int3"
        case int4 = "int4"
        case int5 = "int5"
        case int6 = "int6"
    }
}

As expected except there are too many initialization methods. But the next step can be to just generate an initializing constructor containing all parameters with default values.

soumyamahunt commented 7 months ago

@SouHanaQiao thanks for this PR, the original reason for generating different initializers was optimization, i.e. if there is no data provided for int2 in your example it won't be re-initialized again. This is also what the compiler follows when generating memberwise initializers. Can you provide what benefits your change will provide by removing this optimization?

Also, it seems like with your change int2 is using assigned value as default value even without user input for Default value macro. I don't want this default behavior with this library as the default expectation for users are decoding failure error unless they want to override with a default value.