Skip to content

Commit

Permalink
better query escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Aug 9, 2022
1 parent 39ea729 commit 5c10a20
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion trealla/prolog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ func TestQuery(t *testing.T) {
{
name: "member/2",
want: trealla.Answer{
Query: `member(X, [1,foo(bar),4.2,"baz",'boop']).`,
Query: `member(X, [1,foo(bar),4.2,"baz",'boop', [q, '"'], '\\', '\n']).`,
Result: "success",
Answers: []trealla.Solution{
{"X": int64(1)},
{"X": trealla.Compound{Functor: "foo", Args: []trealla.Term{"bar"}}},
{"X": 4.2},
{"X": "baz"},
{"X": "boop"},
{"X": []trealla.Term{"q", `"`}},
{"X": `\`},
{"X": "\n"},
},
},
},
Expand Down
4 changes: 3 additions & 1 deletion trealla/trealla.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ func (pl *prolog) ask(ctx context.Context, query string) (string, error) {
}

func escapeQuery(query string) string {
query = strings.ReplaceAll(query, `"`, `\"`)
query = stringEscaper.Replace(query)
return fmt.Sprintf(`use_module(library(wasm_toplevel)), wasm_ask("%s")`, query)
}

var stringEscaper = strings.NewReplacer(`\`, `\\`, `"`, `\"`)

// Option is an optional parameter for New.
type Option func(*prolog)

Expand Down

0 comments on commit 5c10a20

Please sign in to comment.