Skip to content

Commit 512af6f

Browse files
committed
simplify
Signed-off-by: xil <fridalu66@gmail.com>
1 parent 4cdf7fb commit 512af6f

4 files changed

Lines changed: 68 additions & 96 deletions

File tree

protos/generated/services/default_service.proto

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ option java_multiple_files = true;
1212
option java_outer_classname = "CommonProto";
1313
option java_package = "org.opensearch.protobufs";
1414

15-
service DefaultService {
16-
rpc Bulk (BulkRequest) returns (BulkResponse);
17-
18-
rpc Search (SearchRequest) returns (SearchResponse);
19-
20-
}
21-
2215
message BulkRequest {
2316
repeated BulkRequestBody bulk_request_body = 1;
2417

tools/proto-convert/src/postprocessing/BackwardCompatibleWriter.ts

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { existsSync, readFileSync, writeFileSync } from 'fs';
2-
import { join } from 'path';
1+
import { existsSync } from 'fs';
32
import { Command, Option } from '@commander-js/extra-typings';
43
import {
54
ProtoMessage,
@@ -8,15 +7,9 @@ import {
87
} from './types';
98
import { parseProtoFile } from './parser';
109
import { mergeMessage, mergeEnum } from './CompatibilityMerger';
11-
import { generateMessage, generateEnum } from './writer';
10+
import { writeProtoFile, CUSTOM_MESSAGE_NAMES, CUSTOM_ENUM_NAMES } from './writer';
1211
import logger from '../utils/logger';
1312

14-
const TEMPLATE_DIR = join(__dirname, '../config/protobuf-schema-template');
15-
16-
// Load fixed header and custom messages from templates
17-
const PROTO_HEADER = readFileSync(join(TEMPLATE_DIR, 'partial_header.mustache'), 'utf-8');
18-
const CUSTOM_MESSAGES = readFileSync(join(TEMPLATE_DIR, 'custom_message.mustache'), 'utf-8');
19-
2013
// ==================== CLI ====================
2114

2215
const command = new Command()
@@ -37,10 +30,6 @@ type BackwardCompatOpts = {
3730

3831
const opts = command.opts() as BackwardCompatOpts;
3932

40-
// Messages defined in custom_message.mustache - skip and use template instead
41-
const CUSTOM_MESSAGE_NAMES = new Set(['ObjectMap', 'GeneralNumber']);
42-
const CUSTOM_ENUM_NAMES = new Set(['NullValue']);
43-
4433
export class BackwardCompatibleWriter {
4534
private existingMessages: ProtoMessage[];
4635
private existingEnums: ProtoEnum[];
@@ -78,63 +67,55 @@ export class BackwardCompatibleWriter {
7867
}
7968

8069
process(): void {
81-
const outputParts: string[] = [];
70+
const finalMessages: ProtoMessage[] = [];
71+
const finalEnums: ProtoEnum[] = [];
8272

83-
// Use fixed header from template
84-
outputParts.push(PROTO_HEADER.trim());
85-
86-
// Process messages
73+
// Process existing messages (merge with incoming if present)
8774
for (const existingMsg of this.existingMessages) {
8875
if (CUSTOM_MESSAGE_NAMES.has(existingMsg.name)) {
8976
this.incomingMessageMap.delete(existingMsg.name);
9077
continue;
9178
}
9279

9380
const incomingMsg = this.incomingMessageMap.get(existingMsg.name);
94-
9581
if (incomingMsg) {
96-
const mergedMsg = mergeMessage(existingMsg, incomingMsg, this.errors);
97-
outputParts.push(generateMessage(mergedMsg));
82+
finalMessages.push(mergeMessage(existingMsg, incomingMsg, this.errors));
9883
this.incomingMessageMap.delete(existingMsg.name);
9984
} else {
100-
outputParts.push(generateMessage(existingMsg));
85+
finalMessages.push(existingMsg);
10186
}
10287
}
10388

104-
// Process enums
89+
// Process existing enums (merge with incoming if present)
10590
for (const existingEnum of this.existingEnums) {
10691
if (CUSTOM_ENUM_NAMES.has(existingEnum.name)) {
10792
this.incomingEnumMap.delete(existingEnum.name);
10893
continue;
10994
}
11095

11196
const incomingEnum = this.incomingEnumMap.get(existingEnum.name);
112-
11397
if (incomingEnum) {
114-
const mergedEnum = mergeEnum(existingEnum, incomingEnum);
115-
outputParts.push(generateEnum(mergedEnum));
98+
finalEnums.push(mergeEnum(existingEnum, incomingEnum));
11699
this.incomingEnumMap.delete(existingEnum.name);
117100
} else {
118-
outputParts.push(generateEnum(existingEnum));
101+
finalEnums.push(existingEnum);
119102
}
120103
}
121104

122-
// Append new messages from incoming proto files
105+
// Add new messages from incoming (not in existing)
123106
for (const [, msg] of this.incomingMessageMap) {
124-
outputParts.push('');
125-
outputParts.push(generateMessage(msg));
107+
if (!CUSTOM_MESSAGE_NAMES.has(msg.name)) {
108+
finalMessages.push(msg);
109+
}
126110
}
127111

128-
// Append new enums from incoming proto files
112+
// Add new enums from incoming (not in existing)
129113
for (const [, protoEnum] of this.incomingEnumMap) {
130-
outputParts.push('');
131-
outputParts.push(generateEnum(protoEnum));
114+
if (!CUSTOM_ENUM_NAMES.has(protoEnum.name)) {
115+
finalEnums.push(protoEnum);
116+
}
132117
}
133118

134-
// Append custom messages from template (ObjectMap, GeneralNumber, NullValue)
135-
outputParts.push('');
136-
outputParts.push(CUSTOM_MESSAGES.trim());
137-
138119
// Check for errors before writing
139120
if (this.errors.length > 0) {
140121
logger.error('Backward compatibility errors:');
@@ -146,7 +127,8 @@ export class BackwardCompatibleWriter {
146127
);
147128
}
148129

149-
writeFileSync(this.outputPath, outputParts.join('\n'));
130+
// Write output using shared function
131+
writeProtoFile(finalMessages, finalEnums, this.outputPath);
150132
logger.info(`Updated: ${this.outputPath}`);
151133
}
152134
}

tools/proto-convert/src/postprocessing/CleanupUnusedMessages.ts

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,13 @@
44
* (directly or indirectly) by any root message.
55
*/
66

7-
import { existsSync, readFileSync, writeFileSync } from 'fs';
8-
import { join } from 'path';
7+
import { existsSync } from 'fs';
98
import { Command, Option } from '@commander-js/extra-typings';
109
import { parseProtoFile } from './parser';
11-
import { generateMessage, generateEnum } from './writer';
10+
import { writeProtoFile, CUSTOM_MESSAGE_NAMES, CUSTOM_ENUM_NAMES } from './writer';
1211
import { ProtoMessage, ProtoEnum } from './types';
1312
import logger from '../utils/logger';
1413

15-
const TEMPLATE_DIR = join(__dirname, '../config/protobuf-schema-template');
16-
17-
// Load fixed header and custom messages from templates
18-
const PROTO_HEADER = readFileSync(join(TEMPLATE_DIR, 'partial_header.mustache'), 'utf-8');
19-
const CUSTOM_MESSAGES = readFileSync(join(TEMPLATE_DIR, 'custom_message.mustache'), 'utf-8');
20-
21-
// Custom messages/enums defined in template - always included at end
22-
const CUSTOM_MESSAGE_NAMES = new Set(['ObjectMap', 'GeneralNumber']);
23-
const CUSTOM_ENUM_NAMES = new Set(['NullValue']);
24-
2514
export function isBuiltInType(type: string): boolean {
2615
const builtIns = new Set([
2716
'double', 'float', 'int32', 'int64', 'uint32', 'uint64',
@@ -159,7 +148,7 @@ if (require.main === module) {
159148
process.exit(1);
160149
}
161150

162-
// Get roots: from --roots if provided, otherwise from service file
151+
// Get roots
163152
let roots: string[];
164153
if (opts.roots && opts.roots.length > 0) {
165154
roots = opts.roots;
@@ -190,42 +179,8 @@ if (require.main === module) {
190179
const keptMessages = filterMessages(parsed.messages, reachable);
191180
const keptEnums = filterEnums(parsed.enums, reachable);
192181

193-
const removedMessages = parsed.messages.length - keptMessages.length;
194-
const removedEnums = parsed.enums.length - keptEnums.length;
195-
196-
if (removedMessages === 0 && removedEnums === 0) {
197-
logger.info('No unused messages or enums found.');
198-
process.exit(0);
199-
}
200-
201-
logger.info(`Removing ${removedMessages} unused messages, ${removedEnums} unused enums.`);
202-
203-
// Generate output
204-
const outputParts: string[] = [];
205-
206-
// Use fixed header from template
207-
outputParts.push(PROTO_HEADER.trim());
208-
209-
// Generate messages (excluding custom ones - they're added from template)
210-
for (const msg of keptMessages) {
211-
if (!CUSTOM_MESSAGE_NAMES.has(msg.name)) {
212-
outputParts.push(generateMessage(msg));
213-
}
214-
}
215-
216-
// Generate enums (excluding custom ones)
217-
for (const e of keptEnums) {
218-
if (!CUSTOM_ENUM_NAMES.has(e.name)) {
219-
outputParts.push(generateEnum(e));
220-
}
221-
}
222-
223-
// Append custom messages from template
224-
outputParts.push('');
225-
outputParts.push(CUSTOM_MESSAGES.trim());
226-
182+
// Write output
227183
const outputPath = opts.output || opts.input;
228-
writeFileSync(outputPath, outputParts.join('\n'));
229-
184+
writeProtoFile(keptMessages, keptEnums, outputPath);
230185
logger.info(`Updated: ${outputPath}`);
231186
}

tools/proto-convert/src/postprocessing/writer.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22
* Writer module: Generate .proto file output using Mustache templates.
33
*/
44

5-
import { readFileSync } from 'fs';
5+
import { readFileSync, writeFileSync } from 'fs';
66
import { join } from 'path';
77
import { render } from 'mustache';
88
import { ProtoMessage, ProtoEnum } from './types';
99

1010
const TEMPLATE = readFileSync(join(__dirname, 'templates', 'proto.mustache'), 'utf8');
1111

12+
const TEMPLATE_DIR = join(__dirname, '../config/protobuf-schema-template');
13+
const PROTO_HEADER = readFileSync(join(TEMPLATE_DIR, 'partial_header.mustache'), 'utf-8');
14+
const CUSTOM_MESSAGES = readFileSync(join(TEMPLATE_DIR, 'custom_message.mustache'), 'utf-8');
15+
16+
// Custom messages/enums defined in template - always handled separately
17+
export const CUSTOM_MESSAGE_NAMES = new Set(['ObjectMap', 'GeneralNumber']);
18+
export const CUSTOM_ENUM_NAMES = new Set(['NullValue']);
19+
1220
/**
1321
* Split a comment into lines for template rendering.
1422
*/
@@ -75,3 +83,37 @@ export function generateEnum(protoEnum: ProtoEnum): string {
7583
const data = prepareEnumData(protoEnum);
7684
return render(TEMPLATE, data).trimEnd();
7785
}
86+
87+
/**
88+
* Write a complete proto file with header, messages, enums, and custom messages.
89+
*/
90+
export function writeProtoFile(
91+
messages: ProtoMessage[],
92+
enums: ProtoEnum[],
93+
outputPath: string
94+
): void {
95+
const outputParts: string[] = [];
96+
97+
// Use fixed header from template
98+
outputParts.push(PROTO_HEADER.trim());
99+
100+
// Generate messages (excluding custom ones - they're added from template)
101+
for (const msg of messages) {
102+
if (!CUSTOM_MESSAGE_NAMES.has(msg.name)) {
103+
outputParts.push(generateMessage(msg));
104+
}
105+
}
106+
107+
// Generate enums (excluding custom ones)
108+
for (const e of enums) {
109+
if (!CUSTOM_ENUM_NAMES.has(e.name)) {
110+
outputParts.push(generateEnum(e));
111+
}
112+
}
113+
114+
// Append custom messages from template
115+
outputParts.push('');
116+
outputParts.push(CUSTOM_MESSAGES.trim());
117+
118+
writeFileSync(outputPath, outputParts.join('\n'));
119+
}

0 commit comments

Comments
 (0)