diff --git a/service/handlers.go b/service/handlers.go index f27162b84..d40fb4bf4 100644 --- a/service/handlers.go +++ b/service/handlers.go @@ -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) { @@ -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, diff --git a/service/request.go b/service/request.go index b87739c03..33f2d954a 100644 --- a/service/request.go +++ b/service/request.go @@ -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) { @@ -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"} diff --git a/service/ztests/compile.yaml b/service/ztests/compile.yaml index 88e21dfa2..29aec661d 100644 --- a/service/ztests/compile.yaml +++ b/service/ztests/compile.yaml @@ -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 @@ -9,3 +14,6 @@ outputs: - name: stdout data: | {compilation_errors:[{Msg:"parse error",Pos:6,End:-1}]} + === + 200 + same