Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2023 Google LLC. All rights reserved. |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 3 | // |
Hong Shin | b837d17 | 2023-11-07 13:24:59 -0800 | [diff] [blame] | 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file or at |
| 6 | // https://developers.google.com/open-source/licenses/bsd |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 7 | |
Adam Cozzette | 12c7bb0 | 2023-09-28 12:54:11 -0700 | [diff] [blame] | 8 | #include "upb_generator/upbdev.h" |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 9 | |
| 10 | #ifdef _WIN32 |
| 11 | #ifndef WIN32_LEAN_AND_MEAN |
| 12 | #define WIN32_LEAN_AND_MEAN |
| 13 | #endif |
| 14 | #include <windows.h> |
| 15 | #else // _WIN32 |
| 16 | #include <unistd.h> |
| 17 | #endif // !_WIN32 |
| 18 | |
| 19 | #include "google/protobuf/compiler/plugin.upb.h" |
| 20 | #include "google/protobuf/compiler/plugin.upbdefs.h" |
| 21 | #include "upb/base/status.h" |
Eric Salo | b997cb6 | 2023-12-21 08:09:54 -0800 | [diff] [blame] | 22 | #include "upb/base/upcast.h" |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 23 | #include "upb/json/decode.h" |
| 24 | #include "upb/json/encode.h" |
| 25 | #include "upb/mem/arena.h" |
Adam Cozzette | 12c7bb0 | 2023-09-28 12:54:11 -0700 | [diff] [blame] | 26 | #include "upb_generator/code_generator_request.h" |
| 27 | #include "upb_generator/code_generator_request.upb.h" |
| 28 | #include "upb_generator/code_generator_request.upbdefs.h" |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 29 | |
| 30 | static google_protobuf_compiler_CodeGeneratorResponse* upbc_JsonDecode( |
| 31 | const char* data, size_t size, upb_Arena* arena, upb_Status* status) { |
| 32 | google_protobuf_compiler_CodeGeneratorResponse* response = |
| 33 | google_protobuf_compiler_CodeGeneratorResponse_new(arena); |
| 34 | |
| 35 | upb_DefPool* s = upb_DefPool_New(); |
| 36 | const upb_MessageDef* m = google_protobuf_compiler_CodeGeneratorResponse_getmsgdef(s); |
| 37 | |
Eric Salo | b997cb6 | 2023-12-21 08:09:54 -0800 | [diff] [blame] | 38 | (void)upb_JsonDecode(data, size, UPB_UPCAST(response), m, s, 0, arena, |
| 39 | status); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 40 | if (!upb_Status_IsOk(status)) return NULL; |
| 41 | |
| 42 | upb_DefPool_Free(s); |
| 43 | |
| 44 | return response; |
| 45 | } |
| 46 | |
Adam Cozzette | 12c7bb0 | 2023-09-28 12:54:11 -0700 | [diff] [blame] | 47 | static upb_StringView upbc_JsonEncode(const upb_CodeGeneratorRequest* request, |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 48 | upb_Arena* arena, upb_Status* status) { |
| 49 | upb_StringView out = {.data = NULL, .size = 0}; |
| 50 | |
| 51 | upb_DefPool* s = upb_DefPool_New(); |
Adam Cozzette | 12c7bb0 | 2023-09-28 12:54:11 -0700 | [diff] [blame] | 52 | const upb_MessageDef* m = upb_CodeGeneratorRequest_getmsgdef(s); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 53 | const int options = upb_JsonEncode_FormatEnumsAsIntegers; |
| 54 | |
Eric Salo | b997cb6 | 2023-12-21 08:09:54 -0800 | [diff] [blame] | 55 | out.size = |
| 56 | upb_JsonEncode(UPB_UPCAST(request), m, s, options, NULL, 0, status); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 57 | if (!upb_Status_IsOk(status)) goto done; |
| 58 | |
| 59 | char* data = (char*)upb_Arena_Malloc(arena, out.size + 1); |
| 60 | |
Eric Salo | b997cb6 | 2023-12-21 08:09:54 -0800 | [diff] [blame] | 61 | (void)upb_JsonEncode(UPB_UPCAST(request), m, s, options, data, out.size + 1, |
| 62 | status); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 63 | if (!upb_Status_IsOk(status)) goto done; |
| 64 | |
| 65 | out.data = (const char*)data; |
| 66 | |
| 67 | done: |
| 68 | upb_DefPool_Free(s); |
| 69 | return out; |
| 70 | } |
| 71 | |
| 72 | upb_StringView upbdev_ProcessInput(const char* buf, size_t size, |
| 73 | upb_Arena* arena, upb_Status* status) { |
| 74 | upb_StringView out = {.data = NULL, .size = 0}; |
| 75 | |
| 76 | google_protobuf_compiler_CodeGeneratorRequest* inner_request = |
| 77 | google_protobuf_compiler_CodeGeneratorRequest_parse(buf, size, arena); |
| 78 | |
Adam Cozzette | 12c7bb0 | 2023-09-28 12:54:11 -0700 | [diff] [blame] | 79 | const upb_CodeGeneratorRequest* outer_request = |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 80 | upbc_MakeCodeGeneratorRequest(inner_request, arena, status); |
| 81 | if (!upb_Status_IsOk(status)) return out; |
| 82 | |
| 83 | return upbc_JsonEncode(outer_request, arena, status); |
| 84 | } |
| 85 | |
Eric Salo | 7a1c926 | 2024-04-25 08:53:50 -0700 | [diff] [blame] | 86 | static upb_StringView upbdev_ProcessOutput(const char* buf, size_t size, |
| 87 | upb_Arena* arena, |
| 88 | upb_Status* status) { |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 89 | upb_StringView out = {.data = NULL, .size = 0}; |
| 90 | |
| 91 | const google_protobuf_compiler_CodeGeneratorResponse* response = |
| 92 | upbc_JsonDecode(buf, size, arena, status); |
| 93 | if (!upb_Status_IsOk(status)) return out; |
| 94 | |
| 95 | out.data = google_protobuf_compiler_CodeGeneratorResponse_serialize(response, arena, |
| 96 | &out.size); |
| 97 | return out; |
| 98 | } |
| 99 | |
| 100 | void upbdev_ProcessStdout(const char* buf, size_t size, upb_Arena* arena, |
| 101 | upb_Status* status) { |
| 102 | const upb_StringView sv = upbdev_ProcessOutput(buf, size, arena, status); |
| 103 | if (!upb_Status_IsOk(status)) return; |
| 104 | |
| 105 | const char* ptr = sv.data; |
| 106 | size_t len = sv.size; |
| 107 | while (len) { |
| 108 | int n = write(1, ptr, len); |
| 109 | if (n > 0) { |
| 110 | ptr += n; |
| 111 | len -= n; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Protobuf Team Bot | 19c800c | 2023-12-27 12:29:51 -0800 | [diff] [blame] | 116 | upb_Arena* upbdev_Arena_New(void) { return upb_Arena_New(); } |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 117 | |
| 118 | void upbdev_Status_Clear(upb_Status* status) { upb_Status_Clear(status); } |