protobuf 0.1.1 → 0.1.2
raw patch · 3 files changed
+25/−13 lines, 3 files
Files
- protobuf.cabal +1/−1
- src/Data/ProtocolBuffers/Wire.hs +8/−8
- tests/Main.hs +16/−4
protobuf.cabal view
@@ -1,5 +1,5 @@ name: protobuf-version: 0.1.1+version: 0.1.2 synopsis: Google Protocol Buffers via GHC.Generics description: Google Protocol Buffers via GHC.Generics.
src/Data/ProtocolBuffers/Wire.hs view
@@ -427,20 +427,20 @@ xs <- decodePackedList getVarInt x return . PackedList $ toEnum <$> xs -instance (Foldable f, Enum a) => EncodeWire (Enumeration (f a)) where- encodeWire t = traverse_ (encodeWire t . c) . runEnumeration where+instance (Foldable f, Enum a) => EncodeWire (f (Enumeration a)) where+ encodeWire t = traverse_ (encodeWire t . c . runEnumeration) where c :: a -> Int32 c = fromIntegral . fromEnum -instance Enum a => DecodeWire (Enumeration (Maybe a)) where+instance Enum a => DecodeWire (Maybe (Enumeration a)) where decodeWire f = c <$> decodeWire f where- c :: Int32 -> Enumeration (Maybe a)- c = Enumeration . Just . toEnum . fromIntegral+ c :: Int32 -> Maybe (Enumeration a)+ c = Just . Enumeration . toEnum . fromIntegral -instance Enum a => DecodeWire (Enumeration (Always a)) where+instance Enum a => DecodeWire (Always (Enumeration a)) where decodeWire f = c <$> decodeWire f where- c :: Int32 -> Enumeration (Always a)- c = Enumeration . Always . toEnum . fromIntegral+ c :: Int32 -> Always (Enumeration a)+ c = Always . Enumeration . toEnum . fromIntegral -- Taken from google's code, but I had to explcitly add fromIntegral in the right places: zzEncode32 :: Int32 -> Word32
tests/Main.hs view
@@ -1,4 +1,5 @@ {-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}@@ -44,6 +45,12 @@ main :: IO () main = defaultMain tests +data EnumFoo+ = EnumFoo1+ | EnumFoo2+ | EnumFoo3+ deriving (Bounded, Enum, Eq, Typeable)+ tests = [ testGroup "Primitive Wire" primitiveWireTests , testGroup "Packed Wire" packedWireTests@@ -74,6 +81,7 @@ , testProperty "float" (f (Proxy :: Proxy Float)) , testProperty "double" (f (Proxy :: Proxy Double)) , testProperty "bool" (f (Proxy :: Proxy Bool))+ , testProperty "enum" (f (Proxy :: Proxy (Always (Enumeration EnumFoo)))) ] primitiveWireTests :: [Test]@@ -129,10 +137,18 @@ arbitrary = Value <$> arbitrary shrink (Value x) = fmap Value $ shrink x +instance (Bounded a, Enum a) => Arbitrary (Enumeration a) where+ arbitrary = Enumeration <$> elements [minBound..maxBound]+ shrink (Enumeration x) = Enumeration . toEnum <$> shrink (fromEnum x)+ instance Arbitrary a => Arbitrary (Pb.Fixed a) where arbitrary = Pb.Fixed <$> arbitrary shrink (Pb.Fixed x) = fmap Pb.Fixed $ shrink x +instance Arbitrary a => Arbitrary (Always a) where+ arbitrary = Always <$> arbitrary+ shrink (Always x) = Always <$> shrink x+ instance Arbitrary WireField where arbitrary = do tag <- choose (0, 536870912)@@ -362,7 +378,3 @@ assert $ 856912304801416 == rt64 856912304801416 assert $ (-75123905439571256) == rt64 (-75123905439571256)--data Test5 a = Test5{test5_e :: Required D3 (Message a)} deriving Generic -- (Generic, Eq, Show)-instance (Monoid (Message a), Encode a) => Encode (Test5 a)-instance (Monoid (Message a), Decode a) => Decode (Test5 a)