@whisklabs/grpc
Version:
gRPC generator and http library for typescript
86 lines • 3.38 kB
JavaScript
import { isPresent, isString } from '@whisklabs/typeguards';
import { GOOGLE_WRAPPERS, TYPES } from './constants';
import { joinPath, safeString } from './utils';
export var getField = function (field, base, name, out, baseName) {
var _a, _b;
var type = field.type;
if (type === 'map') {
var to = getField({ type: (_a = field.map) === null || _a === void 0 ? void 0 : _a.to }, base, name, out, baseName);
var from = 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 (isString(TYPES[type])) {
return TYPES[type];
}
else if (isString(TYPES[GOOGLE_WRAPPERS[type]])) {
return TYPES[GOOGLE_WRAPPERS[type]];
}
else {
var sName = pathField(type, base, out, baseName);
out.fields.push([sName, name]);
return out.enumsList.has(sName) ? "".concat(sName) : sName;
}
};
export var getStruct = function (field, base, out, baseName) {
var _a, _b;
var type = field.type;
if (type === 'map') {
var from = getStruct({ type: (_a = field.map) === null || _a === void 0 ? void 0 : _a.from }, base, out, baseName);
var to = 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(getStruct({ type: type }, base, out, baseName), "]");
}
else if (isString(TYPES[type])) {
return "\"".concat(type, "\"");
}
else if (isString(GOOGLE_WRAPPERS[type])) {
return "[\"wrapper\", \"".concat(GOOGLE_WRAPPERS[type], "\"]");
}
else {
var sName = pathField(field.type, base, out, baseName);
return out.enumsList.has(sName) ? '"enum"' : sName;
}
};
export 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 safeString(field);
}
// Relative path with recursive check
if (isString(parent) && field.indexOf('.') > -1) {
var safeBase = 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 = joinPath(base, current, field);
if (out.names.has(tryName)) {
return tryName;
}
par.pop();
}
}
if (out.names.has(field)) {
return joinPath(field);
}
// Common path
return joinPath(base, field);
};
export var isRequiredField = function (field, optional) {
return isPresent(field.options.required)
? field.options.required !== false
: field.optional
? false
: optional === true
? true
: field.required || field.repeated || field.type === 'map' || isString(TYPES[field.type]);
};
export var isEnumField = function (field, base, out, parent) {
return out.enumsList.has(pathField(field.type, base, out, parent));
};
//# sourceMappingURL=field.js.map