Skip to content

Commit

Permalink
v0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Feb 17, 2024
1 parent 6497891 commit e1926bc
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 26 deletions.
1 change: 1 addition & 0 deletions examples/3-working-with-json/gleam.toml
Expand Up @@ -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"
1 change: 1 addition & 0 deletions examples/5-using-a-database/gleam.toml
Expand Up @@ -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"
38 changes: 23 additions & 15 deletions examples/5-using-a-database/src/app/web/people.gleam
Expand Up @@ -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}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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),
])
Expand All @@ -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))
}
4 changes: 2 additions & 2 deletions examples/5-using-a-database/test/app_test.gleam
Expand Up @@ -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)

Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions examples/6-serving-static-assets/gleam.toml
Expand Up @@ -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"
3 changes: 1 addition & 2 deletions 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 = "<!DOCTYPE html>
Expand All @@ -18,6 +17,6 @@ const html = "<!DOCTYPE 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)
}
4 changes: 2 additions & 2 deletions examples/6-serving-static-assets/test/app_test.gleam
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions examples/8-working-with-cookies/gleam.toml
Expand Up @@ -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"
10 changes: 5 additions & 5 deletions examples/utilities/tiny_database/src/tiny_database.gleam
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down

0 comments on commit e1926bc

Please sign in to comment.