UNPKG

@cosmology/ast

Version:
190 lines (189 loc) 7.99 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.fromJSONMethod = exports.fromJSONMethodFields = void 0; const t = __importStar(require("@babel/types")); const __1 = require(".."); const utils_1 = require("../../../utils"); const utils_2 = require("./utils"); const strict_utils_1 = require("./strict-utils"); const needsImplementation = (name, field) => { throw new Error(`need to implement fromJSON (${field.type} rules[${field.rule}] name[${name}])`); }; const fromJSONMethodFields = (context, name, proto) => { const strictNullCheckForPrototypeMethods = context.pluginValue('prototypes.strictNullCheckForPrototypeMethods'); const fromJSON = strictNullCheckForPrototypeMethods ? strict_utils_1.fromJSON : utils_2.fromJSON; 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 fromJSON.array(args, utils_2.arrayTypes.string()); case 'bytes': return fromJSON.array(args, utils_2.arrayTypes.bytes(args)); case 'bool': return fromJSON.array(args, utils_2.arrayTypes.bool()); case 'float': return fromJSON.array(args, utils_2.arrayTypes.float()); case 'double': return fromJSON.array(args, utils_2.arrayTypes.double()); case 'int32': return fromJSON.array(args, utils_2.arrayTypes.int32()); case 'sint32': return fromJSON.array(args, utils_2.arrayTypes.sint32()); case 'uint32': return fromJSON.array(args, utils_2.arrayTypes.uint32()); case 'fixed32': return fromJSON.array(args, utils_2.arrayTypes.fixed32()); case 'sfixed32': return fromJSON.array(args, utils_2.arrayTypes.sfixed32()); case 'int64': return fromJSON.array(args, utils_2.arrayTypes.int64(args)); case 'sint64': return fromJSON.array(args, utils_2.arrayTypes.sint64(args)); case 'uint64': return fromJSON.array(args, utils_2.arrayTypes.uint64(args)); case 'fixed64': return fromJSON.array(args, utils_2.arrayTypes.fixed64(args)); case 'sfixed64': return fromJSON.array(args, utils_2.arrayTypes.sfixed64(args)); default: switch (field.parsedType.type) { case 'Enum': return fromJSON.array(args, utils_2.arrayTypes.enum(args)); case 'Type': return fromJSON.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 fromJSON.keyHash(args); default: return needsImplementation(fieldName, field); } } switch (field.type) { case 'string': return fromJSON.string(args); case 'bytes': return fromJSON.bytes(args); case 'bool': return fromJSON.bool(args); case 'double': return fromJSON.double(args); case 'float': return fromJSON.float(args); case 'int32': return fromJSON.int32(args); case 'sint32': return fromJSON.sint32(args); case 'uint32': return fromJSON.uint32(args); case 'fixed32': return fromJSON.fixed32(args); case 'sfixed32': return fromJSON.sfixed32(args); case 'int64': return fromJSON.int64(args); case 'sint64': return fromJSON.sint64(args); case 'uint64': return fromJSON.uint64(args); case 'fixed64': return fromJSON.fixed64(args); case 'sfixed64': return fromJSON.sfixed64(args); case 'Duration': case 'google.protobuf.Duration': return fromJSON.duration(args); case 'Timestamp': case 'google.protobuf.Timestamp': return fromJSON.timestamp(args); default: switch (field.parsedType.type) { case 'Enum': return fromJSON.enum(args); case 'Type': return fromJSON.type(args); } return needsImplementation(fieldName, field); } }); return fields; }; exports.fromJSONMethodFields = fromJSONMethodFields; const fromJSONMethod = (context, name, proto) => { const strictNullCheckForPrototypeMethods = context.pluginValue('prototypes.strictNullCheckForPrototypeMethods'); const fields = (0, exports.fromJSONMethodFields)(context, name, proto); let varName = 'object'; if (!fields.length) { varName = '_'; } //scaffold block statement let statements = [ t.variableDeclaration('const', [ t.variableDeclarator(t.identifier('obj'), t.callExpression(t.identifier('createBase' + name), [])) ]) ]; for (let i = 0; i < fields.length; i++) { statements.push(fields[i]); } statements.push(t.returnStatement(t.identifier('obj'))); return (0, utils_1.objectMethod)('method', t.identifier('fromJSON'), [ (0, utils_1.identifier)(varName, t.tsTypeAnnotation(t.tsAnyKeyword()), false) ], t.blockStatement(strictNullCheckForPrototypeMethods ? statements : [ t.returnStatement(t.objectExpression(fields)) ]), false, false, false, // returnType t.tsTypeAnnotation(t.tsTypeReference(t.identifier(name)))); }; exports.fromJSONMethod = fromJSONMethod;