Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Type-safe Web APIs with
Protocol Buffers in Swift
1 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kit...
Hi, I'm Yusuke
@kitasuke
2 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
I'm going to talk about...
→ What Protocol Buffers are
3 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita ...
I'm going to talk about...
→ What Protocol Buffers are
→ Pros and Cons
4 — Type-safe Web APIs with Protocol Buffers in Swi...
I'm going to talk about...
→ What Protocol Buffers are
→ Pros and Cons
→ Where to start
5 — Type-safe Web APIs with Protoc...
Web APIs
6 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
JSON7 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
HTTP Request
JSON ➡ Data
8 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
let json = ["id": 1]
let data = try? JSONSerialization.data(withJSONObject: json,
options: .prettyPrinted)
var request = U...
HTTP Response
Data ➡ JSON
10 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
// {"user": {"name": "Yusuke"}}
URLSession.shared.dataTask(with: request) { (data, _, _) in
let json = (try? JSONSerializa...
{"isTyped": false,
"status": "disappointed"}
12 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuk...
[AnyHashable: Any]
13 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Any?
14 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
!
15 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Protocol
Buffersa.k.a protobuf
16 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Protocol buffers are Google's language-neutral,
platform-neutral, extensible mechanism for
serializing structured data – t...
Serialization format
- think JSON, but
smaller, faster and safer
— Yusuke
18 — Type-safe Web APIs with Protocol Buffers in...
Serialization
Format
19 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Why are Protocol Buffers
a big deal?
20 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSC...
HTTP Request
JSON ➡ Data
21 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
let json = ["id": 1]
let data = try? JSONSerialization.data(withJSONObject: json,
options: .prettyPrinted)
var request = U...
HTTP Request
JSON ➡ Data
23 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
HTTP Request
UserRequest ➡ Data
24 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 20...
// custom type
let userRequest = UserRequest.with {
$0.id = 1
}
let body = try? body.serializedData()
var request = URLReq...
HTTP Response
Data ➡ JSON
26 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
// {"user": {"name": "Yusuke"}}
URLSession.shared.dataTask(with: request) { (data, _, _) in
let json = (try? JSONSerializa...
HTTP Response
Data ➡ JSON
28 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
HTTP Response
Data ➡ UserResponse
29 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon ...
// UserResponse(user: User(name: "Yusuke"))
URLSession.shared.dataTask(with: request) { (data, _, _) in
// custom type
gua...
Protobuf(
isTyped: true,
status: .satisfied
)
31 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasu...
!
32 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
But how do
they work?
33 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
protobuf for Swift
apple/swift-protobuf
34 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), i...
"But we still use
Objective-C !"
35 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2...
protobuf
google/protobuf
36 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
1. Define message types
2. Generate code
3. Serialize/Deserialize
37 — Type-safe Web APIs with Protocol Buffers in Swift, Y...
1. Define message types
2. Generate code
3. Serialize/Deserialize
38 — Type-safe Web APIs with Protocol Buffers in Swift, Y...
Message types define
data structures
in .proto files
39 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@k...
Message types have key-
value pairs
40 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCo...
41 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
user.proto
syntax = "proto3"; // protoc version
message User {
int32 id = 1; // field number
string name = 2;
string intro...
talk.proto
syntax = "proto3";
import "user.proto";
message Talk {
int32 id = 1;
string title = 2;
string desc = 3;
User sp...
1. Define message types
2. Generate code
3. Serialize/Deserialize
44 — Type-safe Web APIs with Protocol Buffers in Swift, Y...
protobuf compiler
generates code
from .proto file
45 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kit...
Basic types
Int32, UInt32, Int64, UInt64, Bool, Float, Double, String,
Array, Dictionary, Data
46 — Type-safe Web APIs wit...
Supported languages
C, C++, C#, Erlang, Go, Haskell, Java, Javascript,
Objective-C, Perl, PHP, Python, Ruby, Rust, Scala,
...
Swift features
→ struct, not class
→ enum RawValue is Int
→ Default value is set
48 — Type-safe Web APIs with Protocol Buf...
49 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
user.proto
syntax = "proto3"; // protoc version
message User {
int32 id = 1; // field number
string name = 2;
string intro...
user.pb.swift
// struct
struct User: SwiftProtobuf.Message, ... {
init() {}
enum Type: SwiftProtobuf.Enum {
typealias RawV...
talk.proto
syntax = "proto3";
import "user.proto";
message Talk {
int32 id = 1;
string title = 2;
string desc = 3;
User sp...
talk.pb.swift
struct Talk: SwiftProtobuf.Message, ... {
init() {}
var id: Int32 = 0
var title: String = ""
var speaker: Us...
54 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
User.java
public static final class User extends Message<User, User.Builder> {
public final Integer id;
public final Strin...
Talk.java
public final class Talk extends Message<Talk, Talk.Builder> {
public final Integer id;
public final String title...
57 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
user.pb.go
type User_Type int32
const (
User_Speaker User_ParticipantType = 0
User_Attendee User_ParticipantType = 1
)
typ...
talk.pb.go
type Talk struct {
ID int32 ˆprotobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"ˆ
Title string ˆprotobuf...
One message type
⬇
Multiple languages code
60 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke)...
Less communication,
More collaboration
with other platforms
61 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke...
1. Define message types
2. Generate source files
3. Serialize/Deserialize
62 — Type-safe Web APIs with Protocol Buffers in S...
Serialization
public func serializedData(partial: Bool = default) throws -> Data
public func jsonString() throws -> String...
let user = User.with {
$0.id = 1
$0.type = .speaker
$0.name = "kitasuke"
}
let talk = Talk.with {
$0.id = 1
$0.title = "Ty...
Deserialization
public convenience init(serializedData data: Data,
extensions: SwiftProtobuf.ExtensionSet? = default,
part...
let talk = try? Talk(serializedData: data)
let title = talk?.title
let speaker = talk?.speaker
let tags = talk?.tags
66 — ...
How serialization works
67 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Binary
Encoding
68 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Key factor
1. Field number
2. Wire type
69 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), i...
Field number
message Talk {
int32 id = 1; ← // Field number
string title = 2;
string desc = 3;
User speaker = 4;
repeated ...
Wire type
Type Meaning Used For
0 Varint int32, int64, uint32, uint64,
sint32, sint64, bool, enum
1 64-bit fixed64, sfixed64...
// message type
message Test1 {
int32 a = 1;
}
test1.a = 300
// encoded message
08 96 01
08 // field number and wire type
...
So small and numeric
73 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
High network
performance
74 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Demo
kitasuke/SwiftProtobufSample
75 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon ...
Quick Recap
76 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Quick Recap
→ Type-safety
77 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Quick Recap
→ Type-safety
→ Shared data model
78 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasu...
Quick Recap
→ Type-safety
→ Shared data model
→ High performance
79 — Type-safe Web APIs with Protocol Buffers in Swift, Y...
You might have concerns
about...
80 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2...
Versioning
81 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Backward compatibility
→ Unknown field is ignored
→ Default value for missing field
82 — Type-safe Web APIs with Protocol Bu...
Backward compatibility
→ Unknown field is ignored
→ Default value for missing field
as long as you don't change existing fiel...
Coexistence of
protobuf & JSON
84 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Absolutely you can!
85 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Accept & Content-Type
→ application/protobuf - protobuf
→ application/json - JSON
86 — Type-safe Web APIs with Protocol Bu...
HTTP Request
var request = URLRequest(url: url)
if protobuf {
request.setValue("application/protobuf", forHTTPHeaderField:...
HTTP Response
URLSession.shared.dataTask(with: request) { (data, urlResponse, _) in
let httpURLResponse = urlResponse as? ...
Sounds good
so far !
89 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
So, what are
Cons?
90 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Not human-readable
→ Binary data is not understandable
91 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita...
Time investment
→ Time consuming at the beginning
→ Involvement from other platforms
92 — Type-safe Web APIs with Protocol...
Swift version
→ Watch Swift version of protobuf plugin
→ Specify tag version if you use older version
93 — Type-safe Web A...
Stability
→ Still pre-release version only for Swift
→ Contribute if you find a bug !
94 — Type-safe Web APIs with Protocol...
Where should
we start?
95 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Internal service
→ Easy to adapt
→ Small start
96 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitas...
Data store
→ Database
→ NSKeyedArchiver
→ NSCache
97 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@ki...
Parsing library
→ JSON parser
→ Server side Swift
98 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@ki...
Conclusion
99 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Conclusion
→ Swifty
100 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Conclusion
→ Swifty
→ Consistent in Cross-platform
101 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@...
Conclusion
→ Swifty
→ Consistent in Cross-platform
→ Extensible
102 — Type-safe Web APIs with Protocol Buffers in Swift, Y...
It's definitely
worth it !
103 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
Partially in Production
70M Downloads
!"#
104 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke)...
Credits
Protocol Buffers
swift-protobuf
Kitura
Protocol Buffers in your Kitura Apps
105 — Type-safe Web APIs with Protocol...
Thank you!
GitHub: https://github.com/kitasuke
Demo: SwiftProtobufSample
Twitter: @kitasuke
Email: yusuke2759@gmail.com
10...
Upcoming SlideShare
Loading in …5
×

Type-safe Web APIs with Protocol Buffers in Swift at iOSCon

2,713 views

Published on

iOSCon 2017 in London
See also here. It's updated version.
https://www.slideshare.net/kitasuke/typesafe-web-apis-with-protocol-buffers-in-swift-at-altconf

Published in: Technology
  • Be the first to comment

Type-safe Web APIs with Protocol Buffers in Swift at iOSCon

  1. 1. Type-safe Web APIs with Protocol Buffers in Swift 1 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  2. 2. Hi, I'm Yusuke @kitasuke 2 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  3. 3. I'm going to talk about... → What Protocol Buffers are 3 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  4. 4. I'm going to talk about... → What Protocol Buffers are → Pros and Cons 4 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  5. 5. I'm going to talk about... → What Protocol Buffers are → Pros and Cons → Where to start 5 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  6. 6. Web APIs 6 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  7. 7. JSON7 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  8. 8. HTTP Request JSON ➡ Data 8 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  9. 9. let json = ["id": 1] let data = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) var request = URLRequest(url: url) request.httpBody = data 9 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  10. 10. HTTP Response Data ➡ JSON 10 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  11. 11. // {"user": {"name": "Yusuke"}} URLSession.shared.dataTask(with: request) { (data, _, _) in let json = (try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)) as? [AnyHashable: Any] let user = json?["user"] as? [AnyHashable: Any] let name = user?["name"] as? String } 11 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  12. 12. {"isTyped": false, "status": "disappointed"} 12 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  13. 13. [AnyHashable: Any] 13 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  14. 14. Any? 14 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  15. 15. ! 15 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  16. 16. Protocol Buffersa.k.a protobuf 16 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  17. 17. Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. — Google 17 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  18. 18. Serialization format - think JSON, but smaller, faster and safer — Yusuke 18 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  19. 19. Serialization Format 19 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  20. 20. Why are Protocol Buffers a big deal? 20 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  21. 21. HTTP Request JSON ➡ Data 21 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  22. 22. let json = ["id": 1] let data = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) var request = URLRequest(url: url) request.httpBody = data 22 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  23. 23. HTTP Request JSON ➡ Data 23 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  24. 24. HTTP Request UserRequest ➡ Data 24 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  25. 25. // custom type let userRequest = UserRequest.with { $0.id = 1 } let body = try? body.serializedData() var request = URLRequest(url: url) request.httpBody = body 25 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  26. 26. HTTP Response Data ➡ JSON 26 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  27. 27. // {"user": {"name": "Yusuke"}} URLSession.shared.dataTask(with: request) { (data, _, _) in let json = (try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)) as? [AnyHashable: Any] let user = json?["user"] as? [AnyHashable: Any] let name = user?["name"] as? String } 27 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  28. 28. HTTP Response Data ➡ JSON 28 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  29. 29. HTTP Response Data ➡ UserResponse 29 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  30. 30. // UserResponse(user: User(name: "Yusuke")) URLSession.shared.dataTask(with: request) { (data, _, _) in // custom type guard let response: UserResponse = try? UserResponse(serializedData: data!) else { // error return } let user = response.user let name = user.name } 30 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  31. 31. Protobuf( isTyped: true, status: .satisfied ) 31 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  32. 32. ! 32 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  33. 33. But how do they work? 33 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  34. 34. protobuf for Swift apple/swift-protobuf 34 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  35. 35. "But we still use Objective-C !" 35 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  36. 36. protobuf google/protobuf 36 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  37. 37. 1. Define message types 2. Generate code 3. Serialize/Deserialize 37 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  38. 38. 1. Define message types 2. Generate code 3. Serialize/Deserialize 38 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  39. 39. Message types define data structures in .proto files 39 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  40. 40. Message types have key- value pairs 40 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  41. 41. 41 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  42. 42. user.proto syntax = "proto3"; // protoc version message User { int32 id = 1; // field number string name = 2; string introduction = 3; string photoUrl = 4; ParticipantType participantType = 5; enum ParticipantType { Speaker = 0; Attendee = 1; } } 42 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  43. 43. talk.proto syntax = "proto3"; import "user.proto"; message Talk { int32 id = 1; string title = 2; string desc = 3; User speaker = 4; repeated string tags = 5; // Array } 43 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  44. 44. 1. Define message types 2. Generate code 3. Serialize/Deserialize 44 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  45. 45. protobuf compiler generates code from .proto file 45 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  46. 46. Basic types Int32, UInt32, Int64, UInt64, Bool, Float, Double, String, Array, Dictionary, Data 46 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  47. 47. Supported languages C, C++, C#, Erlang, Go, Haskell, Java, Javascript, Objective-C, Perl, PHP, Python, Ruby, Rust, Scala, Swift etc. 47 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  48. 48. Swift features → struct, not class → enum RawValue is Int → Default value is set 48 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  49. 49. 49 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  50. 50. user.proto syntax = "proto3"; // protoc version message User { int32 id = 1; // field number string name = 2; string introduction = 3; string photoUrl = 4; ParticipantType participantType = 5; enum ParticipantType { Speaker = 0; Attendee = 1; } } 50 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  51. 51. user.pb.swift // struct struct User: SwiftProtobuf.Message, ... { init() {} enum Type: SwiftProtobuf.Enum { typealias RawValue = Int // always Int case speaker // = 0 case participant // = 1 case UNRECOGNIZED(Int) } // property has default value var id: Int32 = 0 var name: String = "" var introduction: String = "" var photoURL: String = "" var participantType: User.ParticipantType = .speaker } 51 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  52. 52. talk.proto syntax = "proto3"; import "user.proto"; message Talk { int32 id = 1; string title = 2; string desc = 3; User speaker = 4; repeated string tags = 5; // Array } 52 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  53. 53. talk.pb.swift struct Talk: SwiftProtobuf.Message, ... { init() {} var id: Int32 = 0 var title: String = "" var speaker: User = User() var desc: String = "" var tags: [String] = [] } 53 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  54. 54. 54 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  55. 55. User.java public static final class User extends Message<User, User.Builder> { public final Integer id; public final String name; public final String introduction; public final String photoUrl; public final ParticipantType participantType; } 55 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  56. 56. Talk.java public final class Talk extends Message<Talk, Talk.Builder> { public final Integer id; public final String title; public final User speaker; public final String summary; public final List<String> tags; } 56 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  57. 57. 57 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  58. 58. user.pb.go type User_Type int32 const ( User_Speaker User_ParticipantType = 0 User_Attendee User_ParticipantType = 1 ) type User struct { ID int32 ˆprotobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"ˆ Name string ˆprotobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"ˆ Introduction string ˆprotobuf:"bytes,3,opt,name=introduction,proto3" json:"introduction,omitempty"ˆ PhotoURL string ˆprotobuf:"bytes,4,opt,name=photoUrl,proto3" json:"photoUrl,omitempty"ˆ Type User_Type ˆprotobuf:"varint,5,opt,name=type,proto3,enum=api.User_Type" json:"type,omitempty"ˆ } 58 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  59. 59. talk.pb.go type Talk struct { ID int32 ˆprotobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"ˆ Title string ˆprotobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"ˆ Speaker *User ˆprotobuf:"bytes,3,opt,name=speaker" json:"speaker,omitempty"ˆ Summary string ˆprotobuf:"bytes,4,opt,name=summary,proto3" json:"summary,omitempty"ˆ Tags []string ˆprotobuf:"bytes,5,rep,name=tags" json:"tags,omitempty"ˆ } 59 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  60. 60. One message type ⬇ Multiple languages code 60 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  61. 61. Less communication, More collaboration with other platforms 61 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  62. 62. 1. Define message types 2. Generate source files 3. Serialize/Deserialize 62 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  63. 63. Serialization public func serializedData(partial: Bool = default) throws -> Data public func jsonString() throws -> String public func textFormatString() throws -> String 63 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  64. 64. let user = User.with { $0.id = 1 $0.type = .speaker $0.name = "kitasuke" } let talk = Talk.with { $0.id = 1 $0.title = "Type-safe Web APIs with Protocol Buffers in Swift" $0.speaker = user $0.tags = ["swift", "iOS", "protocol-buffers", "ioscon", "type-safe"] } let data = try? talk.serializedData() 64 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  65. 65. Deserialization public convenience init(serializedData data: Data, extensions: SwiftProtobuf.ExtensionSet? = default, partial: Bool = default) throws public convenience init(jsonString: String) throws public convenience init(textFormatString: String, extensions: SwiftProtobuf.ExtensionSet? = default) throws 65 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  66. 66. let talk = try? Talk(serializedData: data) let title = talk?.title let speaker = talk?.speaker let tags = talk?.tags 66 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  67. 67. How serialization works 67 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  68. 68. Binary Encoding 68 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  69. 69. Key factor 1. Field number 2. Wire type 69 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  70. 70. Field number message Talk { int32 id = 1; ← // Field number string title = 2; string desc = 3; User speaker = 4; repeated string tags = 5; } 70 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  71. 71. Wire type Type Meaning Used For 0 Varint int32, int64, uint32, uint64, sint32, sint64, bool, enum 1 64-bit fixed64, sfixed64, double 2 Length-delimited string, bytes, embedded messages, packed repeated fields 3 (deprecated) 4 (deprecated) 5 32-bit fixed32, sfix3d32, float 71 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  72. 72. // message type message Test1 { int32 a = 1; } test1.a = 300 // encoded message 08 96 01 08 // field number and wire type 96 01 // value which is 300 72 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  73. 73. So small and numeric 73 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  74. 74. High network performance 74 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  75. 75. Demo kitasuke/SwiftProtobufSample 75 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  76. 76. Quick Recap 76 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  77. 77. Quick Recap → Type-safety 77 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  78. 78. Quick Recap → Type-safety → Shared data model 78 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  79. 79. Quick Recap → Type-safety → Shared data model → High performance 79 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  80. 80. You might have concerns about... 80 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  81. 81. Versioning 81 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  82. 82. Backward compatibility → Unknown field is ignored → Default value for missing field 82 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  83. 83. Backward compatibility → Unknown field is ignored → Default value for missing field as long as you don't change existing field number or wire type 83 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  84. 84. Coexistence of protobuf & JSON 84 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  85. 85. Absolutely you can! 85 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  86. 86. Accept & Content-Type → application/protobuf - protobuf → application/json - JSON 86 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  87. 87. HTTP Request var request = URLRequest(url: url) if protobuf { request.setValue("application/protobuf", forHTTPHeaderField: "Content-Type") request.setValue("application/protobuf", forHTTPHeaderField: "Accept") } else if json { request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.setValue("application/json", forHTTPHeaderField: "Accept") } 87 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  88. 88. HTTP Response URLSession.shared.dataTask(with: request) { (data, urlResponse, _) in let httpURLResponse = urlResponse as? HTTPURLResponse let contentType = httpURLResponse?.allHeaderFields["Content-Type"] as? String if contentType == "application/protobuf" { let response = try? Response(serializedData: data!) } else if contentType == "application/json" { let response = try? Response(jsonString: data!) } } 88 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  89. 89. Sounds good so far ! 89 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  90. 90. So, what are Cons? 90 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  91. 91. Not human-readable → Binary data is not understandable 91 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  92. 92. Time investment → Time consuming at the beginning → Involvement from other platforms 92 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  93. 93. Swift version → Watch Swift version of protobuf plugin → Specify tag version if you use older version 93 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  94. 94. Stability → Still pre-release version only for Swift → Contribute if you find a bug ! 94 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  95. 95. Where should we start? 95 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  96. 96. Internal service → Easy to adapt → Small start 96 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  97. 97. Data store → Database → NSKeyedArchiver → NSCache 97 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  98. 98. Parsing library → JSON parser → Server side Swift 98 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  99. 99. Conclusion 99 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  100. 100. Conclusion → Swifty 100 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  101. 101. Conclusion → Swifty → Consistent in Cross-platform 101 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  102. 102. Conclusion → Swifty → Consistent in Cross-platform → Extensible 102 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  103. 103. It's definitely worth it ! 103 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  104. 104. Partially in Production 70M Downloads !"# 104 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  105. 105. Credits Protocol Buffers swift-protobuf Kitura Protocol Buffers in your Kitura Apps 105 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017
  106. 106. Thank you! GitHub: https://github.com/kitasuke Demo: SwiftProtobufSample Twitter: @kitasuke Email: yusuke2759@gmail.com 106 — Type-safe Web APIs with Protocol Buffers in Swift, Yusuke Kita (@kitasuke), iOSCon 2017

×