Skip to content

Commit

Permalink
UUID: Quote the output of show
Browse files Browse the repository at this point in the history
Some packages (e.g., `pretty-simple`) expect strings to contain the
output of `stock`-derived `show`. Using UUIDs with such libraries
necessitates wrangling which can be easily avoided.
  • Loading branch information
endgame committed Aug 26, 2024
1 parent 68d7fe2 commit 7478f0b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
5 changes: 5 additions & 0 deletions uuid-types/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.0.0

- Return a quoted string from the `Show` instance, to avoid confusing
packages which try to parse stock-derived `show` output.

## 1.0.6 (2023-04-16)

- Support GHC-8.6.5..GHC-9.10.1
Expand Down
13 changes: 7 additions & 6 deletions uuid-types/src/Data/UUID/Types/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -512,14 +512,15 @@ instance Hashable UUID where
-- "00000000-0000-0000-0000-000000000000"
--
instance Show UUID where
show = toString
show = show . toString

instance Read UUID where
readsPrec _ str =
let noSpaces = dropWhile isSpace str
in case fromString (take 36 noSpaces) of
Nothing -> []
Just u -> [(u,drop 36 noSpaces)]
readsPrec p quotedStr = do
(str, rest) <- readsPrec p quotedStr
let noSpaces = dropWhile isSpace str
case fromString (take 36 noSpaces) of
Nothing -> []
Just u -> [(u, rest)]

-- | This 'Storable' instance uses the memory layout as described in <http://tools.ietf.org/html/rfc4122 RFC 4122>, but in contrast to the 'Binary' instance, __the fields are stored in host byte order__.
instance Storable UUID where
Expand Down
2 changes: 1 addition & 1 deletion uuid-types/tests/TestUUID.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ test_Binary =
(BL8.pack "\xa5\xca\x85\x66\xd9\xc5\x48\x35\x99\xc8\xe1\xf1\x3e\x73\xb5\xe2") @=? encode inputUUID

inputUUID :: U.UUID
inputUUID = read "a5ca8566-d9c5-4835-99c8-e1f13e73b5e2"
inputUUID = read $ show "a5ca8566-d9c5-4835-99c8-e1f13e73b5e2"

prop_stringRoundTrip :: Test
prop_stringRoundTrip = testProperty "String round trip" stringRoundTrip
Expand Down
2 changes: 1 addition & 1 deletion uuid-types/uuid-types.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12
name: uuid-types
version: 1.0.6
version: 2.0.0
x-revision: 1
copyright:
(c) 2017-2018 Herbert Valerio Riedel
Expand Down

0 comments on commit 7478f0b

Please sign in to comment.