Skip to content

Commit

Permalink
bigint support
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Oct 11, 2022
1 parent 7ab7a92 commit f0c3169
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions trealla/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math/big"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -132,6 +133,15 @@ func TestQuery(t *testing.T) {
},
},
},
{
name: "bigint",
want: []trealla.Answer{
{
Query: "X=9999999999999999, Y = -9999999999999999, Z = 123",
Solution: trealla.Solution{"X": big.NewInt(9999999999999999), "Y": big.NewInt(-9999999999999999), "Z": int64(123)},
},
},
},
}

for _, tc := range tests {
Expand Down
11 changes: 11 additions & 0 deletions trealla/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"strconv"
"strings"
"unicode"
Expand All @@ -15,6 +16,7 @@ import (
// - string
// - int64
// - float64
// - *big.Int
// - Atom
// - Compound
// - Variable
Expand Down Expand Up @@ -89,13 +91,22 @@ func unmarshalTerm(bs []byte) (Term, error) {
Args []json.RawMessage
Var string
Attr []json.RawMessage
Number string
}
dec = json.NewDecoder(bytes.NewReader(bs))
dec.UseNumber()
if err := dec.Decode(&term); err != nil {
return nil, err
}

if term.Number != "" {
n := new(big.Int)
if _, ok := n.SetString(term.Number, 10); !ok {
return nil, fmt.Errorf("trealla: failed to decode number: %s", term.Number)
}
return n, nil
}

if term.Var != "" {
attr := make([]Term, 0, len(term.Attr))
for _, raw := range term.Attr {
Expand Down

0 comments on commit f0c3169

Please sign in to comment.