fix: code review pt. 2

This commit is contained in:
Richard Ramos 2020-10-26 22:38:13 -04:00 committed by Eric Mastro
parent 247ebd61a4
commit 2a3599c94a
5 changed files with 95 additions and 207 deletions

1
nim_status.nims Symbolic link
View File

@ -0,0 +1 @@
nim_status.nimble

View File

@ -42,7 +42,6 @@ proc createSettings*(db: DbConn, s: Settings, nodecfg: JsonNode) = # TODO: repla
s.signingPhrase,
(if s.walletRootAddress.isSome(): $s.walletRootAddress.get() else: ""))
proc saveSetting*(db: DbConn, setting: SettingsType, value: bool) =
case setting
of SettingsType.ChaosMode:
@ -197,50 +196,50 @@ proc getSettings*(db: DbConn): Settings =
for r in rows(db, query):
result.userAddress = r[0].strVal.parseAddress
result.chaosMode = r[1].optionBool
result.currency = r[2].optionString
result.chaosMode = toOption[bool](r[1])
result.currency = toOption[string](r[2])
result.currentNetwork = r[3].strVal
result.customBootNodes = r[4].optionJsonNode
result.customBootNodesEnabled = r[5].optionJsonNode
result.customBootNodes = toOption[JsonNode](r[4])
result.customBootNodesEnabled = toOption[JsonNode](r[5])
result.dappsAddress = r[6].strVal.parseAddress
result.eip1581Address = r[7].strVal.parseAddress
result.fleet = r[8].optionString
result.hideHomeToolTip = r[9].optionBool
result.fleet = toOption[string](r[8])
result.hideHomeToolTip = toOption[bool](r[9])
result.installationID = r[10].strVal
result.keyUID = r[11].strVal
result.keycardInstanceUID = r[12].optionString
result.keycardPairedOn = r[13].optionInt
result.keycardPairing = r[14].optionString
result.lastUpdated = r[15].optionInt64
result.keycardInstanceUID = toOption[string](r[12])
result.keycardPairedOn = toOption[int64](r[13])
result.keycardPairing = toOption[string](r[14])
result.lastUpdated = toOption[int64](r[15])
result.latestDerivedPath = r[16].intVal.uint
result.logLevel = r[17].optionString
result.mnemonic = r[18].optionString
result.name = r[19].optionString
result.logLevel = toOption[string](r[17])
result.mnemonic = toOption[string](r[18])
result.name = toOption[string](r[19])
result.networks = r[20].strVal.parseJson
result.notificationsEnabled = r[21].optionBool
result.pushNotificationsServerEnabled = r[22].optionBool
result.pushNotificationsFromContactsOnly = r[23].optionBool
result.remotePushNotificationsEnabled = r[24].optionBool
result.sendPushNotifications = r[25].optionBool
result.pushNotificationsBlockMentions = r[26].optionBool
result.notificationsEnabled = toOption[bool](r[21])
result.pushNotificationsServerEnabled = toOption[bool](r[22])
result.pushNotificationsFromContactsOnly = toOption[bool](r[23])
result.remotePushNotificationsEnabled = toOption[bool](r[24])
result.sendPushNotifications = toOption[bool](r[25])
result.pushNotificationsBlockMentions = toOption[bool](r[26])
result.photoPath = r[27].strVal
result.pinnedMailservers = r[28].optionJsonNode
result.preferredName = r[29].optionString
result.pinnedMailservers = toOption[JsonNode](r[28])
result.preferredName = toOption[string](r[29])
result.previewPrivacy = r[30].intVal.bool
result.publicKey = r[31].strVal
result.rememberSyncingChoice = r[32].optionBool
result.rememberSyncingChoice = toOption[bool](r[32])
result.signingPhrase = r[33].strVal
result.stickerPacksInstalled = r[34].optionJsonNode
result.stickersPacksPending = r[35].optionJsonNode
result.stickersRecentStickers = r[36].optionJsonNode
result.syncingOnMobileNetwork = r[37].optionBool
result.stickerPacksInstalled = toOption[JsonNode](r[34])
result.stickersPacksPending = toOption[JsonNode](r[35])
result.stickersRecentStickers = toOption[JsonNode](r[36])
result.syncingOnMobileNetwork = toOption[bool](r[37])
result.useMailservers = r[38].intVal.bool
result.usernames = r[39].optionJsonNode
result.usernames = toOption[JsonNode](r[39])
result.appearance = r[40].intVal.uint
result.walletRootAddress = r[41].optionAddress
result.walletSetUpPassed = r[42].optionBool
result.walletVisibleTokens = r[43].optionJsonNode
result.wakuBloomFilterMode = r[44].optionBool
result.wakuEnabled = r[45].optionBool
result.webViewAllowPermissionRequests = r[46].optionBool
result.walletRootAddress = toOption[Address](r[41])
result.walletSetUpPassed = toOption[bool](r[42])
result.walletVisibleTokens = toOption[JsonNode](r[43])
result.wakuBloomFilterMode = toOption[bool](r[44])
result.wakuEnabled =toOption[bool](r[45])
result.webViewAllowPermissionRequests = toOption[bool](r[46])
break

