UNPKG

@cosmology/ast

Version:
169 lines (168 loc) 7.62 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromSDKJSONMethod = exports.fromSDKJSONMethodFields = void 0; const t = __importStar(require("@babel/types")); const __1 = require(".."); const utils_1 = require("../../../utils"); const utils_2 = require("./utils"); const needsImplementation = (name, field) => { throw new Error(`need to implement fromSDKJSON (${field.type} rules[${field.rule}] name[${name}])`); }; const fromSDKJSONMethodFields = (context, name, proto) => { const oneOfs = (0, __1.getOneOfs)(proto); const fields = Object.keys(proto.fields ?? {}).map((fieldName) => { const field = { name: fieldName, ...proto.fields[fieldName] }; const isOneOf = oneOfs.includes(fieldName); const isOptional = (0, __1.getFieldOptionalityForDefaults)(context, field, isOneOf); const args = { context, field, isOneOf, isOptional }; if (field.rule === 'repeated') { switch (field.type) { case 'string': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.string()); case 'bytes': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.bytes(args)); case 'bool': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.bool()); case 'float': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.float()); case 'double': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.double()); case 'int32': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.int32()); case 'sint32': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.sint32()); case 'uint32': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.uint32()); case 'fixed32': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.fixed32()); case 'sfixed32': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.sfixed32()); case 'int64': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.int64(args)); case 'sint64': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.sint64(args)); case 'uint64': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.uint64(args)); case 'fixed64': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.fixed64(args)); case 'sfixed64': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.sfixed64(args)); default: switch (field.parsedType.type) { case 'Enum': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.enum(args)); case 'Type': return utils_2.fromSDKJSON.array(args, utils_2.arrayTypes.type(args)); } return needsImplementation(fieldName, field); } } if (field.keyType) { switch (field.keyType) { case 'string': case 'int64': case 'sint64': case 'uint64': case 'fixed64': case 'sfixed64': case 'int32': case 'sint32': case 'uint32': case 'fixed32': case 'sfixed32': return utils_2.fromSDKJSON.keyHash(args); default: return needsImplementation(fieldName, field); } } switch (field.type) { case 'string': return utils_2.fromSDKJSON.string(args); case 'bytes': return utils_2.fromSDKJSON.bytes(args); case 'bool': return utils_2.fromSDKJSON.bool(args); case 'double': return utils_2.fromSDKJSON.double(args); case 'float': return utils_2.fromSDKJSON.float(args); case 'int32': return utils_2.fromSDKJSON.int32(args); case 'sint32': return utils_2.fromSDKJSON.sint32(args); case 'uint32': return utils_2.fromSDKJSON.uint32(args); case 'fixed32': return utils_2.fromSDKJSON.fixed32(args); case 'sfixed32': return utils_2.fromSDKJSON.sfixed32(args); case 'int64': return utils_2.fromSDKJSON.int64(args); case 'sint64': return utils_2.fromSDKJSON.sint64(args); case 'uint64': return utils_2.fromSDKJSON.uint64(args); case 'fixed64': return utils_2.fromSDKJSON.fixed64(args); case 'sfixed64': return utils_2.fromSDKJSON.sfixed64(args); case 'Duration': case 'google.protobuf.Duration': return utils_2.fromSDKJSON.duration(args); case 'Timestamp': case 'google.protobuf.Timestamp': return utils_2.fromSDKJSON.timestamp(args); default: switch (field.parsedType.type) { case 'Enum': return utils_2.fromSDKJSON.enum(args); case 'Type': return utils_2.fromSDKJSON.type(args); } return needsImplementation(fieldName, field); } }); return fields; }; exports.fromSDKJSONMethodFields = fromSDKJSONMethodFields; const fromSDKJSONMethod = (context, name, proto) => { const fields = (0, exports.fromSDKJSONMethodFields)(context, name, proto); let varName = 'object'; if (!fields.length) { varName = '_'; } const SDKTypeName = [name, 'SDKType'].filter(Boolean).join(''); return (0, utils_1.objectMethod)('method', t.identifier('fromSDKJSON'), [(0, utils_1.identifier)(varName, t.tsTypeAnnotation(t.tsAnyKeyword()), false)], t.blockStatement([t.returnStatement(t.objectExpression(fields))]), false, false, false, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(SDKTypeName)))); }; exports.fromSDKJSONMethod = fromSDKJSONMethod;