MCPcopy Create free account
hub / github.com/mapbox/pbf / writeMessage

Function writeMessage

compile.js:25–61  ·  view source on GitHub ↗
(ctx, options)

Source from the content-addressed store, hash-verified

23}
24
25function writeMessage(ctx, options) {
26 const fields = ctx._proto.fields;
27
28 let code = '\n';
29
30 if (!options.noRead) {
31 const readName = `read${ctx._name}`;
32
33 code += `${writeFunctionExport(options, readName)}function ${readName}(pbf, end) {\n`;
34 if (fields.length === 0) {
35 code += ' pbf.pos = end;\n return {};\n';
36 } else {
37 code += ` const obj = ${compileDest(ctx)};\n`;
38 code += ' let field;\n';
39 code += ' while ((field = pbf.nextField(end))) {\n';
40
41 for (let i = 0; i < fields.length; i++) {
42 code += ` ${i ? 'else ' : ''}if (field === ${fields[i].tag}) ${compileFieldRead(ctx, fields[i])}\n`;
43 }
44 code += ' }\n return obj;\n';
45 }
46 code += '}\n';
47 }
48
49 if (!options.noWrite) {
50 const writeName = `write${ctx._name}`;
51 code += `${writeFunctionExport(options, writeName)}function ${writeName}(obj, pbf) {`;
52 if (fields.length === 0) {
53 code += '}\n';
54 } else {
55 code += '\n';
56 for (const field of fields) code += compileFieldWriteLine(ctx, field);
57 code += '}\n';
58 }
59 }
60 return code;
61}
62
63function compileFieldWriteLine(ctx, field) {
64 const v = `obj.${field.name}`;

Callers 1

writeContextFunction · 0.85

Calls 4

writeFunctionExportFunction · 0.85
compileDestFunction · 0.85
compileFieldReadFunction · 0.85
compileFieldWriteLineFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…