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

JSON-RPC on APIKit

JSON-RPC on APIKit

Yosuke Ishikawa

August 31, 2016
Tweet

More Decks by Yosuke Ishikawa

Other Decks in Technology

Transcript

  1. JSON-RPC on APIKit

    View full-size slide

  2. APIKit
    リクエストの型に応じてレスポンスの型が変わる

    View full-size slide

  3. l
    e
    t r
    e
    q
    u
    e
    s
    t = P
    o
    s
    t
    T
    w
    e
    e
    t
    R
    e
    q
    u
    e
    s
    t
    (
    t
    e
    x
    t
    : "
    H
    e
    l
    l
    o
    , W
    o
    r
    l
    d
    .
    "
    )
    S
    e
    s
    s
    i
    o
    n
    .
    s
    e
    n
    d
    R
    e
    q
    u
    e
    s
    t
    (
    r
    e
    q
    u
    e
    s
    t
    ) { r
    e
    s
    u
    l
    t i
    n
    s
    w
    i
    t
    c
    h r
    e
    s
    u
    l
    t {
    c
    a
    s
    e .
    S
    u
    c
    c
    e
    s
    s
    (
    l
    e
    t t
    w
    e
    e
    t /
    * i
    s T
    w
    e
    e
    t *
    /
    )
    :
    p
    r
    i
    n
    t
    (
    "
    \
    (
    t
    w
    e
    e
    t
    .
    s
    c
    r
    e
    e
    n
    N
    a
    m
    e
    )
    : \
    (
    t
    w
    e
    e
    t
    .
    t
    e
    x
    t
    )
    "
    )
    c
    a
    s
    e .
    F
    a
    i
    l
    u
    r
    e
    (
    l
    e
    t e
    r
    r
    o
    r
    )
    :
    .
    .
    .
    }
    }

    View full-size slide

  4. l
    e
    t r
    e
    q
    u
    e
    s
    t = G
    e
    t
    T
    i
    m
    e
    l
    i
    n
    e
    R
    e
    q
    u
    e
    s
    t
    (
    )
    S
    e
    s
    s
    i
    o
    n
    .
    s
    e
    n
    d
    R
    e
    q
    u
    e
    s
    t
    (
    r
    e
    q
    u
    e
    s
    t
    ) { r
    e
    s
    u
    l
    t i
    n
    s
    w
    i
    t
    c
    h r
    e
    s
    u
    l
    t {
    c
    a
    s
    e .
    S
    u
    c
    c
    e
    s
    s
    (
    l
    e
    t t
    w
    e
    e
    t
    s /
    * i
    s [
    T
    w
    e
    e
    t
    ] *
    /
    )
    :
    .
    .
    .
    c
    a
    s
    e .
    F
    a
    i
    l
    u
    r
    e
    (
    l
    e
    t e
    r
    r
    o
    r
    )
    :
    .
    .
    .
    }
    }

    View full-size slide

  5. p
    r
    o
    t
    o
    c
    o
    l R
    e
    q
    u
    e
    s
    t
    T
    y
    p
    e {
    a
    s
    s
    o
    c
    i
    a
    t
    e
    d
    t
    y
    p
    e R
    e
    s
    p
    o
    n
    s
    e
    .
    .
    .
    }
    c
    l
    a
    s
    s S
    e
    s
    s
    i
    o
    n {
    f
    u
    n
    c s
    e
    n
    d
    R
    e
    q
    u
    e
    s
    t
    <
    R
    e
    q
    u
    e
    s
    t
    : R
    e
    q
    u
    e
    s
    t
    T
    y
    p
    e
    >
    (
    r
    e
    q
    u
    e
    s
    t
    : R
    e
    q
    u
    e
    s
    t
    ,
    h
    a
    n
    d
    l
    e
    r
    : R
    e
    s
    u
    l
    t
    <
    R
    e
    q
    u
    e
    s
    t
    .
    R
    e
    s
    p
    o
    n
    s
    e
    , E
    r
    r
    o
    r
    >
    )
    .
    .
    .
    }

    View full-size slide

  6. JSON-RPC
    リクエストとレスポンスをJSON
    で表す

    View full-size slide

  7. リクエスト
    {
    "
    i
    d
    "
    : 1
    ,
    "
    j
    s
    o
    n
    r
    p
    c
    "
    : "
    2
    .
    0
    "
    ,
    "
    m
    e
    t
    h
    o
    d
    "
    : "
    s
    u
    b
    t
    r
    a
    c
    t
    "
    ,
    "
    p
    a
    r
    a
    m
    s
    "
    : [
    4
    2
    ,
    2
    3
    ]
    }

    View full-size slide

  8. レスポンス
    {
    "
    i
    d
    "
    : 1
    ,
    "
    j
    s
    o
    n
    r
    p
    c
    "
    : "
    2
    .
    0
    "
    ,
    "
    r
    e
    s
    u
    l
    t
    "
    : 1
    9
    }

    View full-size slide

  9. 1
    回の通信で複数のリクエストも送れる

    View full-size slide

  10. リクエスト
    [
    {
    "
    i
    d
    "
    : 1
    ,
    "
    j
    s
    o
    n
    r
    p
    c
    "
    : "
    2
    .
    0
    "
    ,
    "
    m
    e
    t
    h
    o
    d
    "
    : "
    f
    o
    o
    "
    ,
    "
    p
    a
    r
    a
    m
    s
    "
    : { .
    .
    . }
    }
    ,
    {
    "
    i
    d
    "
    : 2
    ,
    "
    j
    s
    o
    n
    r
    p
    c
    "
    : "
    2
    .
    0
    "
    ,
    "
    m
    e
    t
    h
    o
    d
    "
    : "
    b
    a
    r
    "
    ,
    "
    p
    a
    r
    a
    m
    s
    "
    : { .
    .
    . }
    }
    ]

    View full-size slide

  11. レスポンス
    [
    {
    "
    i
    d
    "
    : 1
    ,
    "
    j
    s
    o
    n
    r
    p
    c
    "
    : "
    2
    .
    0
    "
    ,
    "
    r
    e
    s
    u
    l
    t
    "
    : { .
    .
    . }
    }
    ,
    {
    "
    i
    d
    "
    : 2
    ,
    "
    j
    s
    o
    n
    r
    p
    c
    "
    : "
    2
    .
    0
    "
    ,
    "
    r
    e
    s
    u
    l
    t
    "
    : { .
    .
    . }
    }
    ]

    View full-size slide

  12. APIKit
    リクエストの型に応じてレスポンスの型が変わる

    View full-size slide

  13. JSON-PRC on APIKit
    リクエストの型の組み合わせに応じて
    レスポンスの型の組み合わせが変わる

    View full-size slide

  14. リクエスト:
    レスポンス:

    View full-size slide

  15. l
    e
    t b
    a
    t
    c
    h
    R
    e
    q
    u
    e
    s
    t = B
    a
    t
    c
    h
    R
    e
    q
    u
    e
    s
    t
    (
    G
    e
    t
    B
    a
    n
    n
    e
    r
    R
    e
    q
    u
    e
    s
    t
    (
    i
    d
    : 1
    )
    ,
    G
    e
    t
    P
    o
    s
    t
    s
    R
    e
    q
    u
    e
    s
    t
    (
    i
    d
    : 1
    , c
    o
    o
    r
    d
    i
    n
    a
    t
    e
    : c
    o
    o
    r
    d
    i
    n
    a
    t
    e
    )
    ,
    G
    e
    t
    A
    d
    d
    r
    e
    s
    s
    R
    e
    q
    u
    e
    s
    t
    (
    c
    o
    o
    r
    d
    i
    n
    a
    t
    e
    : c
    o
    o
    r
    d
    i
    n
    a
    t
    e
    )
    )
    S
    e
    s
    s
    i
    o
    n
    .
    s
    e
    n
    d
    R
    e
    q
    u
    e
    s
    t
    (
    b
    a
    t
    c
    h
    R
    e
    q
    u
    e
    s
    t
    ) { r
    e
    s
    u
    l
    t i
    n
    s
    w
    i
    t
    c
    h r
    e
    s
    u
    l
    t {
    c
    a
    s
    e .
    S
    u
    c
    c
    e
    s
    s
    (
    l
    e
    t p
    o
    s
    t
    s
    , l
    e
    t b
    a
    n
    n
    e
    r
    , l
    e
    t a
    d
    d
    r
    e
    s
    s
    )
    :
    .
    .
    .
    c
    a
    s
    e .
    F
    a
    i
    l
    u
    r
    e
    (
    l
    e
    t e
    r
    r
    o
    r
    )
    :
    .
    .
    .
    }
    }

    View full-size slide

  16. よさそう

    View full-size slide

  17. 悲報
    Swi
    には可変長の型パラメー
    ター
    がない

    View full-size slide

  18. 任意の個数の型の組み合わせが表せない
    言語の限界だから仕方がない
    あきらめるのか...?

    View full-size slide

  19. せめてSwi
    らしく妥協しよう

    View full-size slide

  20. 参考:
    標準ライブラリ
    世界で最もSwi
    らしいコー

    View full-size slide

  21. f
    u
    n
    c =
    =
    <
    A
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    >
    (
    l
    h
    s
    : (
    A
    )
    , r
    h
    s
    : (
    A
    )
    ) -
    > B
    o
    o
    l
    f
    u
    n
    c =
    =
    <
    A
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , B
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    >
    (
    l
    h
    s
    : (
    A
    , B
    )
    , r
    h
    s
    : (
    f
    u
    n
    c =
    =
    <
    A
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , B
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , C
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    >
    (
    l
    h
    s
    :
    f
    u
    n
    c =
    =
    <
    A
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , B
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , C
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , D
    : E
    q
    u
    a
    t
    a
    b
    f
    u
    n
    c =
    =
    <
    A
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , B
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , C
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , D
    : E
    q
    u
    a
    t
    a
    b
    f
    u
    n
    c =
    =
    <
    A
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , B
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , C
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , D
    : E
    q
    u
    a
    t
    a
    b
    任意の個数の型の組み合わせ(6
    個まで)

    View full-size slide

  22. 筋肉
    これがSwi
    のやり方

    View full-size slide

  23. 理想:
    任意の数のリクエストの組み合わせ
    現実:
    最大3
    個のリクエストの組み合わせ

    View full-size slide

  24. c
    l
    a
    s
    s B
    a
    t
    c
    h
    F
    a
    c
    t
    o
    r
    y {
    .
    .
    .
    f
    u
    n
    c c
    r
    e
    a
    t
    e
    <
    R
    1
    : R
    e
    q
    u
    e
    s
    t
    T
    y
    p
    e
    >
    (
    r
    e
    q
    u
    e
    s
    t
    : R
    1
    ) -
    > B
    a
    t
    c
    h
    <
    f
    u
    n
    c c
    r
    e
    a
    t
    e
    <
    R
    1
    : R
    e
    q
    u
    e
    s
    t
    T
    y
    p
    e
    , R
    2
    : R
    e
    q
    u
    e
    s
    t
    T
    y
    p
    e
    >
    (
    r
    e
    q
    u
    e
    s
    t
    1
    :
    f
    u
    n
    c c
    r
    e
    a
    t
    e
    <
    R
    1
    : R
    e
    q
    u
    e
    s
    t
    T
    y
    p
    e
    , R
    2
    : R
    e
    q
    u
    e
    s
    t
    T
    y
    p
    e
    , R
    3
    : R
    e
    q
    u
    e
    s
    }

    View full-size slide

  25. https://github.com/bricklife/JSONRPCKit
    1.0.0 beta 1

    View full-size slide

  26. 筋肉最高!

    View full-size slide

  27. 筋肉最高?

    View full-size slide

  28. Swi 4 or later

    View full-size slide

  29. Generics Manifesto
    将来実装される可能性がある
    おしゃれコー
    ド一覧

    View full-size slide

  30. Variadic Generics

    View full-size slide

  31. s
    t
    r
    u
    c
    t B
    a
    t
    c
    h
    R
    e
    q
    u
    e
    s
    t
    <
    .
    .
    . R
    e
    q
    u
    e
    s
    t
    s
    : R
    e
    q
    u
    e
    s
    t
    T
    y
    p
    e
    >
    : R
    e
    q
    u
    e
    s
    t
    T
    y
    p
    e
    t
    y
    p
    e
    a
    l
    i
    a
    s R
    e
    s
    p
    o
    n
    s
    e = (
    R
    e
    q
    u
    e
    s
    t
    s
    .
    R
    e
    s
    p
    o
    n
    s
    e
    .
    .
    .
    )
    l
    e
    t (
    .
    .
    .
    r
    e
    q
    u
    e
    s
    t
    s
    )
    : (
    R
    e
    q
    u
    e
    s
    t
    s
    .
    .
    .
    )
    .
    .
    .
    }

    View full-size slide

  32. 筋肉の時代は終わるのかも?

    View full-size slide