From e1926bc09091ca20414427b6c02ab4b059c1c324 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Sat, 17 Feb 2024 17:03:10 +0000 Subject: [PATCH] v0.12.0 --- examples/3-working-with-json/gleam.toml | 1 + examples/5-using-a-database/gleam.toml | 1 + .../src/app/web/people.gleam | 38 +++++++++++-------- .../5-using-a-database/test/app_test.gleam | 4 +- examples/6-serving-static-assets/gleam.toml | 1 + .../src/app/router.gleam | 3 +- .../test/app_test.gleam | 4 +- examples/8-working-with-cookies/gleam.toml | 1 + .../tiny_database/src/tiny_database.gleam | 10 ++--- 9 files changed, 37 insertions(+), 26 deletions(-) diff --git a/examples/3-working-with-json/gleam.toml b/examples/3-working-with-json/gleam.toml index e38dede..882ed4b 100644 --- a/examples/3-working-with-json/gleam.toml +++ b/examples/3-working-with-json/gleam.toml @@ -9,6 +9,7 @@ wisp = { path = "../.." } gleam_json = "~> 0.6" gleam_erlang = "~> 0.23" mist = "~> 0.14" +gleam_http = "~> 3.5" [dev-dependencies] gleeunit = "~> 1.0" diff --git a/examples/5-using-a-database/gleam.toml b/examples/5-using-a-database/gleam.toml index 3c2470c..6b207dd 100644 --- a/examples/5-using-a-database/gleam.toml +++ b/examples/5-using-a-database/gleam.toml @@ -10,6 +10,7 @@ gleam_json = "~> 0.6" tiny_database = { path = "../utilities/tiny_database" } gleam_erlang = "~> 0.23" mist = "~> 0.14" +gleam_http = "~> 3.5" [dev-dependencies] gleeunit = "~> 1.0" diff --git a/examples/5-using-a-database/src/app/web/people.gleam b/examples/5-using-a-database/src/app/web/people.gleam index 01518fc..eb9a2ba 100644 --- a/examples/5-using-a-database/src/app/web/people.gleam +++ b/examples/5-using-a-database/src/app/web/people.gleam @@ -2,7 +2,7 @@ import app/web.{type Context} import gleam/dynamic.{type Dynamic} import gleam/http.{Get, Post} import gleam/json -import gleam/map +import gleam/dict import gleam/result.{try} import tiny_database import wisp.{type Request, type Response} @@ -41,12 +41,16 @@ pub fn list_people(ctx: Context) -> Response { use ids <- try(tiny_database.list(ctx.db)) // Convert the ids into a JSON array of objects. - Ok(json.to_string_builder(json.object([ - #( - "people", - json.array(ids, fn(id) { json.object([#("id", json.string(id))]) }), + Ok( + json.to_string_builder( + json.object([ + #( + "people", + json.array(ids, fn(id) { json.object([#("id", json.string(id))]) }), + ), + ]), ), - ]))) + ) } case result { @@ -88,11 +92,15 @@ pub fn read_person(ctx: Context, id: String) -> Response { use person <- try(read_from_database(ctx.db, id)) // Construct a JSON payload with the person's details. - Ok(json.to_string_builder(json.object([ - #("id", json.string(id)), - #("name", json.string(person.name)), - #("favourite-colour", json.string(person.favourite_colour)), - ]))) + Ok( + json.to_string_builder( + json.object([ + #("id", json.string(id)), + #("name", json.string(person.name)), + #("favourite-colour", json.string(person.favourite_colour)), + ]), + ), + ) } // Return an appropriate response. @@ -123,9 +131,9 @@ pub fn save_to_database( person: Person, ) -> Result(String, Nil) { // In a real application you might use a database client with some SQL here. - // Instead we create a simple map and save that. + // Instead we create a simple dict and save that. let data = - map.from_list([ + dict.from_list([ #("name", person.name), #("favourite-colour", person.favourite_colour), ]) @@ -138,7 +146,7 @@ pub fn read_from_database( ) -> Result(Person, Nil) { // In a real application you might use a database client with some SQL here. use data <- try(tiny_database.read(db, id)) - use name <- try(map.get(data, "name")) - use favourite_colour <- try(map.get(data, "favourite-colour")) + use name <- try(dict.get(data, "name")) + use favourite_colour <- try(dict.get(data, "favourite-colour")) Ok(Person(name, favourite_colour)) } diff --git a/examples/5-using-a-database/test/app_test.gleam b/examples/5-using-a-database/test/app_test.gleam index 85a8957..952d581 100644 --- a/examples/5-using-a-database/test/app_test.gleam +++ b/examples/5-using-a-database/test/app_test.gleam @@ -12,7 +12,7 @@ pub fn main() { gleeunit.main() } -fn with_context(test: fn(Context) -> t) -> t { +fn with_context(testcase: fn(Context) -> t) -> t { // Create a new database connection for this test use db <- tiny_database.with_connection(app.data_directory) @@ -21,7 +21,7 @@ fn with_context(test: fn(Context) -> t) -> t { let context = Context(db: db) // Run the test with the context - test(context) + testcase(context) } pub fn get_unknown_test() { diff --git a/examples/6-serving-static-assets/gleam.toml b/examples/6-serving-static-assets/gleam.toml index 2d82e4b..137de9a 100644 --- a/examples/6-serving-static-assets/gleam.toml +++ b/examples/6-serving-static-assets/gleam.toml @@ -8,6 +8,7 @@ gleam_stdlib = "~> 0.30" wisp = { path = "../.." } gleam_erlang = "~> 0.23" mist = "~> 0.14" +gleam_http = "~> 3.5" [dev-dependencies] gleeunit = "~> 1.0" diff --git a/examples/6-serving-static-assets/src/app/router.gleam b/examples/6-serving-static-assets/src/app/router.gleam index 2b1fc9e..eb21093 100644 --- a/examples/6-serving-static-assets/src/app/router.gleam +++ b/examples/6-serving-static-assets/src/app/router.gleam @@ -1,6 +1,5 @@ import wisp.{type Request, type Response} import gleam/string_builder -import gleam/http import app/web.{type Context} const html = " @@ -18,6 +17,6 @@ const html = " " pub fn handle_request(req: Request, ctx: Context) -> Response { - use req <- web.middleware(req, ctx) + use _req <- web.middleware(req, ctx) wisp.html_response(string_builder.from_string(html), 200) } diff --git a/examples/6-serving-static-assets/test/app_test.gleam b/examples/6-serving-static-assets/test/app_test.gleam index c789ee4..84cea8f 100644 --- a/examples/6-serving-static-assets/test/app_test.gleam +++ b/examples/6-serving-static-assets/test/app_test.gleam @@ -9,12 +9,12 @@ pub fn main() { gleeunit.main() } -fn with_context(test: fn(Context) -> t) -> t { +fn with_context(testcase: fn(Context) -> t) -> t { // Create the context to use in tests let context = Context(static_directory: app.static_directory()) // Run the test with the context - test(context) + testcase(context) } pub fn get_home_page_test() { diff --git a/examples/8-working-with-cookies/gleam.toml b/examples/8-working-with-cookies/gleam.toml index ef46039..1d3d713 100644 --- a/examples/8-working-with-cookies/gleam.toml +++ b/examples/8-working-with-cookies/gleam.toml @@ -9,6 +9,7 @@ wisp = { path = "../.." } gleam_crypto = "~> 1.0" gleam_erlang = "~> 0.23" mist = "~> 0.14" +gleam_http = "~> 3.5" [dev-dependencies] gleeunit = "~> 1.0" diff --git a/examples/utilities/tiny_database/src/tiny_database.gleam b/examples/utilities/tiny_database/src/tiny_database.gleam index b1f3159..1d676bf 100644 --- a/examples/utilities/tiny_database/src/tiny_database.gleam +++ b/examples/utilities/tiny_database/src/tiny_database.gleam @@ -3,7 +3,7 @@ import gleam/dynamic import ids/nanoid import gleam/json import gleam/list -import gleam/map.{type Map} +import gleam/dict.{type Dict} import simplifile pub opaque type Connection { @@ -41,13 +41,13 @@ pub fn list(connection: Connection) -> Result(List(String), Nil) { pub fn insert( connection: Connection, - values: Map(String, String), + values: Dict(String, String), ) -> Result(String, Nil) { let assert Ok(_) = simplifile.create_directory_all(connection.root) let id = nanoid.generate() let values = values - |> map.to_list + |> dict.to_list |> list.map(fn(pair) { #(pair.0, json.string(pair.1)) }) let json = json.to_string(json.object(values)) use _ <- result.try( @@ -60,13 +60,13 @@ pub fn insert( pub fn read( connection: Connection, id: String, -) -> Result(Map(String, String), Nil) { +) -> Result(Dict(String, String), Nil) { use data <- result.try( simplifile.read(file_path(connection, id)) |> result.nil_error, ) - let decoder = dynamic.map(dynamic.string, dynamic.string) + let decoder = dynamic.dict(dynamic.string, dynamic.string) use data <- result.try( json.decode(data, decoder)