View File

@ -1,8 +1,10 @@
import options, json, json_serialization
import json_serialization/[reader, writer, lexer]
import utils
import web3/[conversions, ethtypes]
type
SettingsError* = object of CatchableError
@ -59,32 +61,32 @@ type
Settings* = object
userAddress* {.serializedFieldName($SettingsType.Address).}: Address
chaosMode* {.serializedFieldName($SettingsType.ChaosMode).}: Option[bool]
currency* {.dontSerialize, serializedFieldName($SettingsType.Currency).}: Option[string]
currentNetwork* {.dontSerialize, serializedFieldName($SettingsType.CurrentNetwork).}: string
currency* {.serializedFieldName($SettingsType.Currency).}: Option[string]
currentNetwork* {.serializedFieldName($SettingsType.CurrentNetwork).}: string
customBootnodes* {.dontSerialize, serializedFieldName($SettingsType.CustomBootnodes).}: Option[JsonNode]
customBootnodesEnabled* {.dontSerialize, serializedFieldName($SettingsType.CustomBootnodesEnabled).}: Option[JsonNode]
dappsAddress* {.dontSerialize, serializedFieldName($SettingsType.DappsAddress).}: Address
eip1581Address* {.dontSerialize, serializedFieldName($SettingsType.EIP1581Address).}: Address
dappsAddress* {.serializedFieldName($SettingsType.DappsAddress).}: Address
eip1581Address* {.serializedFieldName($SettingsType.EIP1581Address).}: Address
fleet* {.dontSerialize, serializedFieldName($SettingsType.Fleet).}: Option[string]
hideHomeTooltip* {.dontSerialize, serializedFieldName($SettingsType.HideHomeTooltip).}: Option[bool]
installationID* {.dontSerialize, serializedFieldName($SettingsType.InstallationID).}: string
keyUID* {.dontSerialize, serializedFieldName($SettingsType.KeyUID).}: string
keycardInstanceUID* {.dontSerialize, serializedFieldName($SettingsType.Keycard_InstanceUID).}: Option[string]
keycardPairedOn* {.dontSerialize, serializedFieldName($SettingsType.Keycard_PairedOn).}: Option[int64]
keycardPairing* {.dontSerialize, serializedFieldName($SettingsType.Keycard_Pairing).}: Option[string]
installationID* {.serializedFieldName($SettingsType.InstallationID).}: string
keyUID* {.serializedFieldName($SettingsType.KeyUID).}: string
keycardInstanceUID* {.serializedFieldName($SettingsType.Keycard_InstanceUID).}: Option[string]
keycardPairedOn* {.serializedFieldName($SettingsType.Keycard_PairedOn).}: Option[int64]
keycardPairing* {.serializedFieldName($SettingsType.Keycard_Pairing).}: Option[string]
lastUpdated* {.dontSerialize, serializedFieldName($SettingsType.LastUpdated).}: Option[int64]
latestDerivedPath* {.dontSerialize, serializedFieldName($SettingsType.LatestDerivedPath).}: uint
latestDerivedPath* {.serializedFieldName($SettingsType.LatestDerivedPath).}: uint
logLevel* {.dontSerialize, serializedFieldName($SettingsType.LogLevel).}: Option[string]
mnemonic* {.dontSerialize, serializedFieldName($SettingsType.Mnemonic).}: Option[string]
name* {.dontSerialize, serializedFieldName($SettingsType.Name).}: Option[string]
networks* {.dontSerialize, serializedFieldName($SettingsType.Networks).}: JsonNode
mnemonic* {.serializedFieldName($SettingsType.Mnemonic).}: Option[string]
name* {.serializedFieldName($SettingsType.Name).}: Option[string]
networks* {.serializedFieldName($SettingsType.Networks).}: JsonNode
# NotificationsEnabled indicates whether local notifications should be enabled (android only)
notificationsEnabled* {.dontSerialize, serializedFieldName($SettingsType.NotificationsEnabled).}: Option[bool]
photoPath* {.dontSerialize, serializedFieldName($SettingsType.PhotoPath).}: string
photoPath* {.serializedFieldName($SettingsType.PhotoPath).}: string
pinnedMailservers* {.dontSerialize, serializedFieldName($SettingsType.PinnedMailservers).}: Option[JsonNode]
preferredName* {.dontSerialize, serializedFieldName($SettingsType.PreferredName).}: Option[string]
previewPrivacy* {.dontSerialize, serializedFieldName($SettingsType.PreviewPrivacy).}: bool
publicKey* {.dontSerialize, serializedFieldName($SettingsType.PublicKey).}: string
previewPrivacy* {.serializedFieldName($SettingsType.PreviewPrivacy).}: bool
publicKey* {.serializedFieldName($SettingsType.PublicKey).}: string
# PushNotificationsServerEnabled indicates whether we should be running a push notification server
pushNotificationsServerEnabled* {.dontSerialize, serializedFieldName($SettingsType.PushNotificationsServerEnabled).}: Option[bool]
# PushNotificationsFromContactsOnly indicates whether we should only receive push notifications from contacts
@ -94,7 +96,7 @@ type
rememberSyncingChoice* {.dontSerialize, serializedFieldName($SettingsType.RememberSyncingChoice).}: Option[bool]
# RemotePushNotificationsEnabled indicates whether we should be using remote notifications (ios only for now)
remotePushNotificationsEnabled* {.dontSerialize, serializedFieldName($SettingsType.RemotePushNotificationsEnabled).}: Option[bool]
signingPhrase* {.dontSerialize, serializedFieldName($SettingsType.SigningPhrase).}: string
signingPhrase* {.serializedFieldName($SettingsType.SigningPhrase).}: string
stickerPacksInstalled* {.dontSerialize, serializedFieldName($SettingsType.StickersPacksInstalled).}: Option[JsonNode]
stickersPacksPending* {.dontSerialize, serializedFieldName($SettingsType.StickersPacksPending).}: Option[JsonNode]
stickersRecentStickers* {.dontSerialize, serializedFieldName($SettingsType.StickersRecentStickers).}: Option[JsonNode]
@ -104,115 +106,36 @@ type
appearance* {.dontSerialize, serializedFieldName($SettingsType.Appearance).}: uint
useMailservers* {.dontSerialize, serializedFieldName($SettingsType.UseMailservers).}: bool
usernames* {.dontSerialize, serializedFieldName($SettingsType.Usernames).}: Option[JsonNode]
walletRootAddress* {.dontSerialize, serializedFieldName($SettingsType.WalletRootAddress).}: Option[Address]
walletRootAddress* {.serializedFieldName($SettingsType.WalletRootAddress).}: Option[Address]
walletSetUpPassed* {.dontSerialize, serializedFieldName($SettingsType.WalletSetUpPassed).}: Option[bool]
walletVisibleTokens* {.dontSerialize, serializedFieldName($SettingsType.WalletVisibleTokens).}: Option[JsonNode]
wakuEnabled* {.dontSerialize, serializedFieldName($SettingsType.WakuEnabled).}: Option[bool]
wakuBloomFilterMode* {.dontSerialize, serializedFieldName($SettingsType.WakuBloomFilterMode).}: Option[bool]
webViewAllowPermissionRequests* {.dontSerialize, serializedFieldName($SettingsType.WebviewAllowPermissionRequests).}: Option[bool]
proc writeValue*(writer: var JsonWriter, value: Settings) =
writer.beginRecord()
for key, val in fieldPairs(value):
when val is Option:
if val.isSome:
writer.writeField $key, val.get()
else:
writer.writeField $key, $val
writer.endRecord()
proc readValue*[T](reader: var JsonReader, value: var Option[T]) =
let tok = reader.lexer.tok
if tok == tkNull:
reset value
reader.lexer.next()
else:
let v = reader.readValue(T)
if v == default(T):
reset value
else:
value = some v
proc `$`*(self: Settings): string =
# echo Json.encode(self)
var output = %* {
$SettingsType.Address: self.userAddress,
$SettingsType.CurrentNetwork: self.currentNetwork,
$SettingsType.DappsAddress: self.dappsAddress,
$SettingsType.EIP1581Address: self.eip1581Address,
$SettingsType.InstallationID: self.installationID,
$SettingsType.KeyUID: self.keyUID,
$SettingsType.LatestDerivedPath: self.latestDerivedPath,
$SettingsType.Networks: self.networks,
$SettingsType.PhotoPath: self.photoPath,
$SettingsType.PreviewPrivacy: self.previewPrivacy,
$SettingsType.PublicKey: self.publicKey,
$SettingsType.Appearance: self.appearance,
$SettingsType.UseMailservers: self.useMailservers,
$SettingsType.SigningPhrase: self.signingPhrase,
}
output.addOptionalValue($SettingsType.ChaosMode, self.chaosMode)
output.addOptionalValue($SettingsType.Currency, self.currency)
output.addOptionalValue($SettingsType.CustomBootNodes, self.customBootnodes)
output.addOptionalValue($SettingsType.CustomBootnodesEnabled, self.customBootnodesEnabled)
output.addOptionalValue($SettingsType.Fleet, self.fleet)
output.addOptionalValue($SettingsType.HideHomeToolTip, self.hideHomeTooltip)
output.addOptionalValue($SettingsType.KeyCard_InstanceUID, self.keycardInstanceUID)
output.addOptionalValue($SettingsType.Keycard_PairedOn, self.keycardPairedOn)
output.addOptionalValue($SettingsType.KeycardPairing, self.keycardPairing)
output.addOptionalValue($SettingsType.LastUpdated, self.lastUpdated)
output.addOptionalValue($SettingsType.LogLevel, self.logLevel)
output.addOptionalValue($SettingsType.Mnemonic, self.mnemonic)
output.addOptionalValue($SettingsType.Name, self.name)
output.addOptionalValue($SettingsType.NotificationsEnabled, self.notificationsEnabled)
output.addOptionalValue($SettingsType.PinnedMailservers, self.pinnedMailservers)
output.addOptionalValue($SettingsType.PreferredName, self.preferredName)
output.addOptionalValue($SettingsType.PushNotificationsServerEnabled, self.pushNotificationsServerEnabled)
output.addOptionalValue($SettingsType.PushNotificationsFromContactsOnly, self.pushNotificationsFromContactsOnly)
output.addOptionalValue($SettingsType.PushNotificationsBlockMentions, self.pushNotificationsBlockMentions)
output.addOptionalValue($SettingsType.RememberSyncingChoice, self.rememberSyncingChoice)
output.addOptionalValue($SettingsType.RemotePushNotificationsEnabled, self.remotePushNotificationsEnabled)
output.addOptionalValue($SettingsType.StickersPacksInstalled, self.stickerPacksInstalled)
output.addOptionalValue($SettingsType.StickersPacksPending, self.stickersPacksPending)
output.addOptionalValue($SettingsType.StickersRecentStickers, self.stickersRecentStickers)
output.addOptionalValue($SettingsType.SyncingOnMobileNetwork, self.syncingOnMobileNetwork)
output.addOptionalValue($SettingsType.SendPushNotifications, self.sendPushNotifications)
output.addOptionalValue($SettingsType.Usernames, self.usernames)
output.addOptionalValue($SettingsType.WalletRootAddress, self.walletRootAddress)
output.addOptionalValue($SettingsType.WalletSetUpPassed, self.walletSetUpPassed)
output.addOptionalValue($SettingsType.WalletVisibleTokens, self.walletVisibleTokens)
output.addOptionalValue($SettingsType.WakuEnabled, self.wakuEnabled)
output.addOptionalValue($SettingsType.WakuBloomFilterMode, self.wakuBloomFilterMode)
output.addOptionalValue($SettingsType.WebViewAllowPermissionRequests, self.webViewAllowPermissionRequests)
result = $output
proc toSettings*(self: string): Settings =
let o = self.parseJson
result.userAddress = parseAddress(o[$SettingsType.Address].getStr)
result.currentNetwork = o[$SettingsType.CurrentNetwork].getStr
result.dappsAddress = parseAddress(o[$SettingsType.DappsAddress].getStr)
result.eip1581Address = parseAddress(o[$SettingsType.EIP1581Address].getStr)
result.installationID = o[$SettingsType.InstallationID].getStr
result.keyUID = o[$SettingsType.KeyUID].getStr
result.latestDerivedPath = o[$SettingsType.LatestDerivedPath].getInt.uint
result.networks = o[$SettingsType.Networks]
result.photoPath = o[$SettingsType.PhotoPath].getStr
result.previewPrivacy = o[$SettingsType.PreviewPrivacy].getBool
result.publicKey = o[$SettingsType.PublicKey].getStr
result.appearance = o{$SettingsType.Appearance}.getInt.uint
result.useMailservers = o{$SettingsType.UseMailservers}.getBool
result.signingPhrase = o[$SettingsType.SigningPhrase].getStr
result.chaosMode = getOption[bool](o, $SettingsType.ChaosMode)
result.currency = getOption[string](o, $SettingsType.Currency)
result.customBootnodes = getOption[JsonNode](o, $SettingsType.CustomBootNodes)
result.customBootnodesEnabled = getOption[JsonNode](o, $SettingsType.CustomBootnodesEnabled)
result.fleet = getOption[string](o, $SettingsType.Fleet)
result.hideHomeTooltip = getOption[bool](o, $SettingsType.HideHomeToolTip)
result.keycardInstanceUID = getOption[string](o, $SettingsType.KeyCard_InstanceUID)
result.keycardPairedOn = getOption[int64](o, $SettingsType.Keycard_PairedOn)
result.keycardPairing = getOption[string](o, $SettingsType.KeycardPairing)
result.lastUpdated = getOption[int64](o, $SettingsType.LastUpdated)
result.logLevel = getOption[string](o, $SettingsType.LogLevel)
result.mnemonic = getOption[string](o, $SettingsType.Mnemonic)
result.name = getOption[string](o, $SettingsType.Name)
result.notificationsEnabled = getOption[bool](o, $SettingsType.NotificationsEnabled)
result.pinnedMailservers = getOption[JsonNode](o, $SettingsType.PinnedMailservers)
result.preferredName = getOption[string](o, $SettingsType.PreferredName)
result.pushNotificationsServerEnabled = getOption[bool](o, $SettingsType.PushNotificationsServerEnabled)
result.pushNotificationsFromContactsOnly = getOption[bool](o, $SettingsType.PushNotificationsFromContactsOnly)
result.pushNotificationsBlockMentions = getOption[bool](o, $SettingsType.PushNotificationsBlockMentions)
result.rememberSyncingChoice = getOption[bool](o, $SettingsType.RememberSyncingChoice)
result.remotePushNotificationsEnabled = getOption[bool](o, $SettingsType.RemotePushNotificationsEnabled)
result.stickerPacksInstalled = getOption[JsonNode](o, $SettingsType.StickersPacksInstalled)
result.stickersPacksPending = getOption[JsonNode](o, $SettingsType.StickersPacksPending)
result.stickersRecentStickers = getOption[JsonNode](o, $SettingsType.StickersRecentStickers)
result.syncingOnMobileNetwork = getOption[bool](o, $SettingsType.SyncingOnMobileNetwork)
result.sendPushNotifications = getOption[bool](o, $SettingsType.SendPushNotifications)
result.usernames = getOption[JsonNode](o, $SettingsType.Usernames)
result.walletRootAddress = getOption[Address](o, $SettingsType.WalletRootAddress)
result.walletSetUpPassed = getOption[bool](o, $SettingsType.WalletSetUpPassed)
result.walletVisibleTokens = getOption[JsonNode](o, $SettingsType.WalletVisibleTokens)
result.wakuEnabled = getOption[bool](o, $SettingsType.WakuEnabled)
result.wakuBloomFilterMode = getOption[bool](o, $SettingsType.WakuBloomFilterMode)
result.webViewAllowPermissionRequests = getOption[bool](o, $SettingsType.WebViewAllowPermissionRequests)
return Json.encode(self)

