Skip to content

Commit

Permalink
Refactor input parsing in v0 API
Browse files Browse the repository at this point in the history
Previously the server was using the Rego parse to conveniently parse
inputs into ast.Value. For large inputs this was taking unnecessarily
long (e.g., 60s for 500KB of JSON.)
  • Loading branch information
tsandall committed Sep 15, 2017
1 parent 7779915 commit b140b74
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1464,33 +1464,26 @@ func getExplain(p []string, zero types.ExplainModeV1) types.ExplainModeV1 {
}

func readInputV0(r io.ReadCloser) (ast.Value, error) {

bs, err := ioutil.ReadAll(r)

if err != nil {
return nil, err
}

s := strings.TrimSpace(string(bs))
if len(s) == 0 {
bs = bytes.TrimSpace(bs)
if len(bs) == 0 {
return nil, nil
}

term, err := ast.ParseTerm(s)
if err != nil {
var x interface{}
if err := util.UnmarshalJSON(bs, &x); err != nil {
return nil, err
}

return term.Value, nil
return ast.InterfaceToValue(x)
}

func readInputGetV1(str string) (ast.Value, error) {
var input interface{}

if err := util.UnmarshalJSON([]byte(str), &input); err != nil {
return nil, errors.Wrapf(err, "parameter contains malformed input document")
}

return ast.InterfaceToValue(input)
}

Expand Down

0 comments on commit b140b74

Please sign in to comment.