Swiftpack.co - Package - tris-foundation/reflection

Reflection

Maps dictionary values to any foreign struct. No protocol conformance required.

Warning

Swift ABI is still changing, use the package with caution.
Current version was tested with Swift 4.1/master snapshots from December 27, 2017.

Package.swift

.package(url: "https://github.com/tris-foundation/reflection.git", .branch("master"))

Usage

You can find this code and more in examples.

struct Person {
    let name: String
    let age: Int
}

let blueprint = Blueprint(of: Person.self)

Strict type

let values: [String : Any] = ["name" : "Tony", "age": 16]
let tony = blueprint.construct(using: values)

Parse string values

let values = ["name" : "Tony", "age" : "16"]
let tony = blueprint.construct(using: stringValues) // ok
let tonyNil = blueprint.construct(using: stringValues, shouldConvert: false) // nil

Nested struct

struct User {
    let username: String
    let person: Person
}

let blueprint = Blueprint(of: User.self)

let values: [String : Any] = [
    "username" : "tonyfreeman",
    "person" : [
        "name" : "Tony",
        "age" : "16"
    ]
]

let user = blueprint.construct(using: values)

Github

link
Stars: 7
Help us keep the lights on

Dependencies