Skip to content

Commit b882cbb

Browse files
committed
Better support @value key
1 parent eb493fa commit b882cbb

File tree

2 files changed

+60
-40
lines changed

2 files changed

+60
-40
lines changed

cardano-db-sync/src/Cardano/DbSync/OffChain.hs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ fetchOffChainVoteData _tracer manager time oVoteWorkQ =
274274
vdt =
275275
DB.OffChainVoteData
276276
{ DB.offChainVoteDataLanguage = Vote.getLanguage offChainData
277-
, DB.offChainVoteDataComment = Vote.comment minimalBody
277+
, DB.offChainVoteDataComment = Vote.textValue <$> Vote.comment minimalBody
278278
, DB.offChainVoteDataTitle = Vote.getTitle offChainData
279279
, DB.offChainVoteDataAbstract = Vote.getAbstract offChainData
280280
, DB.offChainVoteDataMotivation = Vote.getMotivation offChainData
@@ -302,27 +302,27 @@ fetchOffChainVoteData _tracer manager time oVoteWorkQ =
302302
mkAuthor ocvdId au =
303303
DB.OffChainVoteAuthor
304304
{ DB.offChainVoteAuthorOffChainVoteDataId = ocvdId
305-
, DB.offChainVoteAuthorName = Vote.name au
306-
, DB.offChainVoteAuthorWitnessAlgorithm = Vote.witnessAlgorithm $ Vote.witness au
307-
, DB.offChainVoteAuthorPublicKey = Vote.publicKey $ Vote.witness au
308-
, DB.offChainVoteAuthorSignature = Vote.signature $ Vote.witness au
305+
, DB.offChainVoteAuthorName = Vote.textValue <$> Vote.name au
306+
, DB.offChainVoteAuthorWitnessAlgorithm = Vote.textValue $ Vote.witnessAlgorithm $ Vote.witness au
307+
, DB.offChainVoteAuthorPublicKey = Vote.textValue $ Vote.publicKey $ Vote.witness au
308+
, DB.offChainVoteAuthorSignature = Vote.textValue $ Vote.signature $ Vote.witness au
309309
, DB.offChainVoteAuthorWarning = Just "Failed to validate this signature" -- TODO: Conway
310310
}
311311

312312
mkReference ocvdId ref =
313313
DB.OffChainVoteReference
314314
{ DB.offChainVoteReferenceOffChainVoteDataId = ocvdId
315-
, DB.offChainVoteReferenceLabel = Vote.label ref
316-
, DB.offChainVoteReferenceUri = Vote.uri ref
317-
, DB.offChainVoteReferenceHashDigest = Vote.hashDigest <$> Vote.referenceHash ref
318-
, DB.offChainVoteReferenceHashAlgorithm = Vote.rhHashAlgorithm <$> Vote.referenceHash ref
315+
, DB.offChainVoteReferenceLabel = Vote.textValue $ Vote.label ref
316+
, DB.offChainVoteReferenceUri = Vote.textValue $ Vote.uri ref
317+
, DB.offChainVoteReferenceHashDigest = Vote.textValue . Vote.hashDigest <$> Vote.referenceHash ref
318+
, DB.offChainVoteReferenceHashAlgorithm = Vote.textValue . Vote.rhHashAlgorithm <$> Vote.referenceHash ref
319319
}
320320

321321
mkexternalUpdates ocvdId eupd =
322322
DB.OffChainVoteExternalUpdate
323323
{ DB.offChainVoteExternalUpdateOffChainVoteDataId = ocvdId
324-
, DB.offChainVoteExternalUpdateTitle = Vote.euTitle eupd
325-
, DB.offChainVoteExternalUpdateUri = Vote.euUri eupd
324+
, DB.offChainVoteExternalUpdateTitle = Vote.textValue $ Vote.euTitle eupd
325+
, DB.offChainVoteExternalUpdateUri = Vote.textValue $ Vote.euUri eupd
326326
}
327327

328328
mListToList :: Maybe [a] -> [a]

cardano-db-sync/src/Cardano/DbSync/OffChain/Vote/Types.hs

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ getMinimalBody = \case
3131
getTitle :: OffChainVoteData -> Maybe Text
3232
getTitle = \case
3333
OffChainVoteDataOther _ -> Nothing
34-
OffChainVoteDataGa ga -> Just $ title $ body ga
34+
OffChainVoteDataGa ga -> Just $ textValue $ title $ body ga
3535

3636
getAbstract :: OffChainVoteData -> Maybe Text
3737
getAbstract = \case
3838
OffChainVoteDataOther _ -> Nothing
39-
OffChainVoteDataGa ga -> Just $ abstract $ body ga
39+
OffChainVoteDataGa ga -> Just $ textValue $ abstract $ body ga
4040

4141
getMotivation :: OffChainVoteData -> Maybe Text
4242
getMotivation = \case
4343
OffChainVoteDataOther _ -> Nothing
44-
OffChainVoteDataGa ga -> Just $ motivation $ body ga
44+
OffChainVoteDataGa ga -> Just $ textValue $ motivation $ body ga
4545

4646
getRationale :: OffChainVoteData -> Maybe Text
4747
getRationale = \case
4848
OffChainVoteDataOther _ -> Nothing
49-
OffChainVoteDataGa ga -> Just $ rationale $ body ga
49+
OffChainVoteDataGa ga -> Just $ textValue $ rationale $ body ga
5050

5151
eitherDecodeOffChainVoteData :: LBS.ByteString -> Bool -> Either String OffChainVoteData
5252
eitherDecodeOffChainVoteData lbs isGa
@@ -60,70 +60,75 @@ getAuthors = \case
6060

6161
getHashAlgorithm :: OffChainVoteData -> Text
6262
getHashAlgorithm = \case
63-
OffChainVoteDataOther dt -> hashAlgorithm dt
64-
OffChainVoteDataGa dt -> hashAlgorithm dt
63+
OffChainVoteDataOther dt -> textValue $ hashAlgorithm dt
64+
OffChainVoteDataGa dt -> textValue $ hashAlgorithm dt
6565

6666
getLanguage :: OffChainVoteData -> Text
6767
getLanguage = \case
6868
OffChainVoteDataOther dt -> language $ context dt
6969
OffChainVoteDataGa dt -> language $ context dt
7070

7171
data OffChainVoteDataTp tp = OffChainVoteDataTp
72-
{ hashAlgorithm :: Text
72+
{ hashAlgorithm :: TextValue
7373
, authors :: [Author]
7474
, body :: Body tp
7575
, context :: Context
7676
}
7777

78+
newtype TextValue = TextValue {textValue :: Text}
79+
80+
instance Show TextValue where
81+
show = show . textValue
82+
7883
deriving instance (Show (Body tp)) => Show (OffChainVoteDataTp tp)
7984
deriving instance Generic (OffChainVoteDataTp tp)
8085

8186
data Author = Author
82-
{ name :: Maybe Text
87+
{ name :: Maybe TextValue
8388
, witness :: Witness
8489
}
8590
deriving (Show, Generic, FromJSON)
8691

8792
data Witness = Witness
88-
{ witnessAlgorithm :: Text
89-
, publicKey :: Text
90-
, signature :: Text
93+
{ witnessAlgorithm :: TextValue
94+
, publicKey :: TextValue
95+
, signature :: TextValue
9196
}
9297
deriving (Show, Generic, FromJSON)
9398

9499
data MinimalBody = Body
95100
{ references :: Maybe [Reference]
96-
, comment :: Maybe Text
101+
, comment :: Maybe TextValue
97102
, externalUpdates :: Maybe [ExternalUpdate]
98103
}
99104
deriving (Show, Generic, FromJSON)
100105

101106
data GABody = GABody
102107
{ minimalBody :: MinimalBody
103-
, title :: Text -- 80 chars max
104-
, abstract :: Text -- 2500 chars
105-
, motivation :: Text
106-
, rationale :: Text
108+
, title :: TextValue -- 80 chars max
109+
, abstract :: TextValue -- 2500 chars
110+
, motivation :: TextValue
111+
, rationale :: TextValue
107112
}
108113
deriving (Show)
109114

110115
data Reference = Reference
111-
{ rtype :: Text -- key is @type. It can be "GovernanceMetadata" or "Other" or ?? "other" ??
112-
, label :: Text
113-
, uri :: Text
116+
{ rtype :: TextValue -- key is @type. It can be "GovernanceMetadata" or "Other" or ?? "other" ??
117+
, label :: TextValue
118+
, uri :: TextValue
114119
, referenceHash :: Maybe ReferenceHash
115120
}
116121
deriving (Show, Generic)
117122

118123
data ReferenceHash = ReferenceHash
119-
{ hashDigest :: Text
120-
, rhHashAlgorithm :: Text -- key 'hashAlgorithm'
124+
{ hashDigest :: TextValue
125+
, rhHashAlgorithm :: TextValue -- key 'hashAlgorithm'
121126
}
122127
deriving (Show, Generic)
123128

124129
data ExternalUpdate = ExternalUpdate
125-
{ euTitle :: Text -- key 'title'
126-
, euUri :: Text -- key 'uri'
130+
{ euTitle :: TextValue -- key 'title'
131+
, euUri :: TextValue -- key 'uri'
127132
}
128133
deriving (Show, Generic)
129134

@@ -167,17 +172,17 @@ instance FromJSON ReferenceHash where
167172
<$> o .: "hashDigest"
168173
<*> o .: "hashAlgorithm"
169174

170-
parseRefType :: Object -> Parser Text
175+
parseRefType :: Object -> Parser TextValue
171176
parseRefType obj = do
172177
tp <- obj .: "@type"
173-
if tp `elem` ["GovernanceMetadata", "Other", "other"]
178+
if textValue tp `elem` ["GovernanceMetadata", "Other", "other"]
174179
then pure tp
175180
else
176181
fail $
177182
mconcat
178183
[ "reference type should be GovernanceMetadata or Other other"
179184
, " but it's "
180-
, Text.unpack tp
185+
, Text.unpack (textValue tp)
181186
]
182187

183188
instance FromJSON ExternalUpdate where
@@ -199,10 +204,10 @@ instance FromJSON GABody where
199204
where
200205
withObjectV v' s p = withObject s p v'
201206

202-
parseTextLimit :: Int -> Key -> Object -> Parser Text
207+
parseTextLimit :: Int -> Key -> Object -> Parser TextValue
203208
parseTextLimit maxSize str o = do
204209
txt <- o .: str
205-
if Text.length txt <= maxSize
210+
if Text.length (textValue txt) <= maxSize
206211
then pure txt
207212
else
208213
fail $
@@ -211,7 +216,7 @@ parseTextLimit maxSize str o = do
211216
, " must have at most "
212217
, show maxSize
213218
, "characters, but it has "
214-
, show (Text.length txt)
219+
, show (Text.length (textValue txt))
215220
, " characters."
216221
]
217222

@@ -228,3 +233,18 @@ instance FromJSON Context where
228233
withObject "Context" $ \o ->
229234
Context
230235
<$> o .: "@language"
236+
237+
instance FromJSON TextValue where
238+
parseJSON v = case v of
239+
String txt -> pure $ TextValue txt
240+
Object o -> TextValue <$> (o .: "@value")
241+
_ -> fail $ "expected String or Object with @value but encountered " ++ typeOf v
242+
243+
typeOf :: Value -> String
244+
typeOf v = case v of
245+
Object _ -> "Object"
246+
Array _ -> "Array"
247+
String _ -> "String"
248+
Number _ -> "Number"
249+
Bool _ -> "Boolean"
250+
Null -> "Null"

0 commit comments

Comments
 (0)