UNPKG

@whisklabs/grpc

Version:

gRPC generator and http library for typescript

94 lines 4.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isEnumField = exports.isRequiredField = exports.pathField = exports.getStruct = exports.getField = void 0; var typeguards_1 = require("@whisklabs/typeguards"); var constants_1 = require("./constants"); var utils_1 = require("./utils"); var getField = function (field, base, name, out, baseName) { var _a, _b; var type = field.type; if (type === 'map') { var to = (0, exports.getField)({ type: (_a = field.map) === null || _a === void 0 ? void 0 : _a.to }, base, name, out, baseName); var from = (0, exports.getField)({ type: (_b = field.map) === null || _b === void 0 ? void 0 : _b.from }, base, name, out, baseName); return "Record<".concat(from, ", ").concat(to, ">"); } else if ((0, typeguards_1.isString)(constants_1.TYPES[type])) { return constants_1.TYPES[type]; } else if ((0, typeguards_1.isString)(constants_1.TYPES[constants_1.GOOGLE_WRAPPERS[type]])) { return constants_1.TYPES[constants_1.GOOGLE_WRAPPERS[type]]; } else { var sName = (0, exports.pathField)(type, base, out, baseName); out.fields.push([sName, name]); return out.enumsList.has(sName) ? "".concat(sName) : sName; } }; exports.getField = getField; var getStruct = function (field, base, out, baseName) { var _a, _b; var type = field.type; if (type === 'map') { var from = (0, exports.getStruct)({ type: (_a = field.map) === null || _a === void 0 ? void 0 : _a.from }, base, out, baseName); var to = (0, exports.getStruct)({ type: (_b = field.map) === null || _b === void 0 ? void 0 : _b.to }, base, out, baseName); return "[\"map\", ".concat(from, ", ").concat(to, "]"); } else if (field.repeated) { return "[\"repeated\", ".concat((0, exports.getStruct)({ type: type }, base, out, baseName), "]"); } else if ((0, typeguards_1.isString)(constants_1.TYPES[type])) { return "\"".concat(type, "\""); } else if ((0, typeguards_1.isString)(constants_1.GOOGLE_WRAPPERS[type])) { return "[\"wrapper\", \"".concat(constants_1.GOOGLE_WRAPPERS[type], "\"]"); } else { var sName = (0, exports.pathField)(field.type, base, out, baseName); return out.enumsList.has(sName) ? '"enum"' : sName; } }; exports.getStruct = getStruct; var pathField = function (field, base, out, parent) { var inRoot = false; out.packagesList.forEach(function (root) { inRoot = inRoot || (root.length > 0 && field.startsWith(root)); }); // Absolute path if (inRoot) { return (0, utils_1.safeString)(field); } // Relative path with recursive check if ((0, typeguards_1.isString)(parent) && field.indexOf('.') > -1) { var safeBase = (0, utils_1.safeString)(base); var par = (parent.length > 0 && parent.startsWith(safeBase) ? parent.slice(safeBase.length + 1) : parent).split('_'); while (par.length) { var current = par.join('_'); var tryName = (0, utils_1.joinPath)(base, current, field); if (out.names.has(tryName)) { return tryName; } par.pop(); } } if (out.names.has(field)) { return (0, utils_1.joinPath)(field); } // Common path return (0, utils_1.joinPath)(base, field); }; exports.pathField = pathField; var isRequiredField = function (field, optional) { return (0, typeguards_1.isPresent)(field.options.required) ? field.options.required !== false : field.optional ? false : optional === true ? true : field.required || field.repeated || field.type === 'map' || (0, typeguards_1.isString)(constants_1.TYPES[field.type]); }; exports.isRequiredField = isRequiredField; var isEnumField = function (field, base, out, parent) { return out.enumsList.has((0, exports.pathField)(field.type, base, out, parent)); }; exports.isEnumField = isEnumField; //# sourceMappingURL=field.js.map