SlideShare una empresa de Scribd logo
1 de 79
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Python for Android
Python for AndroidPython for Android
Python for Androidphlax
 
Introduction to python programming, Why Python?, Applications of Python
Introduction to python programming, Why Python?, Applications of PythonIntroduction to python programming, Why Python?, Applications of Python
Introduction to python programming, Why Python?, Applications of PythonPro Guide
 
First python project
First python projectFirst python project
First python projectNeetu Jain
 
Introduction to Python - Code Heroku
Introduction to Python - Code HerokuIntroduction to Python - Code Heroku
Introduction to Python - Code Herokucodeheroku
 
Pythonistaで始めるiOSプロトタイプ開発
Pythonistaで始めるiOSプロトタイプ開発Pythonistaで始めるiOSプロトタイプ開発
Pythonistaで始めるiOSプロトタイプ開発Yusuke Muraoka
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming pptismailmrribi
 
Type Annotations in Python: Whats, Whys and Wows!
Type Annotations in Python: Whats, Whys and Wows!Type Annotations in Python: Whats, Whys and Wows!
Type Annotations in Python: Whats, Whys and Wows!Andreas Dewes
 
Go for Mobile Games
Go for Mobile GamesGo for Mobile Games
Go for Mobile GamesTakuya Ueda
 
Python for the Mobile and Web
Python for the Mobile and WebPython for the Mobile and Web
Python for the Mobile and WebDerek Kiong
 
