packages feed

wireform-proto-0.2.0.0: src/Proto/Google/Protobuf/Timestamp.hs

{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StrictData #-}
{-# OPTIONS_GHC -Wno-unused-imports -Wno-unused-matches -Wno-unused-top-binds #-}

{- | Auto-generated protobuf types from package @google.protobuf@.

__THIS FILE IS AUTO-GENERATED BY wireform. DO NOT EDIT.__

Any manual changes will be overwritten the next time code
generation is run.  To modify the types or instances, edit the
@.proto@ source file and re-run the code generator.
-}
module Proto.Google.Protobuf.Timestamp where

import Control.DeepSeq (NFData (..))
import Data.Aeson qualified as Aeson
import Data.Aeson.Key qualified as AesonKey
import Data.Aeson.KeyMap qualified as AesonKM
import Data.Aeson.Types qualified as Aeson
import Data.ByteString (ByteString)
import Data.ByteString qualified as BS
import Data.Hashable (Hashable (..))
import Data.Int (Int32, Int64)
import Data.Map.Strict qualified as Map
import Data.Proxy (Proxy (..))
import Data.Reflection (Given (..), given)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Vector qualified as V
import Data.Vector.Unboxed qualified as VU
import Data.Word (Word32, Word64)
import GHC.Generics (Generic)
import Proto.Decode
import Proto.Encode
import Proto.Extension qualified
import Proto.Internal.Encode.Archetype (
  archBool,
  archBoolSize,
  archBytes,
  archBytesSize,
  archDouble,
  archFixed32,
  archFixed32Size,
  archFixed64,
  archFixed64Size,
  archFloat,
  archSVarint32,
  archSVarint64,
  archString,
  archStringSize,
  archSubmessage,
  archSubmessageSize,
  archVarint,
  archVarintSize,
 )
import Proto.Internal.JSON (bytesFieldToJSON, bytesMapFieldToJSON, jsonObject, parseBytesFieldMaybe, parseBytesMapFieldMaybe, parseFieldMaybe, protoBytesToJSON, (.=:))
import Proto.Internal.JSON.Extension qualified as PJExt
import Proto.Internal.Wire (Tag (..), WireType (..))
import Proto.Internal.Wire.Encode (
  fieldBoolSize,
  fieldBytesSize,
  fieldDoubleSize,
  fieldFixed32Size,
  fieldFixed64Size,
  fieldFloatSize,
  fieldMessageSize,
  fieldSVarint32Size,
  fieldSVarint64Size,
  fieldTextSize,
  fieldVarintSize,
  putByteString,
  putDouble,
  putFixed32,
  putFixed64,
  putFloat,
  putLengthDelimited,
  putSVarint32,
  putSVarint64,
  putTag,
  putText,
  putVarint,
  putVarintSigned,
  tagSize,
  varintSize,
  varintSize32,
  zigZag32,
  zigZag64,
 )
import Proto.Registry (IsMessage)
import Proto.Registry qualified
import Proto.Schema (FieldDescriptor (..), FieldLabel' (..), FieldTypeDescriptor (..), ProtoMessage (..), ScalarFieldType (..), SomeFieldDescriptor (..))
import Wireform.Builder qualified as B


{- | Serialized FileDescriptorProto for this .proto file.
Decode with @Proto.Google.Protobuf.Descriptor.decodeMessage@.
-}
fileDescriptorProtoBytes :: ByteString
fileDescriptorProtoBytes = "\x0a\x1f\x67\x6f\x6f\x67\x6c\x65\x2f\x70\x72\x6f\x74\x6f\x62\x75\x66\x2f\x74\x69\x6d\x65\x73\x74\x61\x6d\x70\x2e\x70\x72\x6f\x74\x6f\x12\x0f\x67\x6f\x6f\x67\x6c\x65\x2e\x70\x72\x6f\x74\x6f\x62\x75\x66\x22\x2b\x0a\x09\x54\x69\x6d\x65\x73\x74\x61\x6d\x70\x12\x0f\x0a\x07\x73\x65\x63\x6f\x6e\x64\x73\x18\x01\x20\x01\x28\x03\x12\x0d\x0a\x05\x6e\x61\x6e\x6f\x73\x18\x02\x20\x01\x28\x05\x62\x06\x70\x72\x6f\x74\x6f\x33"


data Timestamp = Timestamp
  { timestampSeconds :: {-# UNPACK #-} !Int64
  , timestampNanos :: {-# UNPACK #-} !Int32
  , timestampUnknownFields :: ![UnknownField]
  }
  deriving stock (Show, Eq, Generic)
  deriving anyclass (NFData)


defaultTimestamp :: Timestamp
defaultTimestamp =
  Timestamp
    { timestampSeconds = 0
    , timestampNanos = 0
    , timestampUnknownFields = []
    }


instance MessageEncode Timestamp where
  buildMessage msg =
    (if msg.timestampSeconds == 0 then mempty else archVarint 8 (fromIntegral msg.timestampSeconds))
      <> (if msg.timestampNanos == 0 then mempty else archVarint 16 (fromIntegral msg.timestampNanos))
      <> encodeUnknownFields msg.timestampUnknownFields


instance MessageSize Timestamp where
  messageSize msg =
    (if msg.timestampSeconds == 0 then 0 else archVarintSize (fromIntegral msg.timestampSeconds))
      + (if msg.timestampNanos == 0 then 0 else archVarintSize (fromIntegral msg.timestampNanos))
      + unknownFieldsSize msg.timestampUnknownFields


instance MessageDecode Timestamp where
  {-# INLINE messageDecoder #-}
  messageDecoder = loop 0 0 []
    where
      loop acc_0 acc_1 acc_unknown_ =
        withTagM
          (pure (Timestamp {timestampSeconds = acc_0, timestampNanos = acc_1, timestampUnknownFields = reverse acc_unknown_}))
          ( \fn wt -> case fn of
              1 -> do
                v <- (fromIntegral <$> decodeFieldVarint)
                loop v acc_1 acc_unknown_
              2 -> do
                v <- (fromIntegral <$> decodeFieldVarint)
                loop acc_0 v acc_unknown_
              _ -> do
                uf <- captureUnknownField fn (toEnum wt)
                loop acc_0 acc_1 (uf : acc_unknown_)
          )


instance ProtoMessage Timestamp where
  protoMessageName _ = "google.protobuf.Timestamp"
  protoPackageName _ = "google.protobuf"
  protoDefaultValue = defaultTimestamp
  protoFileDescriptorBytes _ = fileDescriptorProtoBytes
  protoFieldDescriptors _ =
    Map.fromList
      [
        ( 1
        , SomeField
            FieldDescriptor
              { fdName = "seconds"
              , fdNumber = 1
              , fdTypeDesc = ScalarType Int64Field
              , fdLabel = LabelOptional
              , fdGet = timestampSeconds
              , fdSet = \v m -> m {timestampSeconds = v}
              }
        )
      ,
        ( 2
        , SomeField
            FieldDescriptor
              { fdName = "nanos"
              , fdNumber = 2
              , fdTypeDesc = ScalarType Int32Field
              , fdLabel = LabelOptional
              , fdGet = timestampNanos
              , fdSet = \v m -> m {timestampNanos = v}
              }
        )
      ]


instance IsMessage Timestamp


instance Aeson.ToJSON Timestamp where
  toJSON msg =
    let s = msg.timestampSeconds
        n = msg.timestampNanos
        (rawDays, remSec) = s `divMod` 86400
        (hours, remH) = remSec `divMod` 3600
        (mins, secs) = remH `divMod` 60
        z = rawDays + 719468
        era = (if z >= 0 then z else z - 146096) `div` 146097
        doe = z - era * 146097
        yoe = (doe - doe `div` 1460 + doe `div` 36524 - doe `div` 146096) `div` 365
        y = yoe + era * 400
        doy = doe - (365 * yoe + yoe `div` 4 - yoe `div` 100)
        mp = (5 * doy + 2) `div` 153
        d = doy - (153 * mp + 2) `div` 5 + 1
        m = mp + (if mp < 10 then 3 else -9)
        y' = y + (if m <= 2 then 1 else 0)
        pad2 x = let sx = T.pack (show (abs x)) in if T.length sx < 2 then T.pack "0" <> sx else sx
        pad4 x = let sx = T.pack (show (abs x)) in T.replicate (4 - T.length sx) (T.pack "0") <> sx
        pad9 x = let sx = T.pack (show (abs x)) in T.replicate (9 - T.length sx) (T.pack "0") <> sx
        nanoStr = if n == 0 then T.pack "" else T.pack "." <> dropTrailingZeros (pad9 (fromIntegral n))
        dropTrailingZeros t = case T.stripSuffix (T.pack "0") t of Just t' -> dropTrailingZeros t'; Nothing -> t
    in Aeson.String
        ( pad4 y'
            <> T.pack "-"
            <> pad2 (fromIntegral m)
            <> T.pack "-"
            <> pad2 (fromIntegral d)
            <> T.pack "T"
            <> pad2 hours
            <> T.pack ":"
            <> pad2 mins
            <> T.pack ":"
            <> pad2 secs
            <> nanoStr
            <> T.pack "Z"
        )


instance Aeson.FromJSON Timestamp where
  parseJSON (Aeson.String _) = pure defaultTimestamp
  parseJSON _ = fail "Expected RFC 3339 timestamp string"


instance Hashable Timestamp where
  hashWithSalt salt msg = hashWithSalt (hashWithSalt (salt) msg.timestampSeconds) msg.timestampNanos


instance Proto.Extension.HasExtensions Timestamp where
  messageUnknownFields = timestampUnknownFields
  setMessageUnknownFields !ufs msg = msg {timestampUnknownFields = ufs}


instance Semigroup Timestamp where
  a <> b =
    Timestamp
      { timestampSeconds = if b.timestampSeconds == 0 then a.timestampSeconds else b.timestampSeconds
      , timestampNanos = if b.timestampNanos == 0 then a.timestampNanos else b.timestampNanos
      , timestampUnknownFields = a.timestampUnknownFields <> b.timestampUnknownFields
      }


instance Monoid Timestamp where
  mempty = defaultTimestamp