blob: 4246919564acbba8d2e883fccbdbbfc3f1dbe2d4 [file] [log] [blame]
Adam Cozzette501ecec2023-09-26 14:36:20 -07001// Protocol Buffers - Google's data interchange format
2// Copyright 2023 Google LLC. All rights reserved.
Adam Cozzette501ecec2023-09-26 14:36:20 -07003//
Hong Shinb837d172023-11-07 13:24:59 -08004// 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 Cozzette501ecec2023-09-26 14:36:20 -07007
Adam Cozzette12c7bb02023-09-28 12:54:11 -07008#include "upb_generator/upbdev.h"
Adam Cozzette501ecec2023-09-26 14:36:20 -07009
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 Salob997cb62023-12-21 08:09:54 -080022#include "upb/base/upcast.h"
Adam Cozzette501ecec2023-09-26 14:36:20 -070023#include "upb/json/decode.h"
24#include "upb/json/encode.h"
25#include "upb/mem/arena.h"
Adam Cozzette12c7bb02023-09-28 12:54:11 -070026#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 Cozzette501ecec2023-09-26 14:36:20 -070029
30static 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 Salob997cb62023-12-21 08:09:54 -080038 (void)upb_JsonDecode(data, size, UPB_UPCAST(response), m, s, 0, arena,
39 status);
Adam Cozzette501ecec2023-09-26 14:36:20 -070040 if (!upb_Status_IsOk(status)) return NULL;
41
42 upb_DefPool_Free(s);
43
44 return response;
45}
46
Adam Cozzette12c7bb02023-09-28 12:54:11 -070047static upb_StringView upbc_JsonEncode(const upb_CodeGeneratorRequest* request,
Adam Cozzette501ecec2023-09-26 14:36:20 -070048 upb_Arena* arena, upb_Status* status) {
49 upb_StringView out = {.data = NULL, .size = 0};
50
51 upb_DefPool* s = upb_DefPool_New();
Adam Cozzette12c7bb02023-09-28 12:54:11 -070052 const upb_MessageDef* m = upb_CodeGeneratorRequest_getmsgdef(s);
Adam Cozzette501ecec2023-09-26 14:36:20 -070053 const int options = upb_JsonEncode_FormatEnumsAsIntegers;
54
Eric Salob997cb62023-12-21 08:09:54 -080055 out.size =
56 upb_JsonEncode(UPB_UPCAST(request), m, s, options, NULL, 0, status);
Adam Cozzette501ecec2023-09-26 14:36:20 -070057 if (!upb_Status_IsOk(status)) goto done;
58
59 char* data = (char*)upb_Arena_Malloc(arena, out.size + 1);
60
Eric Salob997cb62023-12-21 08:09:54 -080061 (void)upb_JsonEncode(UPB_UPCAST(request), m, s, options, data, out.size + 1,
62 status);
Adam Cozzette501ecec2023-09-26 14:36:20 -070063 if (!upb_Status_IsOk(status)) goto done;
64
65 out.data = (const char*)data;
66
67done:
68 upb_DefPool_Free(s);
69 return out;
70}
71
72upb_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 Cozzette12c7bb02023-09-28 12:54:11 -070079 const upb_CodeGeneratorRequest* outer_request =
Adam Cozzette501ecec2023-09-26 14:36:20 -070080 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 Salo7a1c9262024-04-25 08:53:50 -070086static upb_StringView upbdev_ProcessOutput(const char* buf, size_t size,
87 upb_Arena* arena,
88 upb_Status* status) {
Adam Cozzette501ecec2023-09-26 14:36:20 -070089 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
100void 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 Bot19c800c2023-12-27 12:29:51 -0800116upb_Arena* upbdev_Arena_New(void) { return upb_Arena_New(); }
Adam Cozzette501ecec2023-09-26 14:36:20 -0700117
118void upbdev_Status_Clear(upb_Status* status) { upb_Status_Clear(status); }