Easy contributable internationalization process with Sphinx (PyCon APAC 2015 ...
Easy contributable internationalization process with Sphinx (PyCon APAC 2015 ...Easy contributable internationalization process with Sphinx (PyCon APAC 2015 ...
Easy contributable internationalization process with Sphinx (PyCon APAC 2015 ...Takayuki Shimizukawa
 
Python in real world.
Python in real world.Python in real world.
Python in real world.Alph@.M
 
用 Go 語言實戰 Push Notification 服務
用 Go 語言實戰 Push Notification 服務用 Go 語言實戰 Push Notification 服務
用 Go 語言實戰 Push Notification 服務Bo-Yi Wu
 
Python course syllabus
Python course syllabusPython course syllabus
Python course syllabusSugantha T
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonManishJha237
 
Benefits & features of python |Advantages & disadvantages of python
Benefits & features of python |Advantages & disadvantages of pythonBenefits & features of python |Advantages & disadvantages of python
Benefits & features of python |Advantages & disadvantages of pythonparadisetechsoftsolutions
 
Learn Python Programming | Python Programming - Step by Step | Python for Beg...
Learn Python Programming | Python Programming - Step by Step | Python for Beg...Learn Python Programming | Python Programming - Step by Step | Python for Beg...
Learn Python Programming | Python Programming - Step by Step | Python for Beg...Edureka!
 

La actualidad más candente (20)

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

Similar a Introducing protobuf in Swift

Socket programming-in-python
Socket programming-in-pythonSocket programming-in-python
Socket programming-in-pythonYuvaraja Ravi
 
Socket Programming In Python
Socket Programming In PythonSocket Programming In Python
Socket Programming In Pythondidip
 
Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSCisco Russia
 
Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016Carl Brown
 
Government Polytechnic Arvi-1.pptx
Government Polytechnic Arvi-1.pptxGovernment Polytechnic Arvi-1.pptx
Government Polytechnic Arvi-1.pptxShivamDenge
 
python programming.pptx
python programming.pptxpython programming.pptx
python programming.pptxKaviya452563
 
The Onward Journey: Porting Twisted to Python 3
The Onward Journey: Porting Twisted to Python 3The Onward Journey: Porting Twisted to Python 3
The Onward Journey: Porting Twisted to Python 3Craig Rodrigues
 
Open Source Swift Under the Hood
Open Source Swift Under the HoodOpen Source Swift Under the Hood
Open Source Swift Under the HoodC4Media
 
Build Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCBuild Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCTim Burks
 
Breizhcamp: Créer un bot, pas si simple. Faisons le point.
Breizhcamp: Créer un bot, pas si simple. Faisons le point.Breizhcamp: Créer un bot, pas si simple. Faisons le point.
Breizhcamp: Créer un bot, pas si simple. Faisons le point.Cisco DevNet
 
API en Protobuf : 3 fois mieux que le JSON par Pascal CORPET
API en Protobuf : 3 fois mieux que le JSON par Pascal CORPETAPI en Protobuf : 3 fois mieux que le JSON par Pascal CORPET
API en Protobuf : 3 fois mieux que le JSON par Pascal CORPETLa Cuisine du Web
 
Python-Yesterday Today Tomorrow(What's new?)
Python-Yesterday Today Tomorrow(What's new?)Python-Yesterday Today Tomorrow(What's new?)
Python-Yesterday Today Tomorrow(What's new?)Mohan Arumugam
 
Build advanced chat bots - Steve Sfartz - Codemotion Amsterdam 2017
Build advanced chat bots - Steve Sfartz - Codemotion Amsterdam 2017Build advanced chat bots - Steve Sfartz - Codemotion Amsterdam 2017
Build advanced chat bots - Steve Sfartz - Codemotion Amsterdam 2017Codemotion
 
Python final presentation kirti ppt1
Python final presentation kirti ppt1Python final presentation kirti ppt1
Python final presentation kirti ppt1Kirti Verma
 
report on internshala python training
 report on internshala python  training  report on internshala python  training
report on internshala python training surabhimalviya1
 
TypeScript 101 - We RISE Tech Conference
TypeScript 101 - We RISE Tech ConferenceTypeScript 101 - We RISE Tech Conference
TypeScript 101 - We RISE Tech ConferenceFrances Coronel
 
Rome 2017: Building advanced voice assistants and chat bots
Rome 2017: Building advanced voice assistants and chat botsRome 2017: Building advanced voice assistants and chat bots
Rome 2017: Building advanced voice assistants and chat botsCisco DevNet
 

Similar a Introducing protobuf in Swift (20)

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

Más de Yusuke Kita

Integrating libSyntax into the compiler pipeline
Integrating libSyntax into the compiler pipelineIntegrating libSyntax into the compiler pipeline
Integrating libSyntax into the compiler pipelineYusuke Kita
 
Making your own tool using SwiftSyntax
Making your own tool using SwiftSyntaxMaking your own tool using SwiftSyntax
Making your own tool using SwiftSyntaxYusuke Kita
 
[Deprecated] Integrating libSyntax into the compiler pipeline
[Deprecated] Integrating libSyntax into the compiler pipeline[Deprecated] Integrating libSyntax into the compiler pipeline
[Deprecated] Integrating libSyntax into the compiler pipelineYusuke Kita
 
Creating your own Bitrise step
Creating your own Bitrise stepCreating your own Bitrise step
Creating your own Bitrise stepYusuke Kita
 
Introducing swift-format
Introducing swift-formatIntroducing swift-format
Introducing swift-formatYusuke Kita
 
Unidirectional Data Flow Through SwiftUI
Unidirectional Data Flow Through SwiftUIUnidirectional Data Flow Through SwiftUI
Unidirectional Data Flow Through SwiftUIYusuke Kita
 
Open Source Swift Workshop
Open Source Swift WorkshopOpen Source Swift Workshop
Open Source Swift WorkshopYusuke Kita
 
Contributing to Swift Compiler
Contributing to Swift CompilerContributing to Swift Compiler
Contributing to Swift CompilerYusuke Kita
 
Writing a compiler in go
Writing a compiler in goWriting a compiler in go
Writing a compiler in goYusuke Kita
 
Writing an interpreter in swift
Writing an interpreter in swiftWriting an interpreter in swift
Writing an interpreter in swiftYusuke Kita
 
SIL Optimizations - AllocBoxToStack
SIL Optimizations - AllocBoxToStackSIL Optimizations - AllocBoxToStack
SIL Optimizations - AllocBoxToStackYusuke Kita
 
SIL for First Time Learners
SIL for First Time LearnersSIL for First Time Learners
SIL for First Time LearnersYusuke Kita
 
SIL for First Time Leaners LT
SIL for First Time Leaners LTSIL for First Time Leaners LT
SIL for First Time Leaners LTYusuke Kita
 
How to try! Swift
How to try! SwiftHow to try! Swift
How to try! SwiftYusuke Kita
 
SIL for the first time
SIL for the first timeSIL for the first time
SIL for the first timeYusuke Kita
 
SwiftCoreとFoundationを読んでみた
SwiftCoreとFoundationを読んでみたSwiftCoreとFoundationを読んでみた
SwiftCoreとFoundationを読んでみたYusuke Kita
 
Search APIs & Universal Links
Search APIs & Universal LinksSearch APIs & Universal Links
Search APIs & Universal LinksYusuke Kita
 
Introducing Cardio
Introducing CardioIntroducing Cardio
Introducing CardioYusuke Kita
 

Más de Yusuke Kita (20)

Integrating libSyntax into the compiler pipeline
Integrating libSyntax into the compiler pipelineIntegrating libSyntax into the compiler pipeline
Integrating libSyntax into the compiler pipeline
 
Making your own tool using SwiftSyntax
Making your own tool using SwiftSyntaxMaking your own tool using SwiftSyntax
Making your own tool using SwiftSyntax
 
[Deprecated] Integrating libSyntax into the compiler pipeline
[Deprecated] Integrating libSyntax into the compiler pipeline[Deprecated] Integrating libSyntax into the compiler pipeline
[Deprecated] Integrating libSyntax into the compiler pipeline
 
Creating your own Bitrise step
Creating your own Bitrise stepCreating your own Bitrise step
Creating your own Bitrise step
 
Introducing swift-format
Introducing swift-formatIntroducing swift-format
Introducing swift-format
 
Unidirectional Data Flow Through SwiftUI
Unidirectional Data Flow Through SwiftUIUnidirectional Data Flow Through SwiftUI
Unidirectional Data Flow Through SwiftUI
 
Open Source Swift Workshop
Open Source Swift WorkshopOpen Source Swift Workshop
Open Source Swift Workshop
 
Contributing to Swift Compiler
Contributing to Swift CompilerContributing to Swift Compiler
Contributing to Swift Compiler
 
Writing a compiler in go
Writing a compiler in goWriting a compiler in go
Writing a compiler in go
 
Writing an interpreter in swift
Writing an interpreter in swiftWriting an interpreter in swift
Writing an interpreter in swift
 
SIL Optimizations - AllocBoxToStack
SIL Optimizations - AllocBoxToStackSIL Optimizations - AllocBoxToStack
SIL Optimizations - AllocBoxToStack
 
SIL for First Time Learners
SIL for First Time LearnersSIL for First Time Learners
SIL for First Time Learners
 
var, let in SIL
var, let in SILvar, let in SIL
var, let in SIL
 
SIL for First Time Leaners LT
SIL for First Time Leaners LTSIL for First Time Leaners LT
SIL for First Time Leaners LT
 
How to try! Swift
How to try! SwiftHow to try! Swift
How to try! Swift
 
SIL for the first time
SIL for the first timeSIL for the first time
SIL for the first time
 
Swift core
Swift coreSwift core
Swift core
 
SwiftCoreとFoundationを読んでみた
SwiftCoreとFoundationを読んでみたSwiftCoreとFoundationを読んでみた
SwiftCoreとFoundationを読んでみた
 
Search APIs & Universal Links
Search APIs & Universal LinksSearch APIs & Universal Links
Search APIs & Universal Links
 
Introducing Cardio
Introducing CardioIntroducing Cardio
Introducing Cardio
 

Último

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Último (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

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