Skip to content

Commit

Permalink
add a stdout-generating variant of upbdev_ProcessOutput()
Browse files Browse the repository at this point in the history
upbdev_ProcessStdout() does the same thing as upbdev_ProcessOutput() except
instead of returning the serialized buffer it just sends it directly to stdout.

PiperOrigin-RevId: 492511049
  • Loading branch information
ericsalo authored and copybara-github committed Dec 2, 2022
1 parent ee56471 commit f5f8675
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
7 changes: 2 additions & 5 deletions upbc/protoc-gen-upbdev.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,13 @@ int main() {
return -1;
}

// Decode and serialize the JSON response.
const auto response = upbdev_ProcessOutput(json_response.data(),
json_response.size(), a, &status);
// Decode, serialize, and write the JSON response.
upbdev_ProcessOutput(json_response.data(), json_response.size(), a, &status);
if (!upb_Status_IsOk(&status)) {
std::cerr << status.msg << std::endl;
return -1;
}

std::cout << std::string(response.data, response.size);

upb_Arena_Free(a);
return 0;
}
51 changes: 34 additions & 17 deletions upbc/upbdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
#include "upbc/code_generator_request.upbdefs.h"

static google_protobuf_compiler_CodeGeneratorResponse* upbc_JsonDecode(
const char* data, size_t size, upb_Arena* a, upb_Status* status) {
const char* data, size_t size, upb_Arena* arena, upb_Status* status) {
google_protobuf_compiler_CodeGeneratorResponse* response =
google_protobuf_compiler_CodeGeneratorResponse_new(a);
google_protobuf_compiler_CodeGeneratorResponse_new(arena);

upb_DefPool* s = upb_DefPool_New();
const upb_MessageDef* m = google_protobuf_compiler_CodeGeneratorResponse_getmsgdef(s);

(void)upb_JsonDecode(data, size, response, m, s, 0, a, status);
(void)upb_JsonDecode(data, size, response, m, s, 0, arena, status);
if (!upb_Status_IsOk(status)) return NULL;

upb_DefPool_Free(s);
Expand All @@ -54,7 +54,7 @@ static google_protobuf_compiler_CodeGeneratorResponse* upbc_JsonDecode(
}

static upb_StringView upbc_JsonEncode(const upbc_CodeGeneratorRequest* request,
upb_Arena* a, upb_Status* status) {
upb_Arena* arena, upb_Status* status) {
upb_StringView out = {.data = NULL, .size = 0};

upb_DefPool* s = upb_DefPool_New();
Expand All @@ -63,7 +63,7 @@ static upb_StringView upbc_JsonEncode(const upbc_CodeGeneratorRequest* request,
out.size = upb_JsonEncode(request, m, s, 0, NULL, 0, status);
if (!upb_Status_IsOk(status)) goto done;

char* data = (char*)upb_Arena_Malloc(a, out.size + 1);
char* data = (char*)upb_Arena_Malloc(arena, out.size + 1);

(void)upb_JsonEncode(request, m, s, 0, data, out.size + 1, status);
if (!upb_Status_IsOk(status)) goto done;
Expand All @@ -75,29 +75,46 @@ static upb_StringView upbc_JsonEncode(const upbc_CodeGeneratorRequest* request,
return out;
}

upb_StringView upbdev_ProcessInput(const char* buf, size_t size, upb_Arena* a,
upb_Status* status) {
upb_StringView upbdev_ProcessInput(const char* buf, size_t size,
upb_Arena* arena, upb_Status* status) {
upb_StringView out = {.data = NULL, .size = 0};

google_protobuf_compiler_CodeGeneratorRequest* inner_request =
google_protobuf_compiler_CodeGeneratorRequest_parse(buf, size, a);
google_protobuf_compiler_CodeGeneratorRequest_parse(buf, size, arena);

const upbc_CodeGeneratorRequest* outer_request =
upbc_MakeCodeGeneratorRequest(inner_request, a, status);
if (upb_Status_IsOk(status)) out = upbc_JsonEncode(outer_request, a, status);
upbc_MakeCodeGeneratorRequest(inner_request, arena, status);
if (upb_Status_IsOk(status))
out = upbc_JsonEncode(outer_request, arena, status);

return out;
}

upb_StringView upbdev_ProcessOutput(const char* buf, size_t size, upb_Arena* a,
upb_Status* status) {
upb_StringView upbdev_ProcessOutput(const char* buf, size_t size,
upb_Arena* arena, upb_Status* status) {
upb_StringView out = {.data = NULL, .size = 0};

const google_protobuf_compiler_CodeGeneratorResponse* response =
upbc_JsonDecode(buf, size, a, status);
if (upb_Status_IsOk(status)) {
out.data =
google_protobuf_compiler_CodeGeneratorResponse_serialize(response, a, &out.size);
}
upbc_JsonDecode(buf, size, arena, status);
if (!upb_Status_IsOk(status)) return out;

out.data = google_protobuf_compiler_CodeGeneratorResponse_serialize(response, arena,
&out.size);
return out;
}

void upbdev_ProcessStdout(const char* buf, size_t size, upb_Arena* arena,
upb_Status* status) {
const upb_StringView sv = upbdev_ProcessOutput(buf, size, arena, status);
if (!upb_Status_IsOk(status)) return;

const char* ptr = sv.data;
size_t len = sv.size;
while (len) {
int n = write(1, ptr, len);
if (n > 0) {
ptr += n;
len -= n;
}
}
}
14 changes: 9 additions & 5 deletions upbc/upbdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ extern "C" {

// Consume |buf|, deserialize it to a Code_Generator_Request proto, construct a
// upbc_Code_Generator_Request, and return it as a JSON-encoded string.
upb_StringView upbdev_ProcessInput(const char* buf, size_t size, upb_Arena* a,
upb_Status* status);
upb_StringView upbdev_ProcessInput(const char* buf, size_t size,
upb_Arena* arena, upb_Status* status);

// Deserialize |buf| from JSON then serialize it to wire format and return.
upb_StringView upbdev_ProcessOutput(const char* buf, size_t size, upb_Arena* a,
upb_Status* status);
// Decode |buf| from JSON, serialize to wire format, and return it.
upb_StringView upbdev_ProcessOutput(const char* buf, size_t size,
upb_Arena* arena, upb_Status* status);

// Decode |buf| from JSON, serialize to wire format, and write it to stdout.
void upbdev_ProcessStdout(const char* buf, size_t size, upb_Arena* arena,
upb_Status* status);

#ifdef __cplusplus
} /* extern "C" */
Expand Down

0 comments on commit f5f8675

Please sign in to comment.