UNPKG

@whisklabs/grpc

Version:

gRPC generator and http library for typescript

118 lines 5.99 kB
import { isBoolean, isPresent, isString, isText } from '@whisklabs/typeguards'; import { GOOGLE_WRAPPERS, OPTION_MESSAGE_REQUIRED } from './constants'; import { enums } from './enum'; import { getField, getStruct, isRequiredField } from './field'; import { camelCase, checkSame, errorColor, joinPath, safeString, toComment } from './utils'; export function messages(pack, out, items, messageRequired, list, parent) { if (list === void 0) { list = []; } var _loop_1 = function (msg) { var newPack = joinPath(parent !== null && parent !== void 0 ? parent : pack, msg.name); list = list.concat(msg.messages.map(function (i) { return ({ name: i.name, pack: newPack }); }), msg.enums.map(function (i) { return ({ name: i.name, pack: newPack }); })); message(pack, out, msg, list, messageRequired, parent); if (msg.messages.length > 0) { messages(pack, out, msg.messages, messageRequired, list, newPack); } }; for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { var msg = items_1[_i]; _loop_1(msg); } } // eslint-disable-next-line complexity function message(pack, out, item, list, messageRequired, parent) { var _a; var option = item.options[OPTION_MESSAGE_REQUIRED]; var isMessageRequired = isBoolean(option) ? option : messageRequired; var base = joinPath(parent !== null && parent !== void 0 ? parent : pack, item.name); enums(base, out, item.enums); var runtime = []; var baseName = safeString(base); var oneof = {}; // const massagesNames = item.messages.map(m => m.name); var packName = "".concat(pack, ".").concat(item.name); var cID = checkSame(out, 'id'); var cName = checkSame(out, 'name'); if (isString(GOOGLE_WRAPPERS[packName])) { var field = item.fields[0]; if (!(isPresent(field) && field.name === 'value' && field.tag === 1 && field.type === GOOGLE_WRAPPERS[packName])) { out.errors.push(errorColor("Incorrect google wrapper ".concat(packName), field.name, "".concat(field.type, ", ").concat(field.tag))); } } if (item.fields.length > 0) { out.dts.push("export type ".concat(baseName, " = {")); for (var _i = 0, _b = item.fields; _i < _b.length; _i++) { var field = _b[_i]; var fieldPack = getFieldPack(list, field, baseName, pack); var naming = camelCase(field.name); var required = isRequiredField(field, isMessageRequired); if (isText(field.oneof)) { field.oneof = camelCase(field.oneof); oneof[field.oneof] = (_a = oneof[field.oneof]) !== null && _a !== void 0 ? _a : []; oneof[field.oneof].push(field); } else { getComment(field, out); var fieldName = getField(field, fieldPack, "in \"".concat(baseName, "\" field \"").concat(field.name, " = ").concat(field.tag, "\""), out, baseName); out.dts.push(" ".concat(naming).concat(required ? '' : '?', ": ").concat(fieldName).concat(field.repeated ? '[]' : '', ";")); } var type = getStruct(field, fieldPack, out, baseName); runtime.push(" [".concat(field.tag, ", \"").concat(naming, "\", ").concat(type, ", ").concat(required ? '1' : '0').concat(isText(field.oneof) ? ", \"".concat(field.oneof, "\"") : '', "],")); var pathF = "".concat(isString(parent) ? "".concat(parent, ".") : '').concat(packName, ".").concat(field.name); cID(field.tag, pathF); cName(naming, pathF); } for (var one in oneof) { out.dts.push(" ".concat(one, "?:")); for (var _c = 0, _d = oneof[one]; _c < _d.length; _c++) { var field = _d[_c]; var fieldPack = getFieldPack(list, field, baseName, pack); getComment(field, out); var naming = camelCase(field.name); var fieldName = getField(field, fieldPack, "in \"".concat(baseName, "\" field \"").concat(field.name, " = ").concat(field.tag, "\""), out, baseName); out.dts.push(" | { oneof: '".concat(naming, "'; value").concat(isRequiredField(field, isMessageRequired) ? '' : '?', ": ").concat(fieldName).concat(field.repeated ? '[]' : '', "; }")); } out.dts[out.dts.length - 1] += ';'; } out.dts.push('};'); } else { out.dts.push("export type ".concat(baseName, " = FieldEmpty;")); } if (isText(item.comment)) { out.dts.push(toComment(item.comment)); } out.dts.push("export const ".concat(baseName, ": Field<").concat(baseName, ">;")); out.js.push("export function ".concat(baseName, " () { return [")); for (var _e = 0, runtime_1 = runtime; _e < runtime_1.length; _e++) { var field = runtime_1[_e]; out.js.push(field); } out.js.push(']; }'); } function getComment(field, out) { var comment = ''; if (isText(field.options.deprecated)) { comment += "@deprecated ".concat(field.options.deprecated, "\n"); } else if (field.options.deprecated === true) { comment += '@deprecated\n'; } if (isText(field.comment)) { comment += field.comment; } if (isText(comment)) { out.dts.push(toComment(comment)); } } function getFieldPack(list, field, baseName, pack) { // baseName for avoid duplicate name var find = list.find(function (i) { var _a; return i.name === ((_a = field.map) === null || _a === void 0 ? void 0 : _a.to) || (i.name === field.type && baseName === safeString(i.pack)); }); if (isPresent(find)) { return find.pack; } else { var findTop = list.find(function (i) { var _a; return i.name === ((_a = field.map) === null || _a === void 0 ? void 0 : _a.to) || i.name === field.type; }); return isPresent(findTop) ? findTop.pack : pack; } } //# sourceMappingURL=message.js.map