Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive Data and Generic instances for FieldNumber #91

Merged
merged 2 commits into from
Dec 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/Proto3/Wire/Types.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

{-
Expand Down Expand Up @@ -27,23 +29,26 @@ module Proto3.Wire.Types
) where

import Control.DeepSeq ( NFData )
import Data.Data ( Data )
import Data.Hashable ( Hashable )
import Data.Word ( Word64 )
import GHC.Generics ( Generic )
import Test.QuickCheck ( Arbitrary(..), choose )

-- | A 'FieldNumber' identifies a field inside a protobufs message.
--
-- This library makes no attempt to generate these automatically, or even make
-- sure that field numbers are provided in increasing order. Such things are
-- left to other, higher-level libraries.
newtype FieldNumber = FieldNumber { getFieldNumber :: Word64 }
deriving (Eq, Ord, Enum, Hashable, NFData, Num)
newtype FieldNumber = FieldNumber
{ getFieldNumber :: Word64 }
deriving (Bounded, Data, Enum, Eq, Generic, Hashable, NFData, Num, Ord)

instance Show FieldNumber where
show (FieldNumber n) = show n
show (FieldNumber n) = show n

instance Arbitrary FieldNumber where
arbitrary = fmap FieldNumber $ choose (1, 536870911)
arbitrary = FieldNumber <$> choose (1, 536870911)

-- | Create a 'FieldNumber' given the (one-based) integer which would label
-- the field in the corresponding .proto file.
Expand All @@ -52,5 +57,9 @@ fieldNumber = FieldNumber

-- | The (non-deprecated) wire types identified by the Protocol
-- Buffers specification.
data WireType = Varint | Fixed32 | Fixed64 | LengthDelimited
deriving (Show, Eq, Ord)
data WireType
= Varint
| Fixed32
| Fixed64
| LengthDelimited
deriving (Bounded, Data, Enum, Eq, Generic, Ord, Show)