1
1
mirror of https://github.com/github/semantic.git synced 2025-01-03 13:02:37 +03:00

Introduce Present and Absent pattern synonyms for Nested.

This commit is contained in:
Patrick Thomson 2019-01-11 11:45:23 -05:00
parent a4bd06f6be
commit 5b73e89932
4 changed files with 19 additions and 11 deletions

View File

@ -5,6 +5,7 @@ module Data.GitHub.Event.RepositoryPush where
import Prologue
import Proto3.Suite
import Proto3.Suite.Exts
import qualified Data.GitHub.Git as Git
import Data.GitHub.Request.Context
@ -40,7 +41,7 @@ data RepositoryPush = RepositoryPush
samplePush :: RepositoryPush
samplePush = RepositoryPush
{ pushRequestContext = lowerBound
{ pushRequestContext = Absent
, pushActor = pure monalisa
, pushRepository = pure sampleRepository
, pushBefore = Git.SHA "308cf87a25e096a71be8aba8bb53f5575a60c388"

View File

@ -46,7 +46,7 @@ sampleRepository = Repository
, repoParentId = 0
, repoStargazerCount = 1000
, repoPublicForkCount = 777
, repoPushedAt = lowerBound
, repoCreatedAt = lowerBound
, repoUpdatedAt = lowerBound
, repoPushedAt = Absent
, repoCreatedAt = Absent
, repoUpdatedAt = Absent
}

View File

@ -36,7 +36,7 @@ monalisa = User
, userBillingPlan = "medium"
, userSpammy = False
, userGlobalRelayId = "global"
, userCreatedAt = lowerBound
, userCreatedAt = Absent
, userSuspended = False
, userSpamuraiCalculation = Hammy
, userAnalyticsTrackingId = "MACHHOMMY"

View File

@ -1,18 +1,25 @@
{-# LANGUAGE GeneralizedNewtypeDeriving, DerivingStrategies, DerivingVia, ScopedTypeVariables, UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE GeneralizedNewtypeDeriving, DerivingStrategies, DerivingVia, PatternSynonyms, ScopedTypeVariables, UndecidableInstances #-}
module Proto3.Suite.Exts
( PrimitiveEnum (..)
, pattern Present
, pattern Absent
) where
import Prologue
import Data.Either
import Proto3.Suite
import Proto3.Wire.Encode as Encode
import Proto3.Wire.Decode as Decode
instance Lower (Nested a) where
lowerBound = Nested Nothing
pattern Present :: a -> Nested a
pattern Present t = Nested (Just t)
pattern Absent :: Nested a
pattern Absent = Nested Nothing
{-# COMPLETE Present, Absent #-}
newtype PrimitiveEnum a = PrimitiveEnum a
deriving stock (Eq, Ord)
@ -21,7 +28,7 @@ newtype PrimitiveEnum a = PrimitiveEnum a
-- | Provides a DerivingVia hook to opt into a sensible definition of 'Primitive'
-- for a given 'Enum'. Should the decoding fail, the 'HasDefault' instance is used
-- as a fallback.
instance forall a. (Enum a, Bounded a, Named a, HasDefault a) => Primitive (PrimitiveEnum a) where
instance (Enum a, Bounded a, Named a, HasDefault a) => Primitive (PrimitiveEnum a) where
primType _ = Named (Single (nameOf (Proxy @a)))
encodePrimitive = Encode.enum
decodePrimitive = either (const def) id <$> Decode.enum
decodePrimitive = fromRight def <$> Decode.enum