@@ -31,22 +31,22 @@ getMinimalBody = \case
31
31
getTitle :: OffChainVoteData -> Maybe Text
32
32
getTitle = \ case
33
33
OffChainVoteDataOther _ -> Nothing
34
- OffChainVoteDataGa ga -> Just $ title $ body ga
34
+ OffChainVoteDataGa ga -> Just $ textValue $ title $ body ga
35
35
36
36
getAbstract :: OffChainVoteData -> Maybe Text
37
37
getAbstract = \ case
38
38
OffChainVoteDataOther _ -> Nothing
39
- OffChainVoteDataGa ga -> Just $ abstract $ body ga
39
+ OffChainVoteDataGa ga -> Just $ textValue $ abstract $ body ga
40
40
41
41
getMotivation :: OffChainVoteData -> Maybe Text
42
42
getMotivation = \ case
43
43
OffChainVoteDataOther _ -> Nothing
44
- OffChainVoteDataGa ga -> Just $ motivation $ body ga
44
+ OffChainVoteDataGa ga -> Just $ textValue $ motivation $ body ga
45
45
46
46
getRationale :: OffChainVoteData -> Maybe Text
47
47
getRationale = \ case
48
48
OffChainVoteDataOther _ -> Nothing
49
- OffChainVoteDataGa ga -> Just $ rationale $ body ga
49
+ OffChainVoteDataGa ga -> Just $ textValue $ rationale $ body ga
50
50
51
51
eitherDecodeOffChainVoteData :: LBS. ByteString -> Bool -> Either String OffChainVoteData
52
52
eitherDecodeOffChainVoteData lbs isGa
@@ -60,70 +60,75 @@ getAuthors = \case
60
60
61
61
getHashAlgorithm :: OffChainVoteData -> Text
62
62
getHashAlgorithm = \ case
63
- OffChainVoteDataOther dt -> hashAlgorithm dt
64
- OffChainVoteDataGa dt -> hashAlgorithm dt
63
+ OffChainVoteDataOther dt -> textValue $ hashAlgorithm dt
64
+ OffChainVoteDataGa dt -> textValue $ hashAlgorithm dt
65
65
66
66
getLanguage :: OffChainVoteData -> Text
67
67
getLanguage = \ case
68
68
OffChainVoteDataOther dt -> language $ context dt
69
69
OffChainVoteDataGa dt -> language $ context dt
70
70
71
71
data OffChainVoteDataTp tp = OffChainVoteDataTp
72
- { hashAlgorithm :: Text
72
+ { hashAlgorithm :: TextValue
73
73
, authors :: [Author ]
74
74
, body :: Body tp
75
75
, context :: Context
76
76
}
77
77
78
+ newtype TextValue = TextValue { textValue :: Text }
79
+
80
+ instance Show TextValue where
81
+ show = show . textValue
82
+
78
83
deriving instance (Show (Body tp )) => Show (OffChainVoteDataTp tp )
79
84
deriving instance Generic (OffChainVoteDataTp tp )
80
85
81
86
data Author = Author
82
- { name :: Maybe Text
87
+ { name :: Maybe TextValue
83
88
, witness :: Witness
84
89
}
85
90
deriving (Show , Generic , FromJSON )
86
91
87
92
data Witness = Witness
88
- { witnessAlgorithm :: Text
89
- , publicKey :: Text
90
- , signature :: Text
93
+ { witnessAlgorithm :: TextValue
94
+ , publicKey :: TextValue
95
+ , signature :: TextValue
91
96
}
92
97
deriving (Show , Generic , FromJSON )
93
98
94
99
data MinimalBody = Body
95
100
{ references :: Maybe [Reference ]
96
- , comment :: Maybe Text
101
+ , comment :: Maybe TextValue
97
102
, externalUpdates :: Maybe [ExternalUpdate ]
98
103
}
99
104
deriving (Show , Generic , FromJSON )
100
105
101
106
data GABody = GABody
102
107
{ 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
107
112
}
108
113
deriving (Show )
109
114
110
115
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
114
119
, referenceHash :: Maybe ReferenceHash
115
120
}
116
121
deriving (Show , Generic )
117
122
118
123
data ReferenceHash = ReferenceHash
119
- { hashDigest :: Text
120
- , rhHashAlgorithm :: Text -- key 'hashAlgorithm'
124
+ { hashDigest :: TextValue
125
+ , rhHashAlgorithm :: TextValue -- key 'hashAlgorithm'
121
126
}
122
127
deriving (Show , Generic )
123
128
124
129
data ExternalUpdate = ExternalUpdate
125
- { euTitle :: Text -- key 'title'
126
- , euUri :: Text -- key 'uri'
130
+ { euTitle :: TextValue -- key 'title'
131
+ , euUri :: TextValue -- key 'uri'
127
132
}
128
133
deriving (Show , Generic )
129
134
@@ -167,17 +172,17 @@ instance FromJSON ReferenceHash where
167
172
<$> o .: " hashDigest"
168
173
<*> o .: " hashAlgorithm"
169
174
170
- parseRefType :: Object -> Parser Text
175
+ parseRefType :: Object -> Parser TextValue
171
176
parseRefType obj = do
172
177
tp <- obj .: " @type"
173
- if tp `elem` [" GovernanceMetadata" , " Other" , " other" ]
178
+ if textValue tp `elem` [" GovernanceMetadata" , " Other" , " other" ]
174
179
then pure tp
175
180
else
176
181
fail $
177
182
mconcat
178
183
[ " reference type should be GovernanceMetadata or Other other"
179
184
, " but it's "
180
- , Text. unpack tp
185
+ , Text. unpack (textValue tp)
181
186
]
182
187
183
188
instance FromJSON ExternalUpdate where
@@ -199,10 +204,10 @@ instance FromJSON GABody where
199
204
where
200
205
withObjectV v' s p = withObject s p v'
201
206
202
- parseTextLimit :: Int -> Key -> Object -> Parser Text
207
+ parseTextLimit :: Int -> Key -> Object -> Parser TextValue
203
208
parseTextLimit maxSize str o = do
204
209
txt <- o .: str
205
- if Text. length txt <= maxSize
210
+ if Text. length (textValue txt) <= maxSize
206
211
then pure txt
207
212
else
208
213
fail $
@@ -211,7 +216,7 @@ parseTextLimit maxSize str o = do
211
216
, " must have at most "
212
217
, show maxSize
213
218
, " characters, but it has "
214
- , show (Text. length txt)
219
+ , show (Text. length (textValue txt) )
215
220
, " characters."
216
221
]
217
222
@@ -228,3 +233,18 @@ instance FromJSON Context where
228
233
withObject " Context" $ \ o ->
229
234
Context
230
235
<$> 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