Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

Commit f14abfb

Browse files
authored
protobuf: Translate unsigned integers to corresponding Word types (#246)
1 parent 678cd71 commit f14abfb

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

adapter/protobuf/src/Mu/Adapter/ProtoBuf.hs

+17
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Data.Int
4444
import Data.SOP (All)
4545
import qualified Data.Text as T
4646
import qualified Data.Text.Lazy as LT
47+
import Data.Word (Word64, Word32)
4748
import GHC.TypeLits
4849
import Proto3.Wire
4950
import qualified Proto3.Wire.Decode as PBDec
@@ -420,6 +421,22 @@ instance ProtoBridgeOneFieldValue sch ('TPrimitive Int64) where
420421
= PBEnc.packedVarints fid $ map (\(FPrimitive i) -> fromIntegral i) vs
421422
protoToPackedFieldValue = map FPrimitive <$> PBDec.packedVarints
422423

424+
instance ProtoBridgeOneFieldValue sch ('TPrimitive Word32) where
425+
defaultOneFieldValue = Just $ FPrimitive 0
426+
oneFieldValueToProto fid (FPrimitive n) = PBEnc.uint32 fid n
427+
protoToOneFieldValue = FPrimitive <$> PBDec.uint32
428+
supportsPacking _ = True
429+
packedFieldValueToProto fid vs = PBEnc.packedVarints fid $ map (\(FPrimitive i) -> fromIntegral i) vs
430+
protoToPackedFieldValue = map FPrimitive <$> PBDec.packedVarints
431+
432+
instance ProtoBridgeOneFieldValue sch ('TPrimitive Word64) where
433+
defaultOneFieldValue = Just $ FPrimitive 0
434+
oneFieldValueToProto fid (FPrimitive n) = PBEnc.uint64 fid n
435+
protoToOneFieldValue = FPrimitive <$> PBDec.uint64
436+
supportsPacking _ = True
437+
packedFieldValueToProto fid vs = PBEnc.packedVarints fid $ map (\(FPrimitive i) -> i) vs
438+
protoToPackedFieldValue = map FPrimitive <$> PBDec.packedVarints
439+
423440
-- WARNING! These instances may go out of bounds
424441
instance ProtoBridgeOneFieldValue sch ('TPrimitive Integer) where
425442
defaultOneFieldValue = Just $ FPrimitive 0

adapter/protobuf/src/Mu/Quasi/ProtoBuf.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Control.Monad (when)
2222
import Control.Monad.IO.Class
2323
import qualified Data.ByteString as B
2424
import Data.Int
25+
import Data.Word
2526
import qualified Data.Text as T
2627
import Language.Haskell.TH
2728
import Language.ProtocolBuffers.Parser
@@ -112,10 +113,10 @@ pbTypeDeclToType (P.DMessage name _ _ fields _) = do
112113

113114
pbFieldTypeToType :: P.FieldType -> Q Type
114115
pbFieldTypeToType P.TInt32 = [t|'TPrimitive Int32|]
115-
pbFieldTypeToType P.TUInt32 = fail "unsigned integers are not currently supported"
116+
pbFieldTypeToType P.TUInt32 = [t|'TPrimitive Word32|]
116117
pbFieldTypeToType P.TSInt32 = [t|'TPrimitive Int32|]
117118
pbFieldTypeToType P.TInt64 = [t|'TPrimitive Int64|]
118-
pbFieldTypeToType P.TUInt64 = fail "unsigned integers are not currently supported"
119+
pbFieldTypeToType P.TUInt64 = [t|'TPrimitive Word64|]
119120
pbFieldTypeToType P.TSInt64 = [t|'TPrimitive Int64|]
120121
pbFieldTypeToType P.TFixed32 = fail "fixed integers are not currently supported"
121122
pbFieldTypeToType P.TFixed64 = fail "fixed integers are not currently supported"

0 commit comments

Comments
 (0)