Firestore Kotlin Data Convertor
Fireko does generate functions to convert DocumentSnapshot
to your (data) classes. What you have to do is to annotate it with Fireko
and the rest is done automatically.
Advantages over using DocumentSnapshot::toObject):
@Fireko
data class User(
@DocumentId val id: String,
val firstName: String,
val lastName: String,
@ServerTimestamp val createdAt: Instant? = null,
@Embedded val address: Address,
val position: LatLng,
)
@Fireko
data class Address(
val street: String,
val city: String,
@PropertyName("zip_code") val zipCode: String,
val country: String,
)
@FirekoAdapter(LatLngAdapter::class)
data class LatLng(val lat: Double, val lng: Double)
object LatLngAdapter {
@FirekoAdapter.Read
fun read(value: GeoPoint): LatLng = value.run { LatLng(latitude, longitude) }
@FirekoAdapter.Write
fun write(value: LatLng): GeoPoint = value.run { GeoPoint(lat, lng) }
}
// query user
FirebaseFirestore.getInstance()
.collection("users")
.document("user1")
.addSnapshotListener { snapshot, _ ->
val user = snapshot?.toUser() // toUser is generated
// ...
}
// store user
FirebaseFirestore.getInstance()
.collection("users")
.document("user1")
.set(user.toMap()) // toMap is generated
.addOnCompleteListener {
// ...
}
java.time.*
org.threeten.bp.*
java.net.URI
java.net.URL
android.net.Uri
java.util.Currency
android.icu.util.Currency
repositories {
mavenCentral()
}
dependencies {
def fireko_version = 'x.y.z'
compileOnly "de.musichin.fireko:fireko:$fireko_version"
kapt "de.musichin.fireko:fireko-codegen:$fireko_version"
}
Contributors are more than welcome. Just upload a PR with a description of your changes.
File github issues for anything that is unexpectedly broken.
MIT License
Copyright (c) 2020 Anton Musichin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.