{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
module Data.KVITable
(
KVITable(KVITable)
, Key
, KeyVal
, KeyVals
, KeySpec
, fromList
, toList
, Data.KVITable.lookup
, keyVals
, keyValGen
, valueColName
, insert
, foldlInsert
, Data.KVITable.filter
, adjust
, adjustWithKey
, delete
, update
, updateWithKey
, rows
)
where
import Data.Function ( on )
import qualified Data.List as L
import qualified Data.Map as Map
import Data.Text ( Text )
import qualified GHC.Exts
import Lens.Micro ( Lens' )
data KVITable v = KVITable
{ forall v. KVITable v -> KeyVals
keyvals :: KeyVals
, forall v. KVITable v -> Key -> Key
keyvalGen :: Key -> KeyVal
, forall v. KVITable v -> Map KeySpec v
contents :: Map.Map KeySpec v
, forall v. KVITable v -> Key
valuecolName :: Text
}
instance Eq v => Eq (KVITable v) where
== :: KVITable v -> KVITable v -> Bool
(==) = forall a. Eq a => a -> a -> Bool
(==) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall v. KVITable v -> Map KeySpec v
contents
instance Show v => Show (KVITable v) where
show :: KVITable v -> String
show KVITable v
t = String
"KVITable {" forall a. Semigroup a => a -> a -> a
<>
String
" keyvals = " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show (forall v. KVITable v -> KeyVals
keyvals KVITable v
t) forall a. Semigroup a => a -> a -> a
<>
String
" contents = " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show (forall v. KVITable v -> Map KeySpec v
contents KVITable v
t) forall a. Semigroup a => a -> a -> a
<>
String
", valuecolName = " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show (forall v. KVITable v -> Key
valuecolName KVITable v
t) forall a. Semigroup a => a -> a -> a
<>
String
"}"
type Key = Text
type KeyVal = Text
type KeySpec = [ (Key, KeyVal ) ]
type KeyVals = [ (Key, [KeyVal]) ]
instance Semigroup (KVITable v) where
KVITable v
a <> :: KVITable v -> KVITable v -> KVITable v
<> KVITable v
b = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl forall v. KVITable v -> (KeySpec, v) -> KVITable v
foldlInsert
(forall a. Monoid a => a
mempty { valuecolName :: Key
valuecolName = forall v. KVITable v -> Key
valuecolName KVITable v
a
, keyvals :: KeyVals
keyvals = forall v. KVITable v -> KeyVals
keyvals KVITable v
a
})
(forall v. KVITable v -> [Item (KVITable v)]
toList KVITable v
b forall a. Semigroup a => a -> a -> a
<> forall v. KVITable v -> [Item (KVITable v)]
toList KVITable v
a)
instance Monoid (KVITable v) where
mempty :: KVITable v
mempty = KVITable { keyvals :: KeyVals
keyvals = forall a. Monoid a => a
mempty
, keyvalGen :: Key -> Key
keyvalGen = forall a b. a -> b -> a
const Key
""
, contents :: Map KeySpec v
contents = forall a. Monoid a => a
mempty
, valuecolName :: Key
valuecolName = Key
"Value"
}
instance Functor KVITable where
fmap :: forall a b. (a -> b) -> KVITable a -> KVITable b
fmap a -> b
f KVITable a
t = KVITable { contents :: Map KeySpec b
contents = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (forall v. KVITable v -> Map KeySpec v
contents KVITable a
t)
, keyvalGen :: Key -> Key
keyvalGen = forall v. KVITable v -> Key -> Key
keyvalGen KVITable a
t
, keyvals :: KeyVals
keyvals = forall v. KVITable v -> KeyVals
keyvals KVITable a
t
, valuecolName :: Key
valuecolName = forall v. KVITable v -> Key
valuecolName KVITable a
t
}
instance Foldable KVITable where
foldMap :: forall m a. Monoid m => (a -> m) -> KVITable a -> m
foldMap a -> m
f = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KVITable v -> Map KeySpec v
contents
instance Traversable KVITable where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> KVITable a -> f (KVITable b)
traverse a -> f b
f KVITable a
t = (\Map KeySpec b
c -> KVITable { contents :: Map KeySpec b
contents = Map KeySpec b
c
, valuecolName :: Key
valuecolName = forall v. KVITable v -> Key
valuecolName KVITable a
t
, keyvals :: KeyVals
keyvals = forall v. KVITable v -> KeyVals
keyvals KVITable a
t
, keyvalGen :: Key -> Key
keyvalGen = forall v. KVITable v -> Key -> Key
keyvalGen KVITable a
t
}
) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f (forall v. KVITable v -> Map KeySpec v
contents KVITable a
t)
instance GHC.Exts.IsList (KVITable v) where
type Item (KVITable v) = (KeySpec, v)
fromList :: [Item (KVITable v)] -> KVITable v
fromList = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl forall v. KVITable v -> (KeySpec, v) -> KVITable v
foldlInsert forall a. Monoid a => a
mempty
toList :: KVITable v -> [Item (KVITable v)]
toList = forall l. IsList l => l -> [Item l]
GHC.Exts.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KVITable v -> Map KeySpec v
contents
fromList :: [ GHC.Exts.Item (KVITable v) ] -> KVITable v
fromList :: forall v. [Item (KVITable v)] -> KVITable v
fromList = forall l. IsList l => [Item l] -> l
GHC.Exts.fromList
toList :: KVITable v -> [ GHC.Exts.Item (KVITable v) ]
toList :: forall v. KVITable v -> [Item (KVITable v)]
toList = forall l. IsList l => l -> [Item l]
GHC.Exts.toList
keyVals :: Lens' (KVITable v) KeyVals
keyVals :: forall v. Lens' (KVITable v) KeyVals
keyVals KeyVals -> f KeyVals
f KVITable v
t = (\KeyVals
kvs ->
KVITable v
t { keyvals :: KeyVals
keyvals = KeyVals
kvs
, contents :: Map KeySpec v
contents =
let inKVS :: KeySpec -> v -> Bool
inKVS KeySpec
spec v
_ = forall {t :: * -> *} {a} {a}.
(Foldable t, Eq a, Eq a) =>
[(a, a)] -> [(a, t a)] -> Bool
inkv KeySpec
spec KeyVals
kvs
inkv :: [(a, a)] -> [(a, t a)] -> Bool
inkv [] [] = Bool
True
inkv ((a
sk,a
sv):[(a, a)]
srs) ((a
k,t a
vs):[(a, t a)]
kv)
| a
sk forall a. Eq a => a -> a -> Bool
== a
k Bool -> Bool -> Bool
&& a
sv forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` t a
vs = [(a, a)] -> [(a, t a)] -> Bool
inkv [(a, a)]
srs [(a, t a)]
kv
inkv [(a, a)]
_ [(a, t a)]
_ = Bool
False
in forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey KeySpec -> v -> Bool
inKVS (forall v. KVITable v -> Map KeySpec v
contents KVITable v
t)
}
) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyVals -> f KeyVals
f (forall v. KVITable v -> KeyVals
keyvals KVITable v
t)
keyValGen :: Lens' (KVITable v) (Key -> KeyVal)
keyValGen :: forall v. Lens' (KVITable v) (Key -> Key)
keyValGen (Key -> Key) -> f (Key -> Key)
f KVITable v
t = (\Key -> Key
n -> KVITable v
t { keyvalGen :: Key -> Key
keyvalGen = Key -> Key
n } ) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Key -> Key) -> f (Key -> Key)
f (forall v. KVITable v -> Key -> Key
keyvalGen KVITable v
t)
valueColName :: Lens' (KVITable v) Text
valueColName :: forall v. Lens' (KVITable v) Key
valueColName Key -> f Key
f KVITable v
t = (\Key
n -> KVITable v
t { valuecolName :: Key
valuecolName = Key
n } ) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> f Key
f (forall v. KVITable v -> Key
valuecolName KVITable v
t)
lookup :: KeySpec -> KVITable v -> Maybe v
lookup :: forall v. KeySpec -> KVITable v -> Maybe v
lookup KeySpec
keyspec KVITable v
t = case forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup KeySpec
keyspec forall a b. (a -> b) -> a -> b
$ forall v. KVITable v -> Map KeySpec v
contents KVITable v
t of
Just v
v -> forall a. a -> Maybe a
Just v
v
Maybe v
Nothing ->
let ks :: KeySpec
ks = forall v. KVITable v -> KeySpec -> KeySpec
normalizeKeySpec KVITable v
t KeySpec
keyspec
in forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup KeySpec
ks forall a b. (a -> b) -> a -> b
$ forall v. KVITable v -> Map KeySpec v
contents KVITable v
t
normalizeKeySpec :: KVITable v -> KeySpec -> KeySpec
normalizeKeySpec :: forall v. KVITable v -> KeySpec -> KeySpec
normalizeKeySpec KVITable v
t KeySpec
keyspec =
let keyandval :: KeySpec -> (Key, [Key]) -> KeySpec
keyandval KeySpec
s (Key
k,[Key]
vs) = case forall a b. Eq a => a -> [(a, b)] -> Maybe b
L.lookup Key
k KeySpec
keyspec of
Just Key
v -> if Key
v forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Key]
vs then KeySpec
s forall a. Semigroup a => a -> a -> a
<> [(Key
k,Key
v)]
else KeySpec
s
Maybe Key
Nothing -> KeySpec
s forall a. Semigroup a => a -> a -> a
<> [(Key
k, forall v. KVITable v -> Key -> Key
keyvalGen KVITable v
t Key
k)]
in forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl KeySpec -> (Key, [Key]) -> KeySpec
keyandval [] (forall v. KVITable v -> KeyVals
keyvals KVITable v
t)
insert :: KeySpec -> v -> KVITable v -> KVITable v
insert :: forall v. KeySpec -> v -> KVITable v -> KVITable v
insert KeySpec
keyspec v
val KVITable v
t = forall v.
KVITable v
-> v -> KeyVals -> KeySpec -> KeySpec -> KeyVals -> KVITable v
endset KVITable v
t v
val (forall v. KVITable v -> KeyVals
keyvals KVITable v
t) KeySpec
keyspec [] []
remainingKeyValDefaults :: KVITable v -> [(Key,a)] -> KeySpec
remainingKeyValDefaults :: forall v a. KVITable v -> [(Key, a)] -> KeySpec
remainingKeyValDefaults KVITable v
t = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Key
k,a
_) -> (Key
k, forall v. KVITable v -> Key -> Key
keyvalGen KVITable v
t Key
k))
addDefVal :: KVITable v -> (Key, [KeyVal]) -> (Key, [KeyVal])
addDefVal :: forall v. KVITable v -> (Key, [Key]) -> (Key, [Key])
addDefVal KVITable v
t e :: (Key, [Key])
e@(Key
k,[Key]
vs) = if (forall v. KVITable v -> Key -> Key
keyvalGen KVITable v
t Key
k) forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Key]
vs
then (Key, [Key])
e
else (Key
k, forall v. KVITable v -> Key -> Key
keyvalGen KVITable v
t Key
k forall a. a -> [a] -> [a]
: [Key]
vs)
endset :: KVITable v -> v -> KeyVals -> KeySpec -> KeySpec -> KeyVals -> KVITable v
endset :: forall v.
KVITable v
-> v -> KeyVals -> KeySpec -> KeySpec -> KeyVals -> KVITable v
endset KVITable v
t v
val KeyVals
rkv [] KeySpec
tspec KeyVals
kvbld =
let spec :: KeySpec
spec = KeySpec
tspec forall a. Semigroup a => a -> a -> a
<> forall v a. KVITable v -> [(Key, a)] -> KeySpec
remainingKeyValDefaults KVITable v
t KeyVals
rkv
in KVITable v
t { contents :: Map KeySpec v
contents = forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert KeySpec
spec v
val (forall v. KVITable v -> Map KeySpec v
contents KVITable v
t)
, keyvals :: KeyVals
keyvals = KeyVals
kvbld forall a. Semigroup a => a -> a -> a
<> (forall v. KVITable v -> (Key, [Key]) -> (Key, [Key])
addDefVal KVITable v
t forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyVals
rkv)
}
endset KVITable v
t v
val [] KeySpec
spec KeySpec
tspec KeyVals
kvbld =
let spec' :: KeySpec
spec' = KeySpec
tspec forall a. Semigroup a => a -> a -> a
<> KeySpec
spec
keySpecElemToKeyVals :: (Key, Key) -> (Key, [Key])
keySpecElemToKeyVals (Key
k,Key
v) = (Key
k, if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(KeySpec, v)]
curTblList
then [Key
v]
else [Key
v, forall v. KVITable v -> Key -> Key
keyvalGen KVITable v
t Key
k])
keyvals' :: KeyVals
keyvals' = KeyVals
kvbld forall a. Semigroup a => a -> a -> a
<> ((Key, Key) -> (Key, [Key])
keySpecElemToKeyVals forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeySpec
spec)
curTblList :: [(KeySpec, v)]
curTblList = forall k a. Map k a -> [(k, a)]
Map.toList forall a b. (a -> b) -> a -> b
$ forall v. KVITable v -> Map KeySpec v
contents KVITable v
t
defaultsExtension :: KeySpec
defaultsExtension = forall v a. KVITable v -> [(Key, a)] -> KeySpec
remainingKeyValDefaults KVITable v
t KeySpec
spec
updTblList :: [(KeySpec, v)]
updTblList = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(KeySpec
ks,v
v) -> (KeySpec
ks forall a. Semigroup a => a -> a -> a
<> KeySpec
defaultsExtension, v
v)) [(KeySpec, v)]
curTblList
in KVITable v
t { contents :: Map KeySpec v
contents = forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert KeySpec
spec' v
val forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(KeySpec, v)]
updTblList
, keyvals :: KeyVals
keyvals = KeyVals
keyvals'
}
endset KVITable v
t v
val kvs :: KeyVals
kvs@((Key
k,[Key]
vs):KeyVals
rkvs) ((Key
sk,Key
sv):KeySpec
srs) KeySpec
tspec KeyVals
kvbld =
if Key
k forall a. Eq a => a -> a -> Bool
== Key
sk
then let kv' :: KeyVals
kv' = if Key
sv forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Key]
vs
then KeyVals
kvbld forall a. Semigroup a => a -> a -> a
<> [(Key
k, [Key]
vs)]
else KeyVals
kvbld forall a. Semigroup a => a -> a -> a
<> [(Key
k, Key
sv forall a. a -> [a] -> [a]
: [Key]
vs)]
in forall v.
KVITable v
-> v -> KeyVals -> KeySpec -> KeySpec -> KeyVals -> KVITable v
endset KVITable v
t v
val KeyVals
rkvs KeySpec
srs (KeySpec
tspec forall a. Semigroup a => a -> a -> a
<> [(Key
k,Key
sv)]) KeyVals
kv'
else
if Key
sk forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyVals
rkvs) Bool -> Bool -> Bool
&& Key
k forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeySpec
srs)
then forall v.
KVITable v
-> v -> KeyVals -> KeySpec -> KeySpec -> KeyVals -> KVITable v
endset KVITable v
t v
val KeyVals
kvs (KeySpec
srs forall a. Semigroup a => a -> a -> a
<> [(Key
sk,Key
sv)]) KeySpec
tspec KeyVals
kvbld
else
if forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyVals
kvs)) (forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeySpec
srs)
then forall v.
KVITable v
-> v -> KeyVals -> KeySpec -> KeySpec -> KeyVals -> KVITable v
endset KVITable v
t v
val KeyVals
kvs (KeySpec
srs forall a. Semigroup a => a -> a -> a
<> [(Key
sk,Key
sv)]) KeySpec
tspec KeyVals
kvbld
else
let defVal :: Key
defVal = forall v. KVITable v -> Key -> Key
keyvalGen KVITable v
t Key
k
vs' :: [Key]
vs' = if Key
defVal forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Key]
vs then [Key]
vs else (Key
defVal forall a. a -> [a] -> [a]
: [Key]
vs)
in forall v.
KVITable v
-> v -> KeyVals -> KeySpec -> KeySpec -> KeyVals -> KVITable v
endset KVITable v
t v
val KeyVals
rkvs ((Key
sk,Key
sv)forall a. a -> [a] -> [a]
:KeySpec
srs) (KeySpec
tspec forall a. Semigroup a => a -> a -> a
<> [(Key
k,Key
defVal)]) (KeyVals
kvbld forall a. Semigroup a => a -> a -> a
<> [(Key
k,[Key]
vs')])
foldlInsert :: KVITable v -> (KeySpec, v) -> KVITable v
foldlInsert :: forall v. KVITable v -> (KeySpec, v) -> KVITable v
foldlInsert KVITable v
t (KeySpec
k,v
v) = forall v. KeySpec -> v -> KVITable v -> KVITable v
insert KeySpec
k v
v KVITable v
t
filter :: ((KeySpec, v) -> Bool) -> KVITable v -> KVITable v
filter :: forall v. ((KeySpec, v) -> Bool) -> KVITable v -> KVITable v
filter (KeySpec, v) -> Bool
f KVITable v
t = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl KVITable v -> (KeySpec, v) -> KVITable v
chkInsert (forall {v} {v}. KVITable v -> KVITable v
emptyClone KVITable v
t) forall a b. (a -> b) -> a -> b
$ forall v. KVITable v -> [Item (KVITable v)]
toList KVITable v
t
where emptyClone :: KVITable v -> KVITable v
emptyClone KVITable v
o = KVITable v
o { contents :: Map KeySpec v
contents = forall a. Monoid a => a
mempty }
chkInsert :: KVITable v -> (KeySpec, v) -> KVITable v
chkInsert KVITable v
o (KeySpec
k,v
v) = if (KeySpec, v) -> Bool
f (KeySpec
k,v
v) then forall v. KeySpec -> v -> KVITable v -> KVITable v
insert KeySpec
k v
v KVITable v
o else KVITable v
o
delete :: KeySpec -> KVITable v -> KVITable v
delete :: forall v. KeySpec -> KVITable v -> KVITable v
delete KeySpec
k KVITable v
t = KVITable v
t { contents :: Map KeySpec v
contents = forall k a. Ord k => k -> Map k a -> Map k a
Map.delete (forall v. KVITable v -> KeySpec -> KeySpec
normalizeKeySpec KVITable v
t KeySpec
k) forall a b. (a -> b) -> a -> b
$ forall v. KVITable v -> Map KeySpec v
contents KVITable v
t }
adjustWithKey :: (KeySpec -> v -> v) -> KeySpec -> KVITable v -> KVITable v
adjustWithKey :: forall v.
(KeySpec -> v -> v) -> KeySpec -> KVITable v -> KVITable v
adjustWithKey KeySpec -> v -> v
f KeySpec
k KVITable v
t =
KVITable v
t { contents :: Map KeySpec v
contents = forall k a. Ord k => (k -> a -> a) -> k -> Map k a -> Map k a
Map.adjustWithKey KeySpec -> v -> v
f (forall v. KVITable v -> KeySpec -> KeySpec
normalizeKeySpec KVITable v
t KeySpec
k) forall a b. (a -> b) -> a -> b
$ forall v. KVITable v -> Map KeySpec v
contents KVITable v
t }
adjust :: (v -> v) -> KeySpec -> KVITable v -> KVITable v
adjust :: forall v. (v -> v) -> KeySpec -> KVITable v -> KVITable v
adjust v -> v
f KeySpec
k KVITable v
t = KVITable v
t { contents :: Map KeySpec v
contents = forall k a. Ord k => (a -> a) -> k -> Map k a -> Map k a
Map.adjust v -> v
f (forall v. KVITable v -> KeySpec -> KeySpec
normalizeKeySpec KVITable v
t KeySpec
k) forall a b. (a -> b) -> a -> b
$ forall v. KVITable v -> Map KeySpec v
contents KVITable v
t }
updateWithKey :: (KeySpec -> v -> Maybe v) -> KeySpec -> KVITable v -> KVITable v
updateWithKey :: forall v.
(KeySpec -> v -> Maybe v) -> KeySpec -> KVITable v -> KVITable v
updateWithKey KeySpec -> v -> Maybe v
f KeySpec
k KVITable v
t =
KVITable v
t { contents :: Map KeySpec v
contents = forall k a. Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
Map.updateWithKey KeySpec -> v -> Maybe v
f (forall v. KVITable v -> KeySpec -> KeySpec
normalizeKeySpec KVITable v
t KeySpec
k) forall a b. (a -> b) -> a -> b
$ forall v. KVITable v -> Map KeySpec v
contents KVITable v
t }
update :: (v -> Maybe v) -> KeySpec -> KVITable v -> KVITable v
update :: forall v. (v -> Maybe v) -> KeySpec -> KVITable v -> KVITable v
update v -> Maybe v
f KeySpec
k KVITable v
t = KVITable v
t { contents :: Map KeySpec v
contents = forall k a. Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a
Map.update v -> Maybe v
f (forall v. KVITable v -> KeySpec -> KeySpec
normalizeKeySpec KVITable v
t KeySpec
k) forall a b. (a -> b) -> a -> b
$ forall v. KVITable v -> Map KeySpec v
contents KVITable v
t }
rows :: KVITable v -> [ ([KeyVal], v) ]
rows :: forall v. KVITable v -> [([Key], v)]
rows KVITable v
t = KeyVals -> KeySpec -> [([Key], v)]
go (forall v. KVITable v -> KeyVals
keyvals KVITable v
t) []
where
go :: KeyVals -> KeySpec -> [([Key], v)]
go [] KeySpec
spec = let spec' :: KeySpec
spec' = forall a. [a] -> [a]
reverse KeySpec
spec
in case forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup KeySpec
spec' (forall v. KVITable v -> Map KeySpec v
contents KVITable v
t) of
Maybe v
Nothing -> []
Just v
v -> [ (forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeySpec
spec', v
v) ]
go ((Key
key, [Key]
vals):KeyVals
kvs) KeySpec
spec =
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\Key
v -> let spec' :: KeySpec
spec' = (Key
key,Key
v)forall a. a -> [a] -> [a]
:KeySpec
spec in KeyVals -> KeySpec -> [([Key], v)]
go KeyVals
kvs KeySpec
spec') [Key]
vals