Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Protoファイルを使用しないProto DataStore実装

Rui
October 02, 2024

Protoファイルを使用しないProto DataStore実装

2024/09/30 【GO/note/令和トラベル】After iOSDC & DroidKaigi 2024
https://reiwatravel.connpass.com/event/327079/

Rui

October 02, 2024
Tweet

More Decks by Rui

Other Decks in Technology

Transcript

  1. 1SFGFSFODFT%BUB4UPSFͱ1SPUP%BUB4UPSF छྨ 1SFGFSFODFT%BUB4UPSF 1SPUP%BUB4UPSF εΩʔϚ ఆٛࡁΈεΩʔϚෆཁ ϓϩτίϧόοϑΝͰεΩʔϚఆ͕ٛඞཁ σʔλ΁ͷΞΫηε ΩʔΛ࢖ͬͯΞΫηε ΧελϜσʔλܕͷΠϯελϯεͱͯ͠ΞΫηε

    ܕ҆શੑ ͳ͠ ͋Γ ϝϦοτ ఆٛࡁΈͷεΩʔϚ͕ෆཁͳͨΊɺखܰʹར༻ ։࢝Ͱ͖Δ λΠϓηʔϑͰ͋ΔͨΊɺΑΓ҆શʹσʔλ؅ ཧ͕Մೳ σϝϦοτ λΠϓηʔϑͰ͸ͳ͍ͨΊɺ࣮ߦ࣌ΤϥʔͷϦε Ϋ͕͋Δ ϓϩτίϧόοϑΝͷ஌͕ࣝඞཁͱͳΔ దͨ͠࢖༻ํ๏ γϯϓϧͳΩʔόϦϡʔϖΞΛอଘ͍ͨ͠৔߹ ΧελϜσʔλܕΛ༻͍ͯɺΑΓෳࡶͳσʔλ Λอଘ͍ͨ͠৔߹
  2. syntax = "proto3"; option java_package = "com.codelab.android.datastore"; option java_multiple_files =

    true; message UserPreferences { // filter for showing / hiding completed tasks bool show_completed = 1; }
  3. syntax = "proto3"; option java_package = "com.codelab.android.datastore"; option java_multiple_files =

    true; message UserPreferences { // filter for showing / hiding completed tasks bool show_completed = 1; }
  4. 4FSJBMJ[FSΛ࡞੒ class PersonSerializer( private val stringFormat: StringFormat = Json {

    ignoreUnknownKeys = true encodeDefaults = true }, ) : Serializer<Person> { override val defaultValue: Person get() = Person() override suspend fun readFrom(input: InputStream): Person { try { val bytes = input.readBytes() val string = bytes.decodeToString() return stringFormat.decodeFromString(string) //ɹPersonΫϥεʹσίʔυͯ͠ಡΈऔΔ } catch (e: SerializationException) { throw CorruptionException("Cannot read stored data", e) } } override suspend fun writeTo(t: Person, output: OutputStream) { val string = stringFormat.encodeToString(t) val bytes = string.encodeToByteArray() // PersonΫϥε͔ΒΤϯίʔυͯ͠ॻ͖ࠐΉ withContext(Dispatchers.IO) { output.write(bytes) } } }
  5. ׬