This is an automated email from the ASF dual-hosted git repository.

gstein pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/steve.git

commit f5e392ea6834dda6c39a213f88f3c73a4a586d16
Author: Greg Stein <[email protected]>
AuthorDate: Tue Jun 7 00:42:50 2022 -0500

    KV as JSON should be internal; not part of the API
---
 v3/steve/election.py      | 32 +++++++++++++++++++++++++-------
 v3/test/check_coverage.py |  4 ++--
 2 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/v3/steve/election.py b/v3/steve/election.py
index d8b5df5..e9e54ba 100644
--- a/v3/steve/election.py
+++ b/v3/steve/election.py
@@ -21,9 +21,11 @@
 #
 
 import sys
+import json
 
 from . import crypto
 from . import db
+from . import vtypes
 
 
 class Election:
@@ -174,17 +176,19 @@ class Election:
 
         # NEVER return issue.salt
         issue = self.q_get_issue.first_row((iid,))
-        return issue.title, issue.description, issue.type, issue.kv
 
-    def add_issue(self, iid, title, description, type, kv):
+        return (issue.title, issue.description, issue.type,
+                self.json2kv(issue.kv))
+
+    def add_issue(self, iid, title, description, vtype, kv):
         "Add or update an issue designated by IID."
         assert self.is_editable()
-
-        ### validate TYPE
+        assert vtype in vtypes.TYPES
 
         # If we ADD, then SALT will be NULL. If we UPDATE, then it will not
         # be touched (it should be NULL).
-        self.c_add_issue.perform((iid, title, description, type, kv, None))
+        self.c_add_issue.perform((iid, title, description, vtype,
+                                  self.kv2json(kv), None))
 
     def delete_issue(self, iid):
         "Delete the Issue designated by IID."
@@ -195,9 +199,12 @@ class Election:
     def list_issues(self):
         "Return ordered (IID, TITLE, DESCRIPTION, TYPE, KV) for all ISSUES."
 
-        # NOTE: the SALT column is omitted. It should never be exposed.
+        def extract_issue(row):
+            # NOTE: the SALT column is omitted. It should never be exposed.
+            return row[:4] + (self.json2kv(row.kv),)
+
         self.q_issues.perform()
-        return [ row[:5] for row in self.q_issues.fetchall() ]
+        return [ extract_issue(row) for row in self.q_issues.fetchall() ]
 
     def get_person(self, pid):
         "Return NAME, EMAIL for Person identified by PID."
@@ -347,6 +354,17 @@ class Election:
 
         return self.S_OPEN
 
+    @staticmethod
+    def kv2json(kv):
+        'Convert a structured KV into a JSON string for storage.'
+        # Note: avoid serializing None.
+        return kv and json.dumps(kv)
+
+    @staticmethod
+    def json2kv(j):
+        'Convert the KV JSON string back into its structured value.'
+        return j and json.loads(j)
+
 
 def new_eid():
     "Create a new ElectionID."
diff --git a/v3/test/check_coverage.py b/v3/test/check_coverage.py
index 7efaf66..4eeaeb7 100755
--- a/v3/test/check_coverage.py
+++ b/v3/test/check_coverage.py
@@ -71,7 +71,7 @@ def touch_every_line():
     _ = e.get_person('alice')
 
     e.add_issue('a', 'issue A', None, 'yna', None)
-    e.add_issue('b', 'issue B', None, 'stv', json.dumps({
+    e.add_issue('b', 'issue B', None, 'stv', {
         'seats': 3,
         'labelmap': {
             'a': 'Alice',
@@ -80,7 +80,7 @@ def touch_every_line():
             'd': 'David',
             'e': 'Eve',
             },
-        }))
+        })
     _ = e.list_issues()
     e.add_issue('c', 'issue C', None, 'yna', None)
     e.delete_issue('c')

Reply via email to