Skip to content

Commit

Permalink
process.pid_from_dynamic
Browse files Browse the repository at this point in the history
Closes #41
  • Loading branch information
lpil committed Mar 20, 2024
1 parent 4386834 commit 76f5475
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 159 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
- uses: actions/checkout@v3
- uses: erlef/setup-beam@v1
with:
otp-version: "25.2"
otp-version: "26.2"
rebar3-version: "3"
gleam-version: "0.32"
gleam-version: "1.0.0"
- run: gleam test
- run: gleam format --check src test
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.25.0 - 2024-03-20

- Updated for `gleam_stdlib` v0.33.0.

## v0.24.0 - 2024-01-03

- Updated for `gleam_stdlib` v0.33.0.
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "gleam_erlang"

version = "0.24.0"
version = "0.25.0"
licences = ["Apache-2.0"]
description = "A Gleam library for working with Erlang"

Expand Down
2 changes: 1 addition & 1 deletion src/gleam/erlang.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gleam/dynamic.{type Dynamic}
import gleam/list
import gleam/erlang/atom.{type Atom}
import gleam/erlang/charlist.{type Charlist}
import gleam/list

@external(erlang, "io_lib", "format")
fn erl_format(a: String, b: List(a)) -> Charlist
Expand Down
26 changes: 20 additions & 6 deletions src/gleam/erlang/process.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gleam/string
import gleam/dynamic.{type Dynamic}
import gleam/dynamic.{type DecodeErrors, type Dynamic}
import gleam/erlang.{type Reference}
import gleam/erlang/atom.{type Atom}
import gleam/string

/// A `Pid` (or Process identifier) is a reference to an Erlang process. Each
/// process has a `Pid` and it is one of the lowest level building blocks of
Expand Down Expand Up @@ -563,10 +563,9 @@ pub fn try_call(
let result =
new_selector()
|> selecting(reply_subject, Ok)
|> selecting_process_down(
monitor,
fn(down: ProcessDown) { Error(CalleeDown(reason: down.reason)) },
)
|> selecting_process_down(monitor, fn(down: ProcessDown) {
Error(CalleeDown(reason: down.reason))
})
|> select(timeout)

// Demonitor the process and close the channels as we're done
Expand Down Expand Up @@ -744,3 +743,18 @@ pub fn unregister(name: Atom) -> Result(Nil, Nil)
///
@external(erlang, "gleam_erlang_ffi", "process_named")
pub fn named(name: Atom) -> Result(Pid, Nil)

/// Checks to see whether a `Dynamic` value is a pid, and return the pid if
/// it is.
///
/// ## Examples
///
/// > import gleam/dynamic
/// > from_dynamic(dynamic.from(process.self()))
/// Ok(process.self())
///
/// > from_dynamic(dynamic.from(123))
/// Error([DecodeError(expected: "Pid", found: "Int", path: [])])
///
@external(erlang, "gleam_erlang_ffi", "pid_from_dynamic")
pub fn pid_from_dynamic(from from: Dynamic) -> Result(Pid, DecodeErrors)
7 changes: 6 additions & 1 deletion src/gleam_erlang_ffi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
new_selector/0, link/1, insert_selector_handler/3, select/1, select/2,
trap_exits/1, map_selector/2, merge_selector/2, flush_messages/0,
priv_directory/1, connect_node/1, register_process/2, unregister_process/1,
process_named/1, identity/1
process_named/1, identity/1, pid_from_dynamic/1
]).

-spec atom_from_string(binary()) -> {ok, atom()} | {error, atom_not_loaded}.
Expand All @@ -20,6 +20,11 @@ atom_from_dynamic(Data) when is_atom(Data) ->
atom_from_dynamic(Data) ->
{error, [{decode_error, <<"Atom">>, gleam@dynamic:classify(Data), []}]}.

pid_from_dynamic(Data) when is_pid(Data) ->
{ok, Data};
pid_from_dynamic(Data) ->
{error, [{decode_error, <<"Pid">>, gleam@dynamic:classify(Data), []}]}.

-spec get_line(io:prompt()) -> {ok, unicode:unicode_binary()} | {error, eof | no_data}.
get_line(Prompt) ->
case io:get_line(Prompt) of
Expand Down
2 changes: 1 addition & 1 deletion test/gleam/erlang/atom_test.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gleam/erlang/atom
import gleam/dynamic.{DecodeError}
import gleam/erlang/atom

pub fn from_string_test() {
let assert Ok(_) = atom.from_string("ok")
Expand Down
2 changes: 1 addition & 1 deletion test/gleam/erlang/env_test.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gleam/erlang/os
import gleam/dict
import gleam/erlang/os

pub fn get_all_test() {
os.set_env("MYVAR", "MYVALUE")
Expand Down
2 changes: 1 addition & 1 deletion test/gleam/erlang/node_tests.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gleam/erlang/node
import gleam/erlang/atom
import gleam/erlang/node

// TODO: Improve these tests by spawning a peer node.

Expand Down
Loading

0 comments on commit 76f5475

Please sign in to comment.