SlideShare a Scribd company logo
Introducing protobuf in Swift
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 1
Hi, I'm Yusuke
@kitasuke
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 2
Protocol
Buffersa.k.a protobuf
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 3
Protocol buffers are Google's language-neutral, platform-neutral,
extensible mechanism for serializing structured data – think XML,
but smaller, faster, and simpler.
— Google
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 4
Serialization format
- think JSON, but
smaller, faster and safer
— Yusuke
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 5
Serialization Format
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 6
protobuf
google/protobuf
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 7
protobuf for Swift
apple/swift-protobuf
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 8
"But we still use Objective-C !"
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 9
1. Define message types
2. Generate code
3. Serialize/Deserialize
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 10
1. Define message types
2. Generate code
3. Serialize/Deserialize
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 11
Message types define data structures
in .proto files
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 12
Message types have key-value pairs
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 13
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 14
user.proto
syntax = "proto3"; // protoc version
message User {
int32 id = 1; // field number
string name = 2;
string introduction = 3;
string photoUrl = 4;
Type type = 5;
enum Type {
Speaker = 0;
Attendee = 1;
}
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 15
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
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 16
1. Define message types
2. Generate code
3. Serialize/Deserialize
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 17
protobuf compiler generates code
from .proto file
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 18
Basic types
Int32, UInt32, Int64, UInt64, Bool, Float, Double, String,
Array, Dictionary, Data
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 19
Supported languages
C, C++, C#, Go, Haskell, Java, Javascript, Objective-C, PHP, Python,
Ruby, Rust, Scala, Swift etc.
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 20
Swift features
• struct, not class
• enum RawValue is Int
• Default value is set
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 21
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 22
user.proto
syntax = "proto3"; // protoc version
message User {
int32 id = 1; // field number
string name = 2;
string introduction = 3;
string photoUrl = 4;
Type type = 5;
enum Type {
Speaker = 0;
Attendee = 1;
}
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 23
user.pb.swift
// struct
struct User: SwiftProtobuf.Message, ... {
init() {}
enum Type: SwiftProtobuf.Enum {
typealias RawValue = Int // always Int
case speaker // = 0
case attendee // = 1
case UNRECOGNIZED(Int)
}
// property has default value
var id: Int32 = 0
var name: String = String()
var introduction: String = String()
var photoURL: String = String()
var type: User.TypeEnum = .speaker
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 24
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
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 25
talk.pb.swift
struct Talk: SwiftProtobuf.Message, ... {
init() {}
var id: Int32 = 0
var title: String = String()
var speaker: User = User()
var desc: String = String()
var tags: [String] = []
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 26
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 27
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 Type type;
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 28
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;
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 29
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 30
user.pb.go
type User_Type int32
const (
User_Speaker User_Type = 0
User_Attendee User_Type = 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"`
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 31
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"`
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 32
One message type
➡
Multiple languages code
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 33
Less communication, More collaboration
with other platforms
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 34
1. Define message types
2. Generate source files
3. Serialize/Deserialize
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 35
Serialization
public func serializedData(partial: Bool = default) throws -> Data
public func jsonString() throws -> String
public func textFormatString() throws -> String
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 36
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", "altconf", "type-safe"]
}
let data = try? talk.serializedData()
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 37
Deserialization
public convenience init(serializedData data: Data,
extensions: ExtensionMap = default,
partial: Bool = default) throws
public convenience init(jsonString: String) throws
public convenience init(jsonUTF8Data: Data) throws
public convenience init(textFormatString: String,
extensions: ExtensionMap? = default) throws
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 38
let talk = try? Talk(serializedData: data)
let title = talk?.title
let speaker = talk?.speaker
let tags = talk?.tags
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 39
How serialization works
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 40
Binary Encoding
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 41
Key factor
1. Field number
2. Wire type
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 42
Field number
message Talk {
int32 id = 1; ← // Field number
string title = 2;
string desc = 3;
User speaker = 4;
repeated string tags = 5;
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 43
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
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 44
// 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
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 45
Small and Numeric
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 46
High network performance
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 47
Quick Recap
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 48
Quick Recap
• Type-safety
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 49
Quick Recap
• Type-safety
• Shared data model
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 50
Quick Recap
• Type-safety
• Shared data model
• High performance
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 51
You might have concerns about...
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 52
Versioning
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 53
Backward compatibility
• Unknown field is ignored
• Default value for missing field
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 54
Backward compatibility
• Unknown field is ignored
• Default value for missing field
as long as you don't change existing field number or wire type
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 55
Coexistence of
protobuf & JSON
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 56
Absolutely you can!
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 57
Accept & Content-Type
• application/protobuf - protobuf
• application/json - JSON
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 58
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")
}
request.body = try? userRequest.serializedData()
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 59
HTTP Response
URLSession.shared.dataTask(with: request) { (data, urlResponse, _) in
let httpURLResponse = urlResponse as? HTTPURLResponse
let contentType = httpURLResponse?.allHeaderFields["Content-Type"] as? String
let response: Response?
if contentType == "application/protobuf" {
response = try? Response(serializedData: data!)
} else if contentType == "application/json" {
response = try? Response(jsonUTF8Data: data!)
}
}
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 60
Sounds good
so far !
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 61
So, what are Cons?
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 62
Not human-readable
• Binary data is not understandable
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 63
Time investment
• Time consuming at the beginning
• Involvement from other platforms
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 64
Swift version
• Watch Swift version of protobuf plugin
• Specify tag version if you use older version
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 65
Stability
• Still pre-release version only for Swift
• Contribute if you find a bug !
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 66
Conclusion
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 67
Conclusion
• Swifty
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 68
Conclusion
• Swifty
• Consistent in Cross-platform
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 69
Conclusion
• Swifty
• Consistent in Cross-platform
• Better performance
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 70
It's definitely worth it !
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 71
One more thing...
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 72
Codable !
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 73
struct UserRequest: Codable {
let id: Int
}
let encoder = JSONEncoder()
let userRequest = UserRequest(id: 1)
let data = try? encoder.encode(userRequest)
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 74
struct UserResponse: Codable {
let user: User
struct User: Codable {
let name: String
}
}
let decoder = JSONDecoder()
let response = try? decoder.decode(UserResponse.self, from: data)
let name = response?.user.name
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 75
Codable(
isTyped: true,
status: .excited
)
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 76
Awesome !
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 77
Credits
Protocol Buffers
swift-protobuf
Kitura
Protocol Buffers in your Kitura Apps
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 78
Thank you!
GitHub: SwiftProtobufSample
Video: Realm Academy(en)
Video: Realm Academy(jp)
Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 79

More Related Content

PDF
Type-safe Web APIs with Protocol Buffers in Swift at iOSCon
PDF
Type-safe Web APIs with Protocol Buffers in Swift at AltConf
PDF
Introduction to python programming
PDF
Introduction to python
PPTX
IHTM Python PCEP Introduction to Python
PDF
Pycon2011 android programming-using_python
PDF
Introduction to Python
PDF
How we realized SOA by Python at PyCon JP 2015
Type-safe Web APIs with Protocol Buffers in Swift at iOSCon
Type-safe Web APIs with Protocol Buffers in Swift at AltConf
Introduction to python programming
Introduction to python
IHTM Python PCEP Introduction to Python
Pycon2011 android programming-using_python
Introduction to Python
How we realized SOA by Python at PyCon JP 2015

What's hot (20)

ODP
Python for Android
PPTX
Introduction to python programming, Why Python?, Applications of Python
PPTX
First python project
PPTX
Introduction to Python - Code Heroku
PDF
Pythonistaで始めるiOSプロトタイプ開発
PPT
Python Introduction
PPT
Python Programming ppt
PPTX
Type Annotations in Python: Whats, Whys and Wows!
PPTX
Introduction to python
PDF
Go for Mobile Games
PDF
Python for the Mobile and Web
PPTX
Easy contributable internationalization process with Sphinx (PyCon APAC 2015 ...
PDF
Python in real world.
PDF
用 Go 語言實戰 Push Notification 服務
PDF
Python course syllabus
PDF
Lets learn Python !
PPTX
Introduction to python
PPTX
Benefits & features of python |Advantages & disadvantages of python
PDF
Learn Python Programming | Python Programming - Step by Step | Python for Beg...
Python for Android
Introduction to python programming, Why Python?, Applications of Python
First python project
Introduction to Python - Code Heroku
Pythonistaで始めるiOSプロトタイプ開発
Python Introduction
Python Programming ppt
Type Annotations in Python: Whats, Whys and Wows!
Introduction to python
Go for Mobile Games
Python for the Mobile and Web
Easy contributable internationalization process with Sphinx (PyCon APAC 2015 ...
Python in real world.
用 Go 語言實戰 Push Notification 服務
Python course syllabus
Lets learn Python !
Introduction to python
Benefits & features of python |Advantages & disadvantages of python
Learn Python Programming | Python Programming - Step by Step | Python for Beg...
Ad

Similar to Introducing protobuf in Swift (20)

PDF
Socket programming-in-python
PDF
Socket Programming In Python
PDF
Возможности интерпретатора Python в NX-OS
PDF
Swift 2.2 Design Patterns CocoaConf Austin 2016
PPTX
Shivam PPT.pptx
PPTX
Government Polytechnic Arvi-1.pptx
PPTX
python programming.pptx
PPTX
The Onward Journey: Porting Twisted to Python 3
PDF
Open Source Swift Under the Hood
PDF
Build Great Networked APIs with Swift, OpenAPI, and gRPC
PDF
Scikit-learn : Machine Learning in Python
PPTX
Breizhcamp: Créer un bot, pas si simple. Faisons le point.
PDF
API en Protobuf : 3 fois mieux que le JSON par Pascal CORPET
PPTX
Python-Yesterday Today Tomorrow(What's new?)
PDF
Build advanced chat bots - Steve Sfartz - Codemotion Amsterdam 2017
PDF
Report om 3
PPTX
Python final presentation kirti ppt1
PDF
report on internshala python training
PDF
TypeScript 101 - We RISE Tech Conference
PDF
Swift for tensorflow
Socket programming-in-python
Socket Programming In Python
Возможности интерпретатора Python в NX-OS
Swift 2.2 Design Patterns CocoaConf Austin 2016
Shivam PPT.pptx
Government Polytechnic Arvi-1.pptx
python programming.pptx
The Onward Journey: Porting Twisted to Python 3
Open Source Swift Under the Hood
Build Great Networked APIs with Swift, OpenAPI, and gRPC
Scikit-learn : Machine Learning in Python
Breizhcamp: Créer un bot, pas si simple. Faisons le point.
API en Protobuf : 3 fois mieux que le JSON par Pascal CORPET
Python-Yesterday Today Tomorrow(What's new?)
Build advanced chat bots - Steve Sfartz - Codemotion Amsterdam 2017
Report om 3
Python final presentation kirti ppt1
report on internshala python training
TypeScript 101 - We RISE Tech Conference
Swift for tensorflow
Ad

More from Yusuke Kita (20)

PDF
Integrating libSyntax into the compiler pipeline
PDF
Making your own tool using SwiftSyntax
PDF
[Deprecated] Integrating libSyntax into the compiler pipeline
PDF
Creating your own Bitrise step
PDF
Introducing swift-format
PDF
Unidirectional Data Flow Through SwiftUI
PDF
Open Source Swift Workshop
PDF
Contributing to Swift Compiler
PDF
Writing a compiler in go
PDF
Writing an interpreter in swift
PDF
SIL Optimizations - AllocBoxToStack
PDF
SIL for First Time Learners
PDF
var, let in SIL
PDF
SIL for First Time Leaners LT
PDF
How to try! Swift
PDF
SIL for the first time
PDF
Swift core
PDF
SwiftCoreとFoundationを読んでみた
PDF
Search APIs & Universal Links
PDF
Introducing Cardio
Integrating libSyntax into the compiler pipeline
Making your own tool using SwiftSyntax
[Deprecated] Integrating libSyntax into the compiler pipeline
Creating your own Bitrise step
Introducing swift-format
Unidirectional Data Flow Through SwiftUI
Open Source Swift Workshop
Contributing to Swift Compiler
Writing a compiler in go
Writing an interpreter in swift
SIL Optimizations - AllocBoxToStack
SIL for First Time Learners
var, let in SIL
SIL for First Time Leaners LT
How to try! Swift
SIL for the first time
Swift core
SwiftCoreとFoundationを読んでみた
Search APIs & Universal Links
Introducing Cardio

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
cuic standard and advanced reporting.pdf
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Machine learning based COVID-19 study performance prediction
PDF
Getting Started with Data Integration: FME Form 101
PPT
Teaching material agriculture food technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
1. Introduction to Computer Programming.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
gpt5_lecture_notes_comprehensive_20250812015547.pdf
MYSQL Presentation for SQL database connectivity
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
cuic standard and advanced reporting.pdf
Machine Learning_overview_presentation.pptx
Programs and apps: productivity, graphics, security and other tools
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Assigned Numbers - 2025 - Bluetooth® Document
Machine learning based COVID-19 study performance prediction
Getting Started with Data Integration: FME Form 101
Teaching material agriculture food technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Group 1 Presentation -Planning and Decision Making .pptx
Spectroscopy.pptx food analysis technology
1. Introduction to Computer Programming.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf

Introducing protobuf in Swift

  • 1. Introducing protobuf in Swift Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 1
  • 2. Hi, I'm Yusuke @kitasuke Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 2
  • 3. Protocol Buffersa.k.a protobuf Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 3
  • 4. Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. — Google Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 4
  • 5. Serialization format - think JSON, but smaller, faster and safer — Yusuke Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 5
  • 6. Serialization Format Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 6
  • 7. protobuf google/protobuf Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 7
  • 8. protobuf for Swift apple/swift-protobuf Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 8
  • 9. "But we still use Objective-C !" Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 9
  • 10. 1. Define message types 2. Generate code 3. Serialize/Deserialize Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 10
  • 11. 1. Define message types 2. Generate code 3. Serialize/Deserialize Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 11
  • 12. Message types define data structures in .proto files Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 12
  • 13. Message types have key-value pairs Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 13
  • 14. Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 14
  • 15. user.proto syntax = "proto3"; // protoc version message User { int32 id = 1; // field number string name = 2; string introduction = 3; string photoUrl = 4; Type type = 5; enum Type { Speaker = 0; Attendee = 1; } } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 15
  • 16. 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 } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 16
  • 17. 1. Define message types 2. Generate code 3. Serialize/Deserialize Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 17
  • 18. protobuf compiler generates code from .proto file Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 18
  • 19. Basic types Int32, UInt32, Int64, UInt64, Bool, Float, Double, String, Array, Dictionary, Data Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 19
  • 20. Supported languages C, C++, C#, Go, Haskell, Java, Javascript, Objective-C, PHP, Python, Ruby, Rust, Scala, Swift etc. Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 20
  • 21. Swift features • struct, not class • enum RawValue is Int • Default value is set Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 21
  • 22. Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 22
  • 23. user.proto syntax = "proto3"; // protoc version message User { int32 id = 1; // field number string name = 2; string introduction = 3; string photoUrl = 4; Type type = 5; enum Type { Speaker = 0; Attendee = 1; } } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 23
  • 24. user.pb.swift // struct struct User: SwiftProtobuf.Message, ... { init() {} enum Type: SwiftProtobuf.Enum { typealias RawValue = Int // always Int case speaker // = 0 case attendee // = 1 case UNRECOGNIZED(Int) } // property has default value var id: Int32 = 0 var name: String = String() var introduction: String = String() var photoURL: String = String() var type: User.TypeEnum = .speaker } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 24
  • 25. 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 } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 25
  • 26. talk.pb.swift struct Talk: SwiftProtobuf.Message, ... { init() {} var id: Int32 = 0 var title: String = String() var speaker: User = User() var desc: String = String() var tags: [String] = [] } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 26
  • 27. Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 27
  • 28. 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 Type type; } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 28
  • 29. 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; } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 29
  • 30. Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 30
  • 31. user.pb.go type User_Type int32 const ( User_Speaker User_Type = 0 User_Attendee User_Type = 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"` } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 31
  • 32. 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"` } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 32
  • 33. One message type ➡ Multiple languages code Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 33
  • 34. Less communication, More collaboration with other platforms Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 34
  • 35. 1. Define message types 2. Generate source files 3. Serialize/Deserialize Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 35
  • 36. Serialization public func serializedData(partial: Bool = default) throws -> Data public func jsonString() throws -> String public func textFormatString() throws -> String Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 36
  • 37. 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", "altconf", "type-safe"] } let data = try? talk.serializedData() Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 37
  • 38. Deserialization public convenience init(serializedData data: Data, extensions: ExtensionMap = default, partial: Bool = default) throws public convenience init(jsonString: String) throws public convenience init(jsonUTF8Data: Data) throws public convenience init(textFormatString: String, extensions: ExtensionMap? = default) throws Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 38
  • 39. let talk = try? Talk(serializedData: data) let title = talk?.title let speaker = talk?.speaker let tags = talk?.tags Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 39
  • 40. How serialization works Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 40
  • 41. Binary Encoding Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 41
  • 42. Key factor 1. Field number 2. Wire type Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 42
  • 43. Field number message Talk { int32 id = 1; ← // Field number string title = 2; string desc = 3; User speaker = 4; repeated string tags = 5; } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 43
  • 44. 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 Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 44
  • 45. // 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 Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 45
  • 46. Small and Numeric Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 46
  • 47. High network performance Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 47
  • 48. Quick Recap Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 48
  • 49. Quick Recap • Type-safety Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 49
  • 50. Quick Recap • Type-safety • Shared data model Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 50
  • 51. Quick Recap • Type-safety • Shared data model • High performance Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 51
  • 52. You might have concerns about... Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 52
  • 53. Versioning Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 53
  • 54. Backward compatibility • Unknown field is ignored • Default value for missing field Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 54
  • 55. Backward compatibility • Unknown field is ignored • Default value for missing field as long as you don't change existing field number or wire type Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 55
  • 56. Coexistence of protobuf & JSON Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 56
  • 57. Absolutely you can! Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 57
  • 58. Accept & Content-Type • application/protobuf - protobuf • application/json - JSON Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 58
  • 59. 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") } request.body = try? userRequest.serializedData() Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 59
  • 60. HTTP Response URLSession.shared.dataTask(with: request) { (data, urlResponse, _) in let httpURLResponse = urlResponse as? HTTPURLResponse let contentType = httpURLResponse?.allHeaderFields["Content-Type"] as? String let response: Response? if contentType == "application/protobuf" { response = try? Response(serializedData: data!) } else if contentType == "application/json" { response = try? Response(jsonUTF8Data: data!) } } Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 60
  • 61. Sounds good so far ! Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 61
  • 62. So, what are Cons? Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 62
  • 63. Not human-readable • Binary data is not understandable Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 63
  • 64. Time investment • Time consuming at the beginning • Involvement from other platforms Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 64
  • 65. Swift version • Watch Swift version of protobuf plugin • Specify tag version if you use older version Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 65
  • 66. Stability • Still pre-release version only for Swift • Contribute if you find a bug ! Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 66
  • 67. Conclusion Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 67
  • 68. Conclusion • Swifty Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 68
  • 69. Conclusion • Swifty • Consistent in Cross-platform Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 69
  • 70. Conclusion • Swifty • Consistent in Cross-platform • Better performance Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 70
  • 71. It's definitely worth it ! Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 71
  • 72. One more thing... Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 72
  • 73. Codable ! Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 73
  • 74. struct UserRequest: Codable { let id: Int } let encoder = JSONEncoder() let userRequest = UserRequest(id: 1) let data = try? encoder.encode(userRequest) Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 74
  • 75. struct UserResponse: Codable { let user: User struct User: Codable { let name: String } } let decoder = JSONDecoder() let response = try? decoder.decode(UserResponse.self, from: data) let name = response?.user.name Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 75
  • 76. Codable( isTyped: true, status: .excited ) Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 76
  • 77. Awesome ! Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 77
  • 78. Credits Protocol Buffers swift-protobuf Kitura Protocol Buffers in your Kitura Apps Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 78
  • 79. Thank you! GitHub: SwiftProtobufSample Video: Realm Academy(en) Video: Realm Academy(jp) Introducing protobuf in Swift, Yusuke Kita (@kitasuke), iOSDC 2017 79