initial commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import gleam/dynamic/decode
|
||||
import gleam/json
|
||||
|
||||
pub type Player {
|
||||
Player(name: String, id: Int, score: Int)
|
||||
}
|
||||
|
||||
pub fn serialize(player: Player) -> List(#(String, json.Json)) {
|
||||
[
|
||||
#("name", json.string(player.name)),
|
||||
#("id", json.int(player.id)),
|
||||
#("score", json.int(player.score)),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn decoder() -> decode.Decoder(Player) {
|
||||
{
|
||||
use name <- decode.field("name", decode.string)
|
||||
use id <- decode.field("id", decode.int)
|
||||
use score <- decode.field("score", decode.int)
|
||||
decode.success(Player(name:, id:, score:))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import gleam/dynamic/decode
|
||||
import gleam/json
|
||||
import gleam/list
|
||||
import player
|
||||
|
||||
pub type Session {
|
||||
Session(id: Int, host_user_id: Int, players: List(player.Player))
|
||||
}
|
||||
|
||||
pub fn serialize(session: Session) -> json.Json {
|
||||
json.object([
|
||||
#("id", json.int(session.id)),
|
||||
#("host_user_id", json.int(session.host_user_id)),
|
||||
#(
|
||||
"players",
|
||||
json.array(
|
||||
list.map(session.players, fn(p) { player.serialize(p) }),
|
||||
of: json.object,
|
||||
),
|
||||
),
|
||||
])
|
||||
}
|
||||
|
||||
pub fn deseralize(json_string: String) -> Result(Session, json.DecodeError) {
|
||||
let session_decoder = {
|
||||
use id <- decode.field("id", decode.int)
|
||||
use host_user_id <- decode.field("host_user_id", decode.int)
|
||||
use players <- decode.field("players", decode.list(player.decoder()))
|
||||
|
||||
decode.success(Session(id:, host_user_id:, players:))
|
||||
}
|
||||
|
||||
json.parse(from: json_string, using: session_decoder)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import gleam/io
|
||||
|
||||
pub fn main() {
|
||||
io.println("Hello from shared!")
|
||||
}
|
||||
Reference in New Issue
Block a user