8000 update dependencies and replace deprecated functions by Rhal95 · Pull Request #1 · MystPi/dinostore · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

update dependencies and replace deprecated functions #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
8000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ runtime = "deno"
typescript_declarations = true

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
gleam_javascript = ">= 0.12.0 and < 1.0.0"
gleam_stdlib = ">= 0.59.0 and < 1.0.0"
gleam_javascript = ">= 1.0.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
8 changes: 4 additions & 4 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_javascript", version = "0.12.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "6EB652538B31E852FE0A8307A8B6314DEB34930944B6DDC41CCC31CA344DA35D" },
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
{ name = "gleam_javascript", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "EF6C77A506F026C6FB37941889477CD5E4234FCD4337FF0E9384E297CB8F97EB" },
{ name = "gleam_stdlib", version = "0.59.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "F8FEE9B35797301994B81AF75508CF87C328FE1585558B0FFD188DC2B32EAA95" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
]

[requirements]
gleam_javascript = { version = ">= 0.12.0 and < 1.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleam_javascript = { version = ">= 1.0.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.59.0 and < 1.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
24 changes: 14 additions & 10 deletions src/dinostore/key.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dinostore/ulid
import gleam/dynamic
import gleam/result
import gleam/dynamic/decode

pub type KeyPart {
StringPart(value: String)
Expand All @@ -12,7 +12,7 @@ pub type KeyPart {

/// A key is simply a list of `KeyPart`s.
pub type Key =
List(KeyPart)
List(KeyPart)

/// Helper function to create a `StringPart`.
pub fn s(s: String) -> KeyPart {
Expand Down Expand Up @@ -64,12 +64,16 @@ pub fn unwrap(key: Key) -> JsKey
/// [secondary indexes](https://docs.deno.com/deploy/kv/manual/#improve-querying-with-secondary-indexes)
/// where a primary key is stored as a secondary index's value.
///
pub fn decode(x: dynamic.Dynamic) {
dynamic.list(
of: dynamic.any(of: [
fn(x) { result.map(dynamic.string(x), StringPart) },
fn(x) { result.map(dynamic.float(x), NumberPart) },
fn(x) { result.map(dynamic.bool(x), BoolPart) },
]),
)(x)
pub fn decode(
x: dynamic.Dynamic,
) -> Result(List(KeyPart), List(decode.DecodeError)) {
decode.run(
x,
decode.list(
of: decode.one_of(decode.string |> decode.map(StringPart), or: [
decode.float |> decode.map(NumberPart),
decode.bool |> decode.map(BoolPart),
]),
),
)
}
22 changes: 16 additions & 6 deletions test/dinostore_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import dinostore/atomic
import dinostore/key
import dinostore/kv
import gleam/bool
import gleam/dynamic
import gleam/dynamic/decode
import gleam/float
import gleam/io
import gleam/javascript/promise.{type Promise}
import gleam/list
Expand All @@ -17,13 +18,21 @@ pub fn main() {

io.println("Original funds:")
use users <- promise.await(list_users(conn))
list.each(users, io.debug)
list.each(users, fn(user) {
io.print(user.0)
io.print(":\t")
io.println(float.to_string(user.1))
})

use _ <- promise.await(transfer_funds(conn, "bob", "alice", 10.0))

io.println("Funds after transfer:")
use users <- promise.await(list_users(conn))
list.each(users, io.debug)
list.each(users, fn(user) {
io.print(user.0)
io.print(":\t")
io.println(float.to_string(user.1))
})

promise.resolve(Nil)
}
Expand Down Expand Up @@ -53,7 +62,7 @@ fn list_users(conn: kv.Connection) -> Promise(List(#(String, Float))) {
))

list.map(entries, fn(entry) {
let assert Ok(balance) = dynamic.float(entry.value)
let assert Ok(balance) = decode.run(entry.value, decode.float)
let assert Ok(key.StringPart(name)) = list.last(entry.key)
#(name, balance)
})
Expand Down Expand Up @@ -83,8 +92,9 @@ fn transfer_funds(
use sender_entry <- promise.try_await(get_account(conn, sender_key))
use receiver_entry <- promise.try_await(get_account(conn, receiver_key))

let assert Ok(sender_balance) = dynamic.float(sender_entry.value)
let assert Ok(receiver_balance) = dynamic.float(receiver_entry.value)
let assert Ok(sender_balance) = decode.run(sender_entry.value, decode.float)
let assert Ok(receiver_balance) =
decode.run(receiver_entry.value, decode.float)

use <- bool.guard(
when: sender_balance <. amount,
Expand Down
0