From 58e4ab0f58bcd96a6e8decf217d60e3768c65396 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mikl=C3=B3s=20T=C3=B3th?= <tothmiklostibor@gmail.com>
Date: Fri, 17 Jun 2022 18:01:29 +0200
Subject: [PATCH] Error memes

---
 src/jsMain/kotlin/datasources/GSheet.kt | 28 +++++++++++++------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/src/jsMain/kotlin/datasources/GSheet.kt b/src/jsMain/kotlin/datasources/GSheet.kt
index 387b763..d2e4843 100644
--- a/src/jsMain/kotlin/datasources/GSheet.kt
+++ b/src/jsMain/kotlin/datasources/GSheet.kt
@@ -9,7 +9,7 @@ import pekstuff.trimAsPrinciple
 import kotlin.coroutines.resume
 import kotlin.coroutines.suspendCoroutine
 
-class GSheet private constructor(sheet: sheets.Spreadsheet, sheetNum: Int = 0): DataSource, NoteDataSource {
+class GSheet private constructor(val sheet: sheets.Spreadsheet, sheetNum: Int = 0): DataSource, NoteDataSource {
     companion object {
         private val filterList = listOf(
             "szumma", "színes belépő"
@@ -18,7 +18,6 @@ class GSheet private constructor(sheet: sheets.Spreadsheet, sheetNum: Int = 0):
         suspend fun new(url: String, sheetNum: Int = 0): GSheet {
             if (!isSignedIn) throw Error("You must sign in to get gapi.sheets")
             val sheet = getSheetFromUrl(url)
-            window.asDynamic().test = sheet
             return GSheet(sheet, sheetNum)
         }
 
@@ -36,19 +35,19 @@ class GSheet private constructor(sheet: sheets.Spreadsheet, sheetNum: Int = 0):
         }
     }
 
-    private val rows = sheet.sheets!![sheetNum].data!![0].rowData!!
+    private val rows = sheet.sheets?.get(sheetNum)?.data?.get(0)?.rowData ?: throw SheetException("NullPtrError", this)
 
-    private val principleIndexes = rows[0].values!!.mapIndexed { i, data ->
+    private val principleIndexes = rows[0].values?.mapIndexed { i, data ->
         (data.formattedValue?.trimAsPrinciple() ?: "") to i
-    }.drop(1).filter {
+    }?.drop(1)?.filter {
         filterList.all { filter -> !it.first.lowercase().contains(filter) } &&
             it.first.isNotBlank()
-    }.toMap()
+    }?.toMap() ?: throw SheetException("NullPtrException principles", this)
 
     override val principles get() = principleIndexes.keys
 
     private val peopleIndexes = rows
-        .mapIndexed { i, data -> (data.values!![0].formattedValue ?: "") to i }
+        .mapIndexed { i, data -> (data.values?.get(0)?.formattedValue ?: "") to i }
         .drop(1)
         .filter { it.first.isNotBlank() }
         .toMap()
@@ -56,14 +55,17 @@ class GSheet private constructor(sheet: sheets.Spreadsheet, sheetNum: Int = 0):
     override val people get() = peopleIndexes.keys
 
     override operator fun get(person: String, principle: String): Int {
-        val pers = peopleIndexes[person]!!
-        val princ = principleIndexes[principle]!!
-        return rows[pers].values!![princ].formattedValue!!.toIntOrNull() ?: 0
+        val pers = peopleIndexes[person] ?: throw Error("person $person")
+        val princ = principleIndexes[principle] ?: throw Error("principle $principle")
+        return rows[pers].values?.get(princ)?.formattedValue?.toIntOrNull() ?: 0
     }
 
     override fun getNote(person: String, principle: String): String {
-        val pers = peopleIndexes[person]!!
-        val princ = principleIndexes[principle]!!
-        return rows[pers].values!![princ].note ?: ""
+        val pers = peopleIndexes[person] ?: throw Error("person $person not found")
+        val princ = principleIndexes[principle] ?: throw Error("principle $principle not found")
+        return rows[pers].values?.get(princ)?.note ?: ""
     }
 }
+
+
+class SheetException(str: String, val sheet: GSheet): Exception(str)
\ No newline at end of file
-- 
GitLab