Skip to content

Commit

Permalink
Add fromMaybeTable (#179)
Browse files Browse the repository at this point in the history
A little helper function for `MaybeTable`s analogous to `Data.Maybe.fromMaybe`.
  • Loading branch information
shane-circuithub committed Jun 9, 2022
1 parent bdcfd29 commit 4b4cce9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Rel8.hs
Expand Up @@ -58,6 +58,7 @@ module Rel8
, MaybeTable
, maybeTable, ($?), nothingTable, justTable
, isNothingTable, isJustTable
, fromMaybeTable
, optional
, catMaybeTable
, traverseMaybeTable
Expand Down Expand Up @@ -114,7 +115,7 @@ module Rel8
, isNullTable, isNonNullTable
, catNullTable
, nameNullTable
, fromMaybeTable, toMaybeTable
, toNullTable, toMaybeTable

-- ** Algebraic data types / sum types
-- $adts
Expand Down
6 changes: 6 additions & 0 deletions src/Rel8/Table/Maybe.hs
Expand Up @@ -15,6 +15,7 @@ module Rel8.Table.Maybe
( MaybeTable(..)
, maybeTable, nothingTable, justTable
, isNothingTable, isJustTable
, fromMaybeTable
, ($?)
, aggregateMaybeTable
, nameMaybeTable
Expand Down Expand Up @@ -206,6 +207,11 @@ justTable :: a -> MaybeTable Expr a
justTable = MaybeTable mempty . pure


-- | 'Data.Maybe.fromMaybe' for 'MaybeTable's.
fromMaybeTable :: Table Expr a => a -> MaybeTable Expr a -> a
fromMaybeTable fallback = maybeTable fallback id


-- | Project a single expression out of a 'MaybeTable'. You can think of this
-- operator like the '$' operator, but it also has the ability to return
-- @null@.
Expand Down
6 changes: 3 additions & 3 deletions src/Rel8/Table/Null.hs
Expand Up @@ -13,7 +13,7 @@ module Rel8.Table.Null
, nullableTable, nullTable, nullifyTable, unsafeUnnullifyTable
, isNullTable, isNonNullTable
, nameNullTable
, toMaybeTable, fromMaybeTable
, toMaybeTable, toNullTable
)
where

Expand Down Expand Up @@ -131,5 +131,5 @@ toMaybeTable = nullableTable nothingTable justTable

-- | Convert a 'MaybeTable' to a 'NullTable'. Note that if the underlying @a@
-- has no non-nullable fields, this is a lossy conversion.
fromMaybeTable :: Table Expr a => MaybeTable Expr a -> NullTable Expr a
fromMaybeTable = maybeTable nullTable nullifyTable
toNullTable :: Table Expr a => MaybeTable Expr a -> NullTable Expr a
toNullTable = maybeTable nullTable nullifyTable

0 comments on commit 4b4cce9

Please sign in to comment.