Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Library - Pencil

naru-jpn
November 29, 2016

Swift Library - Pencil

Read/Write struct data on device more easily.

naru-jpn

November 29, 2016
Tweet

More Decks by naru-jpn

Other Decks in Technology

Transcript

  1. Pencil
    Swift Library
    Naruki Chigira - Timers inc.
    GitHub: naru-jpn, Twitter: @naruchigi
    https://github.com/naru-jpn/pencil

    View full-size slide

  2. Motivation / Concept

    View full-size slide

  3. Protocol Buffers (Apple)
    https://github.com/apple/swift-protobuf

    View full-size slide

  4. Protocol buffers are a language-neutral,
    platform-neutral extensible mechanism for
    serializing structured data.
    Protocol Buffers (Google)
    https://developers.google.com/protocol-buffers/

    View full-size slide

  5. Protocol Buffers is a method of serializing structured data. It is
    useful in developing programs to communicate with each other
    over a wire or for storing data.

    The design goals for Protocol Buffers emphasized simplicity and
    performance. In particular, it was designed to be smaller and
    faster than XML.
    Protocol Buffers (Wikipedia)
    https://en.wikipedia.org/wiki/Protocol_Buffers

    View full-size slide

  6. Value types
    +
    Serialize
    +
    Storing data on device
    Pencil

    View full-size slide

  7. ✏…
    Read/Write struct data on device more easily.
    Pencil

    View full-size slide

  8. Write Standard Values (Int)
    // get file URL
    guard let storedURL = Directory.Documents?.append(path: "num.data") else {
    return
    }
    let num: Int = 2016
    // write to file
    num.write(to: storedURL)

    View full-size slide

  9. Read Standard Values (Int)
    // read from file path
    let num = Int.value(from: storedURL)

    View full-size slide

  10. Write Standard Values ([Int])
    // get file URL
    guard let storedURL = Directory.Documents?.append(path: "nums.data") else {
    return
    }
    let nums: Int = [2016, 11, 29]
    // write to file
    nums.write(to: storedURL)

    View full-size slide

  11. Read Standard Values ([Int])
    // read from file path
    let nums = [Int].value(from: storedURL)

    View full-size slide

  12. Standard Values
    Int
    UInt
    Float
    Double
    String
    Array
    Dictionary
    Int8, Int16, Int32, Int64
    UInt8, UInt16, UInt32, UInt64

    View full-size slide

  13. Custom Struct
    // Custom Struct
    struct Sample: CustomReadWriteElement {
    // Parameters
    let dictionary: [String: Int]
    let array: [Int]?
    let identifier: String
    // Function returns Sample from Components
    static var read: (Components) -> Sample? = { components in
    return Sample.init
    =<> components.component(for: "dictionary", defaultValue: ["a": 100])
    -<> components.component(for: "array")
    -<> components.component(for: "identifier", defaultValue: "abc123")
    }
    }

    View full-size slide

  14. Write Custom Struct
    // get file URL
    guard let storedURL = Directory.Documents?.append(path: "sample.data") else {
    return
    }
    let sample: Sample = Sample(dictionary: ["a": 2, "b": 5], array: [2, 3], identifier:
    “id")
    // write to file
    sample.write(to: storedURL)

    View full-size slide

  15. Read Custom Struct
    // read from file path
    let sample = Sample.value(from: storedURL)

    View full-size slide

  16. Write Custom Struct ([Sample])
    // get file URL
    guard let storedURL = Directory.Documents?.append(path: "samples.data") else {
    return
    }
    let sample: Sample = Sample(dictionary: ["a": 2, "b": 5], array: [2, 3], identifier:
    “id”)
    let samples: [Samples] = [sample, sample, sample]
    // write to file
    samples.write(to: storedURL)

    View full-size slide

  17. Read Custom Struct ([Sample])
    // read from file path
    let samples = [Sample].value(from: storedURL)

    View full-size slide

  18. https://arxiv.org

    View full-size slide

  19. Entry
    struct Entry: Equatable, CustomDebugStringConvertible, CustomReadWriteElement {
    var id: String = ""
    var updated: Double = NSDate(timeIntervalSince1970: 0.0).timeIntervalSince1970
    var published: Double = NSDate(timeIntervalSince1970: 0.0).timeIntervalSince1970
    var title: String = ""
    var summary: String = ""
    var authors: [Entry.Author] = []
    var updatedDay: String = ""
    var publishedDay: String = ""
    var htmlLink: String = ""
    var pdfLink: String = ""
    var primaryCategory: String = ""
    struct Author: CustomDebugStringConvertible, CustomReadWriteElement {
    // …
    }
    // …
    }

    View full-size slide

  20. Entry.Author
    struct Author: CustomDebugStringConvertible, CustomReadWriteElement {
    var name: String = ""
    var affiliation: String = ""
    static var read: (Components) -> Entry.Author? = { components in
    return Entry.Author(
    name: components.component(for: "name", defaultValue: ""),
    affiliation: components.component(for: "affiliation", defaultValue: "")
    )
    }
    // …
    }

    View full-size slide

  21. Implementation

    View full-size slide

  22. Data Structure
    Format
    identifier header body

    View full-size slide

  23. Format
    identifier header body
    length + type_name
    (UInt8)
    Data Structure

    View full-size slide

  24. Data Structure (Int)
    identifier header body
    length
    6 → <03496e74 06000000 00000000>
    <03> <496e74>
    + type_name
    <> <06000000 00000000>

    View full-size slide

  25. Data Structure (String)
    identifier header body
    length
    "pencil" → <06537472 696e6706 0070656e 63696c>
    <06> <53747269 6e67>
    + type_name
    <0600> <70656e63 696c>

    View full-size slide

  26. Data Structure ([Int])
    identifier header body
    length
    [1,2,3] → <0a417272 61793c49 6e743e03 000c0000 000c0000
    000c0000 0003496e 74010000 00000000 0003496e 74020000
    00000000 0003496e 74030000 00000000 00>
    <0a>
    <417272 61793c49 6e743e>
    + type_name
    <03000c00 00000c00 00000c00 0000>
    <03496e74 01000000 00000000 03496e74 02000000 00000000 03496e74
    03000000 00000000>

    View full-size slide

  27. Data Structure ([Int])
    identifier header body
    length
    [1,2,3] → <0a417272 61793c49 6e743e03 000c0000 000c0000
    000c0000 0003496e 74010000 00000000 0003496e 74020000
    00000000 0003496e 74030000 00000000 00>
    <0a>
    <417272 61793c49 6e743e>
    + type_name
    <03000c00 00000c00 00000c00 0000>
    <03496e74 01000000 00000000 03496e74 02000000 00000000 03496e74
    03000000 00000000>

    View full-size slide

  28. Data Structure ([Int])
    identifier header body
    length
    [1,2,3] → <0a417272 61793c49 6e743e03 000c0000 000c0000
    000c0000 0003496e 74010000 00000000 0003496e 74020000
    00000000 0003496e 74030000 00000000 00>
    <0a>
    <417272 61793c49 6e743e>
    + type_name
    <03000c00 00000c00 00000c00 0000>
    <03496e74 01000000 00000000 03496e74 02000000 00000000 03496e74
    03000000 00000000>

    View full-size slide

  29. I’m waiting for your Issue, PR and Star

    View full-size slide