store missing port as empty string instead of NULL (#280)

This commit is contained in:
Evgeny Poberezkin
2022-01-11 16:01:09 +00:00
committed by GitHub
parent b5cb5618c1
commit 083d39be22
11 changed files with 48 additions and 35 deletions
+12 -4
View File
@@ -11,6 +11,8 @@ module Simplex.Messaging.Encoding
( Encoding (..),
Tail (..),
Large (..),
smpEncodeList,
smpListP,
)
where
@@ -111,12 +113,18 @@ instance (Encoding a, Encoding b) => Encoding (a, b) where
smpP = (,) <$> smpP <*> smpP
-- lists encode/parse as a sequence of items prefixed with list length (as 1 byte)
instance Encoding a => Encoding [a] where
smpEncode xs = B.cons (lenEncode $ length xs) . B.concat $ map smpEncode xs
smpP = (`A.count` smpP) =<< lenP
smpEncodeList :: Encoding a => [a] -> ByteString
smpEncodeList xs = B.cons (lenEncode $ length xs) . B.concat $ map smpEncode xs
smpListP :: Encoding a => Parser [a]
smpListP = (`A.count` smpP) =<< lenP
instance Encoding String where
smpEncode = smpEncode . B.pack
smpP = B.unpack <$> smpP
instance Encoding a => Encoding (L.NonEmpty a) where
smpEncode = smpEncode . L.toList
smpEncode = smpEncodeList . L.toList
smpP =
lenP >>= \case
0 -> fail "empty list"