4
4
"""This module implements SQLAlchemy type for converting date format to RFC3339 string representation."""
5
5
6
6
import datetime
7
+ import json
7
8
from typing import Any
8
9
9
10
from sqlalchemy import JSON , String , TypeDecorator
@@ -105,16 +106,16 @@ class ProvenancePayload(TypeDecorator): # pylint: disable=W0223
105
106
"""SQLAlchemy column type to serialize InTotoProvenance."""
106
107
107
108
# It is stored in the database as a json value.
108
- impl = JSON
109
+ impl = String
109
110
110
111
# To prevent Sphinx from rendering the docstrings for `cache_ok`, make this docstring private.
111
112
#: :meta private:
112
113
cache_ok = True
113
114
114
- def process_bind_param (self , value : None | InTotoPayload , dialect : Any ) -> None | dict :
115
+ def process_bind_param (self , value : None | InTotoPayload , dialect : Any ) -> str | None :
115
116
"""Process when storing an InTotoPayload object to the SQLite db.
116
117
117
- value: None | InTotoPayload
118
+ value: InTotoPayload | None
118
119
The value being stored.
119
120
"""
120
121
if value is None :
@@ -124,27 +125,33 @@ def process_bind_param(self, value: None | InTotoPayload, dialect: Any) -> None
124
125
raise TypeError ("ProvenancePayload type expects an InTotoPayload." )
125
126
126
127
payload_type = value .__class__ .__name__
127
- return {"payload_type" : payload_type , "payload" : value .statement }
128
+ payload_dict = {"payload_type" : payload_type , "payload" : value .statement }
129
+ return json .dumps (payload_dict )
128
130
129
- def process_result_value (self , value : None | dict , dialect : Any ) -> None | InTotoPayload :
131
+ def process_result_value (self , value : str | None , dialect : Any ) -> InTotoPayload | None :
130
132
"""Process when loading an InTotoPayload object from the SQLite db.
131
133
132
- value: None | dict
134
+ value: str | None
133
135
The value being loaded.
134
136
"""
135
137
if value is None :
136
138
return None
137
139
138
- if not isinstance (value , dict ):
139
- raise TypeError ("ProvenancePayload type expects a dict." )
140
+ try :
141
+ payload_dict = json .loads (value )
142
+ except ValueError as error :
143
+ raise TypeError (f"Error parsing str as JSON: { error } " ) from error
144
+
145
+ if not isinstance (payload_dict , dict ):
146
+ raise TypeError ("Parsed data is not a dict." )
140
147
141
- if "payload_type" not in value or "payload" not in value :
148
+ if "payload_type" not in payload_dict or "payload" not in payload_dict :
142
149
raise TypeError ("Missing keys in dict for ProvenancePayload type." )
143
150
144
- payload = value ["payload" ]
145
- if value ["payload_type" ] == "InTotoV01Payload" :
151
+ payload = payload_dict ["payload" ]
152
+ if payload ["payload_type" ] == "InTotoV01Payload" :
146
153
return InTotoV01Payload (statement = payload )
147
- if value ["payload_type" ] == "InTotoV1Payload" :
154
+ if payload ["payload_type" ] == "InTotoV1Payload" :
148
155
return InTotoV1Payload (statement = payload )
149
156
150
- return validate_intoto_payload (value )
157
+ return validate_intoto_payload (payload )
0 commit comments