View File

@ -6,55 +6,18 @@ import sqlcipher
proc parseAddress*(strAddress: string): Address =
fromHex(Address, strAddress)
proc getOption*[T](self: JsonNode, key: string): Option[T] =
if not self.hasKey(key):
proc toOption*[T](self: DbValue): Option[T] =
if self.kind == sqliteNull:
result = none(T)
else:
# handle special cases
when T is Address:
result = if self{key}.getStr == "": none(T) else: some(parseAddress(self[key].getStr))
when T is JsonNode:
result = if self[key].kind == JNull: none(T) else: some(self[key])
when T is int64:
result = if self[key].getBiggestInt == 0: none(T) else: some(self[key].getBiggestInt)
# for all other (default) cases
when T is string:
result = if self{key}.getStr == "": none(T) else: some(self[key].getStr)
when T is bool:
result = if not self{key}.getBool: none(T) else: some(self[key].getBool)
else:
result = some(self[key].to(T))
result = if self.intVal == 0: none(T) else: some(true)
when T is int64 or T is int:
result = if self.intVal == 0: none(T) else: some(self.intVal)
when T is JsonNode:
result = if self.strVal == "": none(T) else: some(self.strVal.parseJson)
when T is Address:
result = if self.strVal == "": none(T) else: some(self.strVal.parseAddress)
when T is string:
result = if self.strVal == "": none(T) else: some(self.strVal)
proc addOptionalValue*[T](self: var JsonNode, key: string, value: Option[T]) =
if value.isSome:
self[key] = %* value.get()
proc optionBool*(self: DbValue): Option[bool] =
if self.kind == sqliteNull or self.intVal == 0:
return none(bool)
return some(true)
proc optionInt64*(self: DbValue): Option[int64] =
if self.kind == sqliteNull or self.intVal == 0:
return none(int64)
return some(self.intVal)
proc optionInt*(self: DbValue): Option[int64] =
if self.kind == sqliteNull or self.intVal == 0:
return none(int64)
return some(self.intVal)
proc optionString*(self: DbValue): Option[string] =
if self.kind == sqliteNull or self.strVal == "":
return none(string)
return some(self.strVal)
proc optionAddress*(self: DbValue): Option[Address] =
if self.kind == sqliteNull or self.strVal == "":
return none(Address)
return some(self.strVal.parseAddress)
proc optionJsonNode*(self: DbValue): Option[JsonNode] =
if self.kind == sqliteNull or self.strVal == "":
return none(JsonNode)
return some(self.strVal.parseJson)

View File

@ -1,5 +1,5 @@
import sqlcipher
import os, json
import os, json, json_serialization
import options
import ../../nim_status/lib/settings
import ../../nim_status/lib/database
@ -9,7 +9,7 @@ let passwd = "qwerty"
let path = currentSourcePath.parentDir() & "/build/myDatabase"
let db = initializeDB(path, passwd)
let settingsObj = """{
let settingsStr = """{
"address": "0x1122334455667788990011223344556677889900",
"networks/current-network": "mainnet",
"dapps-address": "0x1122334455667788990011223344556677889900",
@ -22,7 +22,9 @@ let settingsObj = """{
"preview-privacy?": false,
"public-key": "0x123",
"signing-phrase": "ABC DEF GHI"
}""".toSettings
}"""
let settingsObj = JSON.decode(settingsStr, Settings, allowUnknownFields = true)
let nodeConfig = %* {"config": 1}