changeset 7:f245b9a53495

Efficiency improvements.
author David Barts <n5jrn@me.com>
date Tue, 20 Sep 2022 21:54:32 -0700
parents 711cc42e96d7
children 698c4a3d758d
files src/main/kotlin/name/blackcap/passman/Database.kt src/main/kotlin/name/blackcap/passman/Entry.kt src/main/kotlin/name/blackcap/passman/ListSubcommand.kt
diffstat 3 files changed, 26 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/Database.kt	Tue Sep 20 20:52:21 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/Database.kt	Tue Sep 20 21:54:32 2022 -0700
@@ -120,12 +120,6 @@
 public fun ResultSet.getDecrypted(columnIndex: Int, encryption: Encryption) =
     encryption.decrypt(getBytes(columnIndex))
 
-public fun ResultSet.getDecryptedString(columnLabel: String, encryption: Encryption) =
-    encryption.decryptToString(getBytes(columnLabel))
-
-public fun ResultSet.getDecrypted(columnLabel: String, encryption: Encryption) =
-    encryption.decrypt(getBytes(columnLabel))
-
 public fun PreparedStatement.setEncryptedString(columnIndex: Int, value: String, encryption: Encryption) =
     setBytes(columnIndex, encryption.encryptFromString(value))
 
--- a/src/main/kotlin/name/blackcap/passman/Entry.kt	Tue Sep 20 20:52:21 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/Entry.kt	Tue Sep 20 21:54:32 2022 -0700
@@ -1,11 +1,19 @@
 package name.blackcap.passman
 
 import java.util.*
+import kotlin.reflect.KProperty
+import kotlin.reflect.full.declaredMemberProperties
 
 class Entry(val name: String, val username: String, val password: CharArray, val notes: String,
             val created: Date? = null, val modified: Date? = null, val accessed: Date? = null) {
 
     companion object {
+        private val FIELD_MAP = mutableMapOf<String, KProperty.Getter<*>>().apply {
+            Entry::class.declaredMemberProperties.forEach{
+                this[it.name] = it.getter
+            }
+        }
+
         fun withPromptedPassword() = Entry(
                 name = _getName(),
                 username = _getUsername(),
@@ -57,6 +65,10 @@
         printDate("Accessed", accessed)
     }
 
+    fun getField(name: String): Any? {
+        return FIELD_MAP[name]!!.call(this)
+    }
+
     private fun printDate(tag: String, date: Date?) {
         kotlin.io.print("${tag}: ")
         if (date == null) {
--- a/src/main/kotlin/name/blackcap/passman/ListSubcommand.kt	Tue Sep 20 20:52:21 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/ListSubcommand.kt	Tue Sep 20 21:54:32 2022 -0700
@@ -35,7 +35,7 @@
         val REDACTED = "(redacted)".toCharArray()
     }
     private lateinit var commandLine: CommandLine
-    private val matchers = mutableMapOf<String, MutableList<(Any) -> Boolean>>()
+    private val matchers = mutableMapOf<String, MutableList<(Any?) -> Boolean>>()
 
     private data class OptionDescriptor(val name: String, val help: String)
 
@@ -75,7 +75,7 @@
                 }
                 try {
                     if (it.name !in matchers) {
-                        matchers[it.name] = mutableListOf<(Any) -> Boolean>()
+                        matchers[it.name] = mutableListOf<(Any?) -> Boolean>()
                     }
                     matchers[it.name]!! += { x -> x is String && x.contains(Regex(value, regexOptions)) }
                 } catch (e: PatternSyntaxException) {
@@ -101,7 +101,7 @@
                     throw RuntimeException("will never happen")
                 }
                 if (it.name !in matchers) {
-                    matchers[it.name] = mutableListOf<(Any) -> Boolean>()
+                    matchers[it.name] = mutableListOf<(Any?) -> Boolean>()
                 }
                 when(op) {
                     '>' -> matchers[it.name]!! += { x -> x is Long && x > value }
@@ -121,21 +121,21 @@
             val printer = if (commandLine.hasOption(LONG)) Entry::printLong else Entry::print
             var count = 0;
             while (results.next()) {
+                val entry = Entry(
+                    name = results.getDecryptedString(1, db.encryption),
+                    username = results.getDecryptedString(2, db.encryption),
+                    password = REDACTED,
+                    notes = results.getDecryptedString(3, db.encryption),
+                    created = results.getDate(4),
+                    modified = results.getDate(5),
+                    accessed = results.getDate(6)
+                )
                 val passed = matchers
-                    .map { entry -> entry.value.fold(true) {
-                            acc, pred -> acc && pred(results.getDecryptedString(entry.key, db.encryption)) } }
+                    .map { x -> x.value.fold(true) {
+                            acc, pred -> acc && pred(entry.getField(x.key)) } }
                     .reduceOrNull() { a, b -> a && b }
                     ?: true
                 if (passed) {
-                    val entry = Entry(
-                        name = results.getDecryptedString(1, db.encryption),
-                        username = results.getDecryptedString(2, db.encryption),
-                        password = REDACTED,
-                        notes = results.getDecryptedString(3, db.encryption),
-                        created = results.getDate(4),
-                        modified = results.getDate(5),
-                        accessed = results.getDate(6)
-                    )
                     if (count > 0) {
                         println()
                     }
@@ -151,7 +151,3 @@
         }
     }
 }
-
-private fun Options.addMultiOption(opt: String?, longOpt: String, hasArg: Boolean, description: String): Unit {
-    addOption(Option(opt, longOpt, hasArg, description).apply { args = Option.UNLIMITED_VALUES })
-}
\ No newline at end of file