GithubHelp home page GithubHelp logo

nuclearace / reflection Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zewo/reflection

0.0 2.0 2.0 112 KB

Advanced Swift reflection including dynamic construction of types

License: MIT License

Swift 98.27% Ruby 1.73%

reflection's Introduction

Reflection

Reflection provides an API for advanced reflection at runtime including dynamic construction of types.

Note

This is a fork of Zewo's Reflection library that adds more support for reading the type metadata.

Usage

import Reflection

struct Person {
  var firstName: String
  var lastName: String
  var age: Int
}

// Reflects the instance properties of type `Person`
let properties = try properties(Person)

var person = Person(firstName: "John", lastName: "Smith", age: 35)

// Retrieves the value of `person.firstName`
let firstName: String = try get("firstName", from: person)

// Sets the value of `person.age`
try set(36, key: "age", for: &person)

// Creates a `Person` from a dictionary
let friend: Person = try construct(dictionary: ["firstName" : "Sarah",
                                                "lastName" : "Gates",
                                                "age" : 28])

Installation

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/nuclearace/Reflection.git", majorVersion: 0, minor: 14),
    ]
)

Advanced Usage

// `Reflection` can be extended for higher-level packages to do mapping and serializing.
// Here is a simple `Mappable` protocol that allows deserializing of arbitrary nested structures.

import Reflection

typealias MappableDictionary = [String : Any]

enum Error : ErrorProtocol {
    case missingRequiredValue(key: String)
}

protocol Mappable {
    init(dictionary: MappableDictionary) throws
}

extension Mappable {

    init(dictionary: MappableDictionary) throws {
        self = try construct { property in
            if let value = dictionary[property.key] {
                if let type = property.type as? Mappable.Type, let value = value as? MappableDictionary {
                    return try type.init(dictionary: value)
                } else {
                    return value
                }
            } else {
                throw Error.missingRequiredValue(key: property.key)
            }
        }
    }

}

struct Person : Mappable {
    var firstName: String
    var lastName: String
    var age: Int
    var phoneNumber: PhoneNumber
}

struct PhoneNumber : Mappable {
    var number: String
    var type: String
}

let dictionary = [
    "firstName" : "Jane",
    "lastName" : "Miller",
    "age" : 54,
    "phoneNumber" : [
        "number" : "924-555-0294",
        "type" : "work"
    ] as MappableDictionary
] as MappableDictionary

let person = try Person(dictionary: dictionary)

License

This project is released under the MIT license. See LICENSE for details.

reflection's People

Contributors

bradhilton avatar nuclearace avatar paulofaria avatar antonmes avatar danappelxx avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.