项目作者: musichin

项目描述 :
Firestore Kotlin Data Convertor
高级语言: Kotlin
项目地址: git://github.com/musichin/fireko.git
创建时间: 2020-06-14T18:47:17Z
项目社区:https://github.com/musichin/fireko

开源协议:MIT License

下载


Firestore Kotlin Data Convertor

Kotlin
CI

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):

  • Empty constructor is not required
  • No reflections
  • More types

Usage

  1. @Fireko
  2. data class User(
  3. @DocumentId val id: String,
  4. val firstName: String,
  5. val lastName: String,
  6. @ServerTimestamp val createdAt: Instant? = null,
  7. @Embedded val address: Address,
  8. val position: LatLng,
  9. )
  10. @Fireko
  11. data class Address(
  12. val street: String,
  13. val city: String,
  14. @PropertyName("zip_code") val zipCode: String,
  15. val country: String,
  16. )
  17. @FirekoAdapter(LatLngAdapter::class)
  18. data class LatLng(val lat: Double, val lng: Double)
  19. object LatLngAdapter {
  20. @FirekoAdapter.Read
  21. fun read(value: GeoPoint): LatLng = value.run { LatLng(latitude, longitude) }
  22. @FirekoAdapter.Write
  23. fun write(value: LatLng): GeoPoint = value.run { GeoPoint(lat, lng) }
  24. }
  25. // query user
  26. FirebaseFirestore.getInstance()
  27. .collection("users")
  28. .document("user1")
  29. .addSnapshotListener { snapshot, _ ->
  30. val user = snapshot?.toUser() // toUser is generated
  31. // ...
  32. }
  33. // store user
  34. FirebaseFirestore.getInstance()
  35. .collection("users")
  36. .document("user1")
  37. .set(user.toMap()) // toMap is generated
  38. .addOnCompleteListener {
  39. // ...
  40. }

Additional supported types:

  • java.time.*
  • org.threeten.bp.*
  • java.net.URI
  • java.net.URL
  • android.net.Uri
  • java.util.Currency
  • android.icu.util.Currency

Binaries

  1. repositories {
  2. mavenCentral()
  3. }
  4. dependencies {
  5. def fireko_version = 'x.y.z'
  6. compileOnly "de.musichin.fireko:fireko:$fireko_version"
  7. kapt "de.musichin.fireko:fireko-codegen:$fireko_version"
  8. }

Contributing

Contributors are more than welcome. Just upload a PR with a description of your changes.

Issues or feature requests?

File github issues for anything that is unexpectedly broken.

License

  1. MIT License
  2. Copyright (c) 2020 Anton Musichin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.