Skip to content

Commit

Permalink
allow module name to be specified in http_consult/1
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Jun 15, 2024
1 parent 27fe596 commit eaa5459
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ These additional predicates are built in:

- `crypto_data_hash/3`
- `http_consult/1`
- Argument can be URL string, or `my_module_name:"https://url.example"`

## WASM binary

Expand Down
2 changes: 1 addition & 1 deletion trealla/interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestInterop(t *testing.T) {
name: "http_consult/1",
want: []Answer{
{
Query: `http_consult("https://raw.githubusercontent.com/guregu/worker-prolog/978c956801ffff83f190450e5c0325a9d34b064a/src/views/examples/fizzbuzz.pl"), !, fizzbuzz(1, 21), !`,
Query: `http_consult(fizzbuzz:"https://raw.githubusercontent.com/guregu/worker-prolog/978c956801ffff83f190450e5c0325a9d34b064a/src/views/examples/fizzbuzz.pl"), fizzbuzz:fizzbuzz(1, 21), !`,
Solution: Substitution{},
Stdout: "1\n2\nfizz\n4\nbuzz\nfizz\n7\n8\nfizz\nbuzz\n11\nfizz\n13\n14\nfizzbuzz\n16\n17\nfizz\n19\nbuzz\nfizz\n",
},
Expand Down
30 changes: 23 additions & 7 deletions trealla/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,27 @@ func http_consult_1(_ Prolog, _ Subquery, goal Term) Term {
if len(cmp.Args) != 1 {
return systemError(piTerm("http_consult", 1))
}
str, ok := cmp.Args[0].(string)
if !ok {
return typeError("chars", cmp.Args[0], piTerm("http_consult", 1))
module := Atom("user")
var addr string
switch x := cmp.Args[0].(type) {
case string:
addr = x
case Compound:
// http_consult(module_name:"http://...")
if x.Functor != ":" || len(x.Args) != 2 {
return typeError("chars", cmp.Args[0], piTerm("http_consult", 1))
}
var ok bool
module, ok = x.Args[0].(Atom)
if !ok {
return typeError("atom", x.Args[0], piTerm("http_consult", 1))
}
addr, ok = x.Args[1].(string)
if !ok {
return typeError("chars", x.Args[1], piTerm("http_consult", 1))
}
}
href, err := url.Parse(str)
href, err := url.Parse(addr)
if err != nil {
return domainError("url", cmp.Args[0], piTerm("http_consult", 1))
}
Expand All @@ -131,9 +147,9 @@ func http_consult_1(_ Prolog, _ Subquery, goal Term) Term {
case http.StatusNoContent:
return goal
case http.StatusNotFound, http.StatusGone:
return existenceError("source_sink", str, piTerm("http_consult", 1))
return existenceError("source_sink", addr, piTerm("http_consult", 1))
case http.StatusForbidden, http.StatusUnauthorized:
return permissionError("open,source_sink", str, piTerm("http_consult", 1))
return permissionError("open,source_sink", addr, piTerm("http_consult", 1))
default:
return systemError(fmt.Errorf("http_consult/1: unexpected status code: %d", resp.StatusCode))
}
Expand All @@ -144,7 +160,7 @@ func http_consult_1(_ Prolog, _ Subquery, goal Term) Term {
}

// call(URL:'$load_chars'(Text)).
return Atom("call").Of(Atom(":").Of(Atom(href.String()), Atom("$load_chars").Of(buf.String())))
return Atom("call").Of(Atom(":").Of(module, Atom("$load_chars").Of(buf.String())))
}

func crypto_data_hash_3(pl Prolog, _ Subquery, goal Term) Term {
Expand Down
2 changes: 1 addition & 1 deletion trealla/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestQuery(t *testing.T) {
want: []trealla.Answer{
{
// TODO: flake? need to retry once for 'run' to be found
Query: "use_module(library(tak)), fail ; run.",
Query: "use_module(library(tak)), run.",
Solution: trealla.Substitution{},
Stdout: "'<https://josd.github.io/eye/ns#tak>'([34,13,8],13).\n",
},
Expand Down

0 comments on commit eaa5459

Please sign in to comment.