Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,16 @@ func handleCompile(c *Core, w *ResponseWriter, r *Request) {
w.Error(srverr.ErrInvalid(err))
return
}
w.Respond(http.StatusOK, ast.Parsed())
// The AST is marshaled without type names, as by "super compile",
// since a named struct type like ast.Assignment would be bound to
// different record types as the concrete types of its interface
// fields vary.
val, err := super.NewMarshaler(w.sctx).Marshal(ast.Parsed())
if err != nil {
w.Error(err)
return
}
w.RespondValue(http.StatusOK, val)
}

func handleQueryDescribe(c *Core, w *ResponseWriter, r *Request) {
Expand Down Expand Up @@ -616,7 +625,7 @@ func handleDelete(c *Core, w *ResponseWriter, r *Request) {
w.Error(err)
return
}
w.Marshal(api.CommitResponse{Commit: commit})
w.Respond(http.StatusOK, api.CommitResponse{Commit: commit})
c.publishEvent(w, "branch-commit", api.EventBranchCommit{
CommitID: commit,
TableID: table.ID,
Expand Down
40 changes: 20 additions & 20 deletions service/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,27 @@ func (w *ResponseWriter) Write(b []byte) (int, error) {
}

func (w *ResponseWriter) Respond(status int, body any) bool {
val, err := w.marshaler.Marshal(body)
if err != nil {
w.Error(err)
return false
}
return w.RespondValue(status, val)
}

// RespondValue writes the status and then val in the response format.
func (w *ResponseWriter) RespondValue(status int, val super.Value) bool {
zw := w.ZioWriter()
if zw == nil {
return false
}
w.WriteHeader(status)
return w.Marshal(body)
if err := zw.Push(sbuf.Dematerialize(w.sctx, val)); err != nil {
w.Error(err)
return false
}
zw.Close()
return true
}

func (w *ResponseWriter) Error(err error) {
Expand All @@ -290,25 +309,6 @@ func (w *ResponseWriter) Error(err error) {
}
}

func (w *ResponseWriter) Marshal(body any) bool {
val, err := w.marshaler.Marshal(body)
if err != nil {
// XXX If status header has not been sent this should send error.
w.Error(err)
return false
}
zw := w.ZioWriter()
if zw == nil {
return false
}
if err := zw.Push(sbuf.Dematerialize(w.sctx, val)); err != nil {
w.Error(err)
return false
}
zw.Close()
return true
}

func errorResponse(e error) (status int, ae *api.Error) {
status = http.StatusInternalServerError
ae = &api.Error{Type: "Error"}
Expand Down
8 changes: 8 additions & 0 deletions service/ztests/compile.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
script: |
source service.sh
curl -d '{"query":"count("}' $DB_URL/compile | super -s -c 'cut compilation_errors' -
echo ===
curl -s -o /dev/null -w '%{http_code}\n' -d '{"query":"from t | count() by x"}' $DB_URL/compile
curl -s -d '{"query":"from t | count() by x"}' $DB_URL/compile > server.sup
super compile -s -c 'from t | count() by x' > local.sup
cmp server.sup local.sup && echo same

inputs:
- name: service.sh
Expand All @@ -9,3 +14,6 @@ outputs:
- name: stdout
data: |
{compilation_errors:[{Msg:"parse error",Pos:6,End:-1}]}
===
200
same
Loading