SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
Firebase Golang Binding
Eddy Reyes
https://github.com/ereyes01/firebase
Firebase Overview
● Real-time Backend-as-a-Service
● NoSQL style JSON data store
● Security rules language
○ Multi-tenancy
● User management
● REST API
○ https://mydb.firebaseio.com/path/to/data.json?
orderBy=”index”&equalTo=”tommy”
Firebase Shortcomings
● Very limited query capabilities
○ Intended for real-time applications
○ Generally only O(N) queries on one index at a time
● REST API typically lags behind JS API in
features.
● Data must be de-normalized (un-nested)
○ You’re on your own joining it all back together
Firebase Language Bindings
Except for JS, all Firebase language bindings
use the REST API
● GET / PUT / POST / PATCH
○ Read / write data
● Real-time updates via Server-Sent Events
○ Long poll protocol spec, part of HTML5
○ http://en.wikipedia.org/wiki/Server-sent_events
Firebase Go Binding
Mimics the official JS API:
type Client interface {
// path
Child(path string) Client
// read / write
Value(destination interface{}) error
Push(value interface{}, params map[string]string)
(Client, error)
Set(path string, value interface{}, params map[string]
string) (Client, error)
Update(path string, value interface{}, params map
[string]string) error
Remove(path string, params map[string]string) error
}
Example:
var w Widget
err := client.Child(“my/path”).Value(&w)
w.Name = “fred”
c, err := client.Child(“my/path”).Set(w)
c, err := client.Child(“newOne”).Push(w)
// c.Key() has name of new resource
Why Write a New Go Binding?
Options were limited:
● github.com/cosn/firebase
○ Nice approach to designing testable library
○ Incomplete, unmaintained
● github.com/JustinTulloss/firebase
○ Cleans up some of cosn’s implementation
No streaming support, tests were deficient
Firebase Go Binding
https://godoc.org/github.com/ereyes01/firebase
● Idiomatic Go implementations of:
○ Streaming (via channels)
○ Firebase server-side timestamps
■ Custom JSON encoder
○ Your Firebase data as structs (or maps if you prefer)
● Well unit-tested, testable by lib consumers
Testable Go Library
Ideal unit test:
● Tests only YOUR code being changed
● No external dependencies, no talking to the
network
○ How to mock in Go?
Interfaces!
func NewClient(root, auth string, api Api) Client
Testable Go Library
The “Api” interface handles HTTP
communication with Firebase.
● A nil api -> you talk to Firebase
(production)
● Non-nil api- test mode
○ Intercept the communication with your stub / test
implementation
Testable Go Library
type Api interface {
Call(method, path, auth string, body interface{},
params map[string]string, dest interface{}) error
Stream(path, auth string, body interface{}, params map
[string]string, stop <-chan bool) (<-chan RawEvent, error)
}
Firebase Streaming
How it works in HTTP:
● GET request, path to resource + query
● Accept: text/event-stream
● Must follow redirects
● Data:
event: patch
data: {“path”:”right/here”,“data”:{“my”: “data”}}
event: keep-alive
data: null
event: keep-alive
data: null
Firebase Streaming in Go
In Go:
● Model stream as a channel
● Channel of what type?
○ Everything unmarshals to map[string]interface{} as:
■ string / float64 / bool / map / []interface{}
○ But my “data” is my own struct type!
○ I want events via chan <MyType>
■ Type-generic channel, type is a parameter
No Generics? No Problem! (Sort of)
You can do reflection gymnastics:
○ func ParametricType(myType reflect.Type)
○ Call like this:
■ ParametricType(reflect.TypeOf(Widget{}))
■ Meanwhile, in ParametricType():
ptr := reflect.New(myType).Interface()
● Allocates a new Widget object, returns pointer
● Type-generic instantiation
No Generics? No Problem! (Sort of)
That’s a working solution, but…
Exposes reflection in an external API.
Alternative:
● interface stream + unmarshaller callback
● Longer, but perhaps cleaner interface
No Generics? No Problem! (Sort of)
Watch(unmarshaller EventUnmarshaller, stop <-chan bool)
(<-chan StreamEvent, error)
type EventUnmarshaller func(jsonText []byte) (interface{},
error)
type StreamEvent struct {
Event string
Path string
Resource interface{}
RawData string
Error error
}
Now You Can Stream Widgets!
func widgetParser(text []byte) (interface{} error) {
var w Widget
err := json.Unmarshal(text, &w)
return w, err
}
…
events, err := client.Watch(widgetParser, stopChan)
for event := range events {
widget := event.Resource.(Widget)
}
Fork Me on Github!
https://github.com/ereyes01/firebase

