Add initial Vault design

3-vault
Bram 2024-05-08 07:57:08 +02:00
parent e1c7c8792e
commit 5cf6b59602
5 changed files with 119 additions and 9 deletions

View File

@ -3,7 +3,7 @@ module Internal.Tools.Hashdict exposing
, empty, singleton, insert, remove, removeKey
, isEmpty, member, memberKey, get, size, isEqual
, keys, values, toList, fromList
, rehash, union, map
, rehash, union, map, update
, coder, encode, decoder, softDecoder
)
@ -35,7 +35,7 @@ This allows you to store values based on an externally defined identifier.
## Transform
@docs rehash, union, map
@docs rehash, union, map, update
## JSON coders
@ -321,6 +321,24 @@ union (Hashdict h1) hd2 =
}
{-| Update a dict to maybe contain a value (or not). If the output does not
have the originally expected key, it is not updated.
-}
update : String -> (Maybe a -> Maybe a) -> Hashdict a -> Hashdict a
update key f ((Hashdict h) as hd) =
-- TODO: Write test for this
case f (get key hd) of
Just v ->
if h.hash v == key then
insert v hd
else
hd
Nothing ->
removeKey key hd
{-| Get all values stored in the hashdict, in the order of their keys.
-}
values : Hashdict a -> List a

View File

@ -1,13 +1,74 @@
module Internal.Values.Vault exposing (Vault)
module Internal.Values.Vault exposing
( fromRoomId, mapRoom, updateRoom
, getAccountData, setAccountData
, Vault
)
{-| This module hosts the Vault module.
{-| This module hosts the Vault module. The Vault is the data type storing all
credentials, all user information and all other information that the user
can receive from the Matrix API.
@docs Vault
## Rooms
Rooms are environments where people can have a conversation with each other.
@docs fromRoomId, mapRoom, updateRoom
## Account data
@docs getAccountData, setAccountData
-}
import FastDict as Dict exposing (Dict)
import Internal.Tools.Hashdict as Hashdict exposing (Hashdict)
import Internal.Tools.Json as Json
import Internal.Values.Room exposing (Room)
{-| This is the Vault type.
-}
type alias Vault =
()
{ accountData : Dict String Json.Value
, rooms : Hashdict Room
}
{-| Get a given room by its room id.
-}
fromRoomId : String -> Vault -> Maybe Room
fromRoomId roomId vault =
Hashdict.get roomId vault.rooms
{-| Get a piece of account data as information from the room.
-}
getAccountData : String -> Vault -> Maybe Json.Value
getAccountData key vault =
Dict.get key vault.accountData
{-| Update a room, if it exists. If the room isn´t known, this operation is
ignored.
-}
mapRoom : String -> (Room -> Room) -> Vault -> Vault
mapRoom roomId f vault =
{ vault | rooms = Hashdict.map roomId f vault.rooms }
{-| Set a piece of account data as information in the global vault data.
-}
setAccountData : String -> Json.Value -> Vault -> Vault
setAccountData key value vault =
{ vault | accountData = Dict.insert key value vault.accountData }
{-| Update a Room based on whether it exists or not.
-}
updateRoom : String -> (Maybe Room -> Maybe Room) -> Vault -> Vault
updateRoom roomId f vault =
{ vault | rooms = Hashdict.update roomId f vault.rooms }

View File

@ -24,7 +24,8 @@ import Types
{-| The Vault type stores all relevant information about the Matrix API.
It currently supports no functionality and it will just stay here - for fun.
If you make sure that the data type stays up-to-date, you can use it to explore
the latest information about an account.
-}
type alias Vault =

View File

@ -103,7 +103,7 @@ suite =
]
, describe "singleton"
[ fuzz TestEvent.fuzzer
"singletong = empty + insert"
"singleton = empty + insert"
(\event ->
Hashdict.empty .eventId
|> Hashdict.insert event
@ -159,6 +159,26 @@ suite =
|> Expect.equal False
)
]
, describe "update"
[ fuzz2 (fuzzer identity Fuzz.string)
Fuzz.string
"add = insert"
(\hashdict value ->
Hashdict.isEqual
(Hashdict.insert value hashdict)
(Hashdict.update value (always (Just value)) hashdict)
|> Expect.equal True
)
, fuzz2 (fuzzer identity Fuzz.string)
Fuzz.string
"remove = removeKey"
(\hashdict value ->
Hashdict.isEqual
(Hashdict.removeKey value hashdict)
(Hashdict.update value (always Nothing) hashdict)
|> Expect.equal True
)
]
, describe "JSON"
[ fuzz2 eventFuzzer
(Fuzz.intRange 0 10)

View File

@ -1,10 +1,20 @@
module Test.Values.Vault exposing (..)
import FastDict as Dict exposing (Dict)
import Fuzz exposing (Fuzzer)
import Internal.Tools.Json as Json
import Internal.Values.Vault exposing (Vault)
import Test exposing (..)
import Test.Tools.Hashdict as TestHashdict
import Test.Values.Room as TestRoom
vault : Fuzzer Vault
vault =
Fuzz.unit
Fuzz.map2 Vault
(Fuzz.string
|> Fuzz.map (\k -> ( k, Json.encode Json.int 0 ))
|> Fuzz.list
|> Fuzz.map Dict.fromList
)
(TestHashdict.fuzzer .roomId TestRoom.fuzzer)