Weitere ähnliche Inhalte

Was ist angesagt?

Fluentd Project Intro at Kubecon 2019 EU
Fluentd Project Intro at Kubecon 2019 EUFluentd Project Intro at Kubecon 2019 EU
Fluentd Project Intro at Kubecon 2019 EUN Masahiro
 
Fluentd and Docker - running fluentd within a docker container
Fluentd and Docker - running fluentd within a docker containerFluentd and Docker - running fluentd within a docker container
Fluentd and Docker - running fluentd within a docker containerTreasure Data, Inc.
 
Fluentd and Docker - running fluentd within a docker container
Fluentd and Docker - running fluentd within a docker containerFluentd and Docker - running fluentd within a docker container
Fluentd and Docker - running fluentd within a docker containerTreasure Data, Inc.
 
Log aggregation and analysis
Log aggregation and analysisLog aggregation and analysis
Log aggregation and analysisDhaval Mehta
 
Build Your Own Search Engine
Build Your Own Search EngineBuild Your Own Search Engine
Build Your Own Search Enginegoodfriday
 
Linked Data voor developers - PiLOD congres 25 juni
Linked Data voor developers - PiLOD congres 25 juniLinked Data voor developers - PiLOD congres 25 juni
Linked Data voor developers - PiLOD congres 25 juniDimitri van Hees
 
Fluentd and Distributed Logging at Kubecon
Fluentd and Distributed Logging at KubeconFluentd and Distributed Logging at Kubecon
Fluentd and Distributed Logging at KubeconN Masahiro
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to GroovyKevin H.A. Tan
 
Bringing spatial love to your python application
Bringing spatial love to your python applicationBringing spatial love to your python application
Bringing spatial love to your python applicationShekhar Gulati
 
Collo -01 , en
Collo -01 , enCollo -01 , en
Collo -01 , en지현 이
 
Logging Application Behavior to MongoDB
Logging Application Behavior to MongoDBLogging Application Behavior to MongoDB
Logging Application Behavior to MongoDBRobert Stewart
 
Rapid, Scalable Web Development with MongoDB, Ming, and Python
Rapid, Scalable Web Development with MongoDB, Ming, and PythonRapid, Scalable Web Development with MongoDB, Ming, and Python
Rapid, Scalable Web Development with MongoDB, Ming, and PythonRick Copeland
 
Talend connect BE Vincent Harcq - Talend ESB - DI
Talend connect BE Vincent Harcq - Talend  ESB - DITalend connect BE Vincent Harcq - Talend  ESB - DI
Talend connect BE Vincent Harcq - Talend ESB - DIVincent Harcq
 
Drupal Camp2009 Asp.Net Vs Drupal
Drupal Camp2009 Asp.Net Vs DrupalDrupal Camp2009 Asp.Net Vs Drupal
Drupal Camp2009 Asp.Net Vs DrupalInna Tuyeva
 
COUNTWORDSFREE - WORD COUNTER
COUNTWORDSFREE - WORD COUNTERCOUNTWORDSFREE - WORD COUNTER
COUNTWORDSFREE - WORD COUNTERKirylTravulka
 

Was ist angesagt? (20)

Fluentd Project Intro at Kubecon 2019 EU
Fluentd Project Intro at Kubecon 2019 EUFluentd Project Intro at Kubecon 2019 EU
Fluentd Project Intro at Kubecon 2019 EU
 
Ruby loves DDD
Ruby loves DDDRuby loves DDD
Ruby loves DDD
 
Fluentd and Docker - running fluentd within a docker container
Fluentd and Docker - running fluentd within a docker containerFluentd and Docker - running fluentd within a docker container
Fluentd and Docker - running fluentd within a docker container
 
Fluentd and Docker - running fluentd within a docker container
Fluentd and Docker - running fluentd within a docker containerFluentd and Docker - running fluentd within a docker container
Fluentd and Docker - running fluentd within a docker container
 
Ajax
AjaxAjax
Ajax
 
Log aggregation and analysis
Log aggregation and analysisLog aggregation and analysis
Log aggregation and analysis
 
Build Your Own Search Engine
Build Your Own Search EngineBuild Your Own Search Engine
Build Your Own Search Engine
 
Linked Data voor developers - PiLOD congres 25 juni
Linked Data voor developers - PiLOD congres 25 juniLinked Data voor developers - PiLOD congres 25 juni
Linked Data voor developers - PiLOD congres 25 juni
 
Fluentd and Distributed Logging at Kubecon
Fluentd and Distributed Logging at KubeconFluentd and Distributed Logging at Kubecon
Fluentd and Distributed Logging at Kubecon
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 
Bringing spatial love to your python application
Bringing spatial love to your python applicationBringing spatial love to your python application
Bringing spatial love to your python application
 
Collo -01 , en
Collo -01 , enCollo -01 , en
Collo -01 , en
 
Logging Application Behavior to MongoDB
Logging Application Behavior to MongoDBLogging Application Behavior to MongoDB
Logging Application Behavior to MongoDB
 
Rapid, Scalable Web Development with MongoDB, Ming, and Python
Rapid, Scalable Web Development with MongoDB, Ming, and PythonRapid, Scalable Web Development with MongoDB, Ming, and Python
Rapid, Scalable Web Development with MongoDB, Ming, and Python
 
Talend connect BE Vincent Harcq - Talend ESB - DI
Talend connect BE Vincent Harcq - Talend  ESB - DITalend connect BE Vincent Harcq - Talend  ESB - DI
Talend connect BE Vincent Harcq - Talend ESB - DI
 
Datasnap
DatasnapDatasnap
Datasnap
 
Drupal Camp2009 Asp.Net Vs Drupal
Drupal Camp2009 Asp.Net Vs DrupalDrupal Camp2009 Asp.Net Vs Drupal
Drupal Camp2009 Asp.Net Vs Drupal
 
The CQRS diet
The CQRS dietThe CQRS diet
The CQRS diet
 
COUNTWORDSFREE - WORD COUNTER
COUNTWORDSFREE - WORD COUNTERCOUNTWORDSFREE - WORD COUNTER
COUNTWORDSFREE - WORD COUNTER
 
Open source data ingestion
Open source data ingestionOpen source data ingestion
Open source data ingestion
 

Ähnlich wie Firebase Golang Binding Mimics Official JS API

Design Web Service API by HungerStation
Design Web Service API by HungerStationDesign Web Service API by HungerStation
Design Web Service API by HungerStationArabNet ME
 
Progressive web applications
Progressive web applicationsProgressive web applications
Progressive web applicationsTom Martin
 
A high profile project with Symfony and API Platform: beIN SPORTS
A high profile project with Symfony and API Platform: beIN SPORTSA high profile project with Symfony and API Platform: beIN SPORTS
A high profile project with Symfony and API Platform: beIN SPORTSSmile I.T is open
 
web2py:Web development like a boss
web2py:Web development like a bossweb2py:Web development like a boss
web2py:Web development like a bossFrancisco Ribeiro
 
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...François-Guillaume Ribreau
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST APICaldera Labs
 
Microservices in Scala: Spray
Microservices in Scala: SprayMicroservices in Scala: Spray
Microservices in Scala: SprayŁukasz Sowa
 
Java-Jersey 到 Python-Flask 服務不中斷重構之旅
Java-Jersey 到 Python-Flask 服務不中斷重構之旅Java-Jersey 到 Python-Flask 服務不中斷重構之旅
Java-Jersey 到 Python-Flask 服務不中斷重構之旅Max Lai
 
Python tools for testing web services over HTTP
Python tools for testing web services over HTTPPython tools for testing web services over HTTP
Python tools for testing web services over HTTPMykhailo Kolesnyk
 
Rethinking Syncing at AltConf 2019
Rethinking Syncing at AltConf 2019Rethinking Syncing at AltConf 2019
Rethinking Syncing at AltConf 2019Joe Keeley
 

Ähnlich wie Firebase Golang Binding Mimics Official JS API (20)

Design Web Service API by HungerStation
Design Web Service API by HungerStationDesign Web Service API by HungerStation
Design Web Service API by HungerStation
 
Progressive web applications
Progressive web applicationsProgressive web applications
Progressive web applications
 
A high profile project with Symfony and API Platform: beIN SPORTS
A high profile project with Symfony and API Platform: beIN SPORTSA high profile project with Symfony and API Platform: beIN SPORTS
A high profile project with Symfony and API Platform: beIN SPORTS
 
web2py:Web development like a boss
web2py:Web development like a bossweb2py:Web development like a boss
web2py:Web development like a boss
 
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
 
Gohan
GohanGohan
Gohan
 
JSON API Specificiation
JSON API SpecificiationJSON API Specificiation
JSON API Specificiation
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST API
 
Microservices in Scala: Spray
Microservices in Scala: SprayMicroservices in Scala: Spray
Microservices in Scala: Spray
 
Java-Jersey 到 Python-Flask 服務不中斷重構之旅
Java-Jersey 到 Python-Flask 服務不中斷重構之旅Java-Jersey 到 Python-Flask 服務不中斷重構之旅
Java-Jersey 到 Python-Flask 服務不中斷重構之旅
 
Python tools for testing web services over HTTP
Python tools for testing web services over HTTPPython tools for testing web services over HTTP
Python tools for testing web services over HTTP
 
Rethinking Syncing at AltConf 2019
Rethinking Syncing at AltConf 2019Rethinking Syncing at AltConf 2019
Rethinking Syncing at AltConf 2019
 
gRPC - RPC rebirth?
gRPC - RPC rebirth?gRPC - RPC rebirth?
gRPC - RPC rebirth?
 
Api crash
Api crashApi crash
Api crash
 
Api crash
Api crashApi crash
Api crash
 
Api crash
Api crashApi crash
Api crash
 
Api crash
Api crashApi crash
Api crash
 
Api crash
Api crashApi crash
Api crash
 
Api crash
Api crashApi crash
Api crash
 
Api crash
Api crashApi crash
Api crash
 

Kürzlich hochgeladen

Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 

Kürzlich hochgeladen (20)

Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 

Firebase Golang Binding Mimics Official JS API

  • 1. Firebase Golang Binding Eddy Reyes https://github.com/ereyes01/firebase
  • 2. Firebase Overview ● Real-time Backend-as-a-Service ● NoSQL style JSON data store ● Security rules language ○ Multi-tenancy ● User management ● REST API ○ https://mydb.firebaseio.com/path/to/data.json? orderBy=”index”&equalTo=”tommy”
  • 3. Firebase Shortcomings ● Very limited query capabilities ○ Intended for real-time applications ○ Generally only O(N) queries on one index at a time ● REST API typically lags behind JS API in features. ● Data must be de-normalized (un-nested) ○ You’re on your own joining it all back together
  • 4. Firebase Language Bindings Except for JS, all Firebase language bindings use the REST API ● GET / PUT / POST / PATCH ○ Read / write data ● Real-time updates via Server-Sent Events ○ Long poll protocol spec, part of HTML5 ○ http://en.wikipedia.org/wiki/Server-sent_events
  • 5. Firebase Go Binding Mimics the official JS API: type Client interface { // path Child(path string) Client // read / write Value(destination interface{}) error Push(value interface{}, params map[string]string) (Client, error) Set(path string, value interface{}, params map[string] string) (Client, error) Update(path string, value interface{}, params map [string]string) error Remove(path string, params map[string]string) error } Example: var w Widget err := client.Child(“my/path”).Value(&w) w.Name = “fred” c, err := client.Child(“my/path”).Set(w) c, err := client.Child(“newOne”).Push(w) // c.Key() has name of new resource
  • 6. Why Write a New Go Binding? Options were limited: ● github.com/cosn/firebase ○ Nice approach to designing testable library ○ Incomplete, unmaintained ● github.com/JustinTulloss/firebase ○ Cleans up some of cosn’s implementation No streaming support, tests were deficient
  • 7. Firebase Go Binding https://godoc.org/github.com/ereyes01/firebase ● Idiomatic Go implementations of: ○ Streaming (via channels) ○ Firebase server-side timestamps ■ Custom JSON encoder ○ Your Firebase data as structs (or maps if you prefer) ● Well unit-tested, testable by lib consumers
  • 8. Testable Go Library Ideal unit test: ● Tests only YOUR code being changed ● No external dependencies, no talking to the network ○ How to mock in Go? Interfaces! func NewClient(root, auth string, api Api) Client
  • 9. Testable Go Library The “Api” interface handles HTTP communication with Firebase. ● A nil api -> you talk to Firebase (production) ● Non-nil api- test mode ○ Intercept the communication with your stub / test implementation
  • 10. Testable Go Library type Api interface { Call(method, path, auth string, body interface{}, params map[string]string, dest interface{}) error Stream(path, auth string, body interface{}, params map [string]string, stop <-chan bool) (<-chan RawEvent, error) }
  • 11. Firebase Streaming How it works in HTTP: ● GET request, path to resource + query ● Accept: text/event-stream ● Must follow redirects ● Data: event: patch data: {“path”:”right/here”,“data”:{“my”: “data”}} event: keep-alive data: null event: keep-alive data: null
  • 12. Firebase Streaming in Go In Go: ● Model stream as a channel ● Channel of what type? ○ Everything unmarshals to map[string]interface{} as: ■ string / float64 / bool / map / []interface{} ○ But my “data” is my own struct type! ○ I want events via chan <MyType> ■ Type-generic channel, type is a parameter
  • 13. No Generics? No Problem! (Sort of) You can do reflection gymnastics: ○ func ParametricType(myType reflect.Type) ○ Call like this: ■ ParametricType(reflect.TypeOf(Widget{})) ■ Meanwhile, in ParametricType(): ptr := reflect.New(myType).Interface() ● Allocates a new Widget object, returns pointer ● Type-generic instantiation
  • 14. No Generics? No Problem! (Sort of) That’s a working solution, but… Exposes reflection in an external API. Alternative: ● interface stream + unmarshaller callback ● Longer, but perhaps cleaner interface
  • 15. No Generics? No Problem! (Sort of) Watch(unmarshaller EventUnmarshaller, stop <-chan bool) (<-chan StreamEvent, error) type EventUnmarshaller func(jsonText []byte) (interface{}, error) type StreamEvent struct { Event string Path string Resource interface{} RawData string Error error }
  • 16. Now You Can Stream Widgets! func widgetParser(text []byte) (interface{} error) { var w Widget err := json.Unmarshal(text, &w) return w, err } … events, err := client.Watch(widgetParser, stopChan) for event := range events { widget := event.Resource.(Widget) }
  • 17. Fork Me on Github! https://github.com/ereyes01/firebase