From 773e6347b57e4a5236b1ef0bb8d361e4b233caf7 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Fri, 14 Apr 2017 18:10:40 +0200 Subject: [PATCH] Other: ext/descriptor docs; Other: Decorators coverage --- ext/descriptor/index.d.ts | 34 +++++++++++-- ext/descriptor/index.js | 82 ++++++++++++++++++------------- ext/descriptor/test.js | 6 +-- src/mapfield.js | 2 +- tests/comp_google_protobuf_any.js | 2 +- tests/comp_typescript.js | 37 +++++++------- tests/comp_typescript.ts | 19 +++++-- 7 files changed, 117 insertions(+), 65 deletions(-) diff --git a/ext/descriptor/index.d.ts b/ext/descriptor/index.d.ts index 5c2a2f66b..40a558e5a 100644 --- a/ext/descriptor/index.d.ts +++ b/ext/descriptor/index.d.ts @@ -14,11 +14,30 @@ type FileDescriptorProtoProperties = { enumType?: EnumDescriptorProtoProperties[]; service?: ServiceDescriptorProtoProperties[]; extension?: FieldDescriptorProtoProperties[]; - options?: any; + options?: FileOptionsProperties; sourceCodeInfo?: any; syntax?: string; }; +type FileOptionsProperties = { + javaPackage?: string; + javaOuterClassname?: string; + javaMultipleFiles?: boolean; + javaGenerateEqualsAndHash?: boolean; + javaStringCheckUtf8?: boolean; + optimizeFor?: FileOptions_OptimizeMode; + goPackage?: string; + ccGenericServices?: boolean; + javaGenericServices?: boolean; + pyGenericServices?: boolean; + deprecated?: boolean; + ccEnableArenas?: boolean; + objcClassPrefix?: string; + csharpNamespace?: string; +}; + +type FileOptions_OptimizeMode = number; + type DescriptorProtoProperties = { name?: string; field?: FieldDescriptorProtoProperties[]; @@ -84,6 +103,7 @@ type EnumValueDescriptorProtoProperties = { type EnumOptionsProperties = { allowAlias?: boolean; + deprecated?: boolean; }; type OneofDescriptorProtoProperties = { @@ -94,18 +114,26 @@ type OneofDescriptorProtoProperties = { type ServiceDescriptorProtoProperties = { name?: string; method?: MethodDescriptorProtoProperties[]; - options?: any; + options?: ServiceOptionsProperties; +}; + +type ServiceOptionsProperties = { + deprecated?: boolean; }; type MethodDescriptorProtoProperties = { name?: string; inputType?: string; outputType?: string; - options?: any; + options?: MethodOptionsProperties; clientStreaming?: boolean; serverStreaming?: boolean; }; +type MethodOptionsProperties = { + deprecated?: boolean; +}; + export const FileDescriptorSet: $protobuf.Type; export const FileDescriptorProto: $protobuf.Type; diff --git a/ext/descriptor/index.js b/ext/descriptor/index.js index 219fb5772..b26b299cb 100644 --- a/ext/descriptor/index.js +++ b/ext/descriptor/index.js @@ -19,7 +19,6 @@ var Namespace = $protobuf.Namespace, * @typedef FileDescriptorSetProperties * @type {Object} * @property {FileDescriptorProtoProperties[]} file Files - * @see Part of the {@link descriptor} extension (ext/descriptor) */ /** @@ -35,17 +34,44 @@ var Namespace = $protobuf.Namespace, * @property {EnumDescriptorProtoProperties[]} [enumType] Nested enums * @property {ServiceDescriptorProtoProperties[]} [service] Nested services * @property {FieldDescriptorProtoProperties[]} [extension] Nested extension fields - * @property {*} [options] Not supported + * @property {FileOptionsProperties} [options] Options * @property {*} [sourceCodeInfo] Not supported * @property {string} [syntax="proto2"] Syntax - * @see Part of the {@link descriptor} extension (ext/descriptor) + */ + +/** + * Properties of a FileOptions message. + * @typedef FileOptionsProperties + * @type {Object} + * @property {string} [javaPackage] + * @property {string} [javaOuterClassname] + * @property {boolean} [javaMultipleFiles] + * @property {boolean} [javaGenerateEqualsAndHash] + * @property {boolean} [javaStringCheckUtf8] + * @property {FileOptions_OptimizeMode} [optimizeFor=1] + * @property {string} [goPackage] + * @property {boolean} [ccGenericServices] + * @property {boolean} [javaGenericServices] + * @property {boolean} [pyGenericServices] + * @property {boolean} [deprecated] + * @property {boolean} [ccEnableArenas] + * @property {string} [objcClassPrefix] + * @property {string} [csharpNamespace] + */ + +/** + * Values of he FileOptions.OptimizeMode enum. + * @typedef FileOptions_OptimizeMode + * @type {number} + * @property {number} SPEED=1 + * @property {number} CODE_SIZE=2 + * @property {number} LITE_RUNTIME=3 */ /** * Creates a root from a descriptor set. * @param {FileDescriptorSetProperties|Reader|Uint8Array} descriptor Descriptor * @returns {Root} Root instance - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Root.fromDescriptor = function fromDescriptor(descriptor) { @@ -89,7 +115,6 @@ Root.fromDescriptor = function fromDescriptor(descriptor) { * Converts a root to a descriptor set. * @returns {Message} Descriptor * @param {string} [syntax="proto2"] Syntax - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Root.prototype.toDescriptor = function toDescriptor(syntax) { var set = exports.FileDescriptorSet.create(); @@ -144,7 +169,6 @@ function Root_toDescriptorRecursive(ns, files, syntax) { * @property {MessageOptionsProperties} [options] Not supported * @property {ReservedRangeProperties[]} [reservedRange] Reserved ranges * @property {string[]} [reservedName] Reserved names - * @see Part of the {@link descriptor} extension (ext/descriptor) */ /** @@ -152,7 +176,6 @@ function Root_toDescriptorRecursive(ns, files, syntax) { * @typedef MessageOptionsProperties * @type {Object} * @property {boolean} [mapEntry=false] Whether this message is a map entry - * @see Part of the {@link descriptor} extension (ext/descriptor) */ /** @@ -161,7 +184,6 @@ function Root_toDescriptorRecursive(ns, files, syntax) { * @type {Object} * @property {number} [start] Start field id * @property {number} [end] End field id - * @see Part of the {@link descriptor} extension (ext/descriptor) */ /** @@ -170,7 +192,6 @@ function Root_toDescriptorRecursive(ns, files, syntax) { * @type {Object} * @property {number} [start] Start field id * @property {number} [end] End field id - * @see Part of the {@link descriptor} extension (ext/descriptor) */ var unnamedMessageIndex = 0; @@ -180,7 +201,6 @@ var unnamedMessageIndex = 0; * @param {DescriptorProtoProperties|Reader|Uint8Array} descriptor Descriptor * @param {string} [syntax="proto2"] Syntax * @returns {Type} Type instance - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Type.fromDescriptor = function fromDescriptor(descriptor, syntax) { @@ -236,7 +256,6 @@ Type.fromDescriptor = function fromDescriptor(descriptor, syntax) { * Converts a type to a descriptor. * @returns {Message} Descriptor * @param {string} [syntax="proto2"] Syntax - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Type.prototype.toDescriptor = function toDescriptor(syntax) { var descriptor = exports.DescriptorProto.create({ name: this.name }), @@ -303,7 +322,6 @@ Type.prototype.toDescriptor = function toDescriptor(syntax) { * @property {number} [oneofIndex] Oneof index if part of a oneof * @property {*} [jsonName] Not supported * @property {FieldOptionsProperties} [options] Field options - * @see Part of the {@link descriptor} extension (ext/descriptor) */ /** @@ -313,7 +331,6 @@ Type.prototype.toDescriptor = function toDescriptor(syntax) { * @property {number} LABEL_OPTIONAL=1 * @property {number} LABEL_REQUIRED=2 * @property {number} LABEL_REPEATED=3 - * @see Part of the {@link descriptor} extension (ext/descriptor) */ /** @@ -338,7 +355,6 @@ Type.prototype.toDescriptor = function toDescriptor(syntax) { * @property {number} TYPE_SFIXED64=16 * @property {number} TYPE_SINT32=17 * @property {number} TYPE_SINT64=18 - * @see Part of the {@link descriptor} extension (ext/descriptor) */ /** @@ -347,7 +363,6 @@ Type.prototype.toDescriptor = function toDescriptor(syntax) { * @type {Object} * @property {boolean} [packed] Whether packed or not (defaults to `false` for proto2 and `true` for proto3) * @property {FieldOptions_JSType} [jstype] JavaScript value type (not used by protobuf.js) - * @see Part of the {@link descriptor} extension (ext/descriptor) */ /** @@ -364,7 +379,6 @@ Type.prototype.toDescriptor = function toDescriptor(syntax) { * @param {FieldDescriptorProtoProperties|Reader|Uint8Array} descriptor Descriptor * @param {string} [syntax="proto2"] Syntax * @returns {Field} Field instance - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Field.fromDescriptor = function fromDescriptor(descriptor, syntax) { @@ -414,7 +428,7 @@ Field.fromDescriptor = function fromDescriptor(descriptor, syntax) { default: var match = $protobuf.parse.numberRe.exec(defaultValue); if (match) - defaultValue = parseInt(defaultValue); + defaultValue = parseInt(defaultValue); // eslint-disable-line radix break; } field.setOption("default", defaultValue); @@ -435,7 +449,6 @@ Field.fromDescriptor = function fromDescriptor(descriptor, syntax) { * Converts a field to a descriptor. * @returns {Message} Descriptor * @param {string} [syntax="proto2"] Syntax - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Field.prototype.toDescriptor = function toDescriptor(syntax) { var descriptor = exports.FieldDescriptorProto.create({ name: this.name, number: this.id }); @@ -498,7 +511,6 @@ Field.prototype.toDescriptor = function toDescriptor(syntax) { * @property {string} [name] Enum name * @property {EnumValueDescriptorProtoProperties[]} [value] Enum values * @property {EnumOptionsProperties} [options] Enum options - * @see Part of the {@link descriptor} extension (ext/descriptor) */ /** @@ -508,7 +520,6 @@ Field.prototype.toDescriptor = function toDescriptor(syntax) { * @property {string} [name] Name * @property {number} [number] Value * @property {*} [options] Not supported - * @see Part of the {@link descriptor} extension (ext/descriptor) */ /** @@ -516,7 +527,7 @@ Field.prototype.toDescriptor = function toDescriptor(syntax) { * @typedef EnumOptionsProperties * @type {Object} * @property {boolean} [allowAlias] Whether aliases are allowed - * @see Part of the {@link descriptor} extension (ext/descriptor) + * @property {boolean} [deprecated] */ var unnamedEnumIndex = 0; @@ -525,7 +536,6 @@ var unnamedEnumIndex = 0; * Creates an enum from a descriptor. * @param {EnumDescriptorProtoProperties|Reader|Uint8Array} descriptor Descriptor * @returns {Enum} Enum instance - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Enum.fromDescriptor = function fromDescriptor(descriptor) { @@ -552,7 +562,6 @@ Enum.fromDescriptor = function fromDescriptor(descriptor) { /** * Converts an enum to a descriptor. * @returns {Message} Descriptor - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Enum.prototype.toDescriptor = function toDescriptor() { @@ -576,7 +585,6 @@ Enum.prototype.toDescriptor = function toDescriptor() { * @type {Object} * @property {string} [name] Oneof name * @property {*} [options] Not supported - * @see Part of the {@link descriptor} extension (ext/descriptor) */ var unnamedOneofIndex = 0; @@ -585,7 +593,6 @@ var unnamedOneofIndex = 0; * Creates a oneof from a descriptor. * @param {OneofDescriptorProtoProperties|Reader|Uint8Array} descriptor Descriptor * @returns {OneOf} OneOf instance - * @see Part of the {@link descriptor} extension (ext/descriptor) */ OneOf.fromDescriptor = function fromDescriptor(descriptor) { @@ -603,7 +610,6 @@ OneOf.fromDescriptor = function fromDescriptor(descriptor) { /** * Converts a oneof to a descriptor. * @returns {Message} Descriptor - * @see Part of the {@link descriptor} extension (ext/descriptor) */ OneOf.prototype.toDescriptor = function toDescriptor() { return exports.OneofDescriptorProto.create({ @@ -620,8 +626,14 @@ OneOf.prototype.toDescriptor = function toDescriptor() { * @type {Object} * @property {string} [name] Service name * @property {MethodDescriptorProtoProperties[]} [method] Methods - * @property {*} [options] Not supported - * @see Part of the {@link descriptor} extension (ext/descriptor) + * @property {ServiceOptionsProperties} [options] Options + */ + +/** + * Properties of a ServiceOptions message. + * @typedef ServiceOptionsProperties + * @type {Object} + * @property {boolean} [deprecated] */ var unnamedServiceIndex = 0; @@ -630,7 +642,6 @@ var unnamedServiceIndex = 0; * Creates a service from a descriptor. * @param {ServiceDescriptorProtoProperties|Reader|Uint8Array} descriptor Descriptor * @returns {Service} Service instance - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Service.fromDescriptor = function fromDescriptor(descriptor) { @@ -649,7 +660,6 @@ Service.fromDescriptor = function fromDescriptor(descriptor) { /** * Converts a service to a descriptor. * @returns {Message} Descriptor - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Service.prototype.toDescriptor = function toDescriptor() { @@ -674,10 +684,16 @@ Service.prototype.toDescriptor = function toDescriptor() { * @property {string} [name] Method name * @property {string} [inputType] Request type name * @property {string} [outputType] Response type name - * @property {*} [options] Not supported + * @property {MethodOptionsProperties} [options] Not supported * @property {boolean} [clientStreaming=false] Whether requests are streamed * @property {boolean} [serverStreaming=false] Whether responses are streamed - * @see Part of the {@link descriptor} extension (ext/descriptor) + */ + +/** + * Properties of a MethodOptions message. + * @typedef MethodOptionsProperties + * @type {Object} + * @property {boolean} [deprecated] */ var unnamedMethodIndex = 0; @@ -686,7 +702,6 @@ var unnamedMethodIndex = 0; * Creates a method from a descriptor. * @param {MethodDescriptorProtoProperties|Reader|Uint8Array} descriptor Descriptor * @returns {Method} Reflected method instance - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Method.fromDescriptor = function fromDescriptor(descriptor) { @@ -709,7 +724,6 @@ Method.fromDescriptor = function fromDescriptor(descriptor) { /** * Converts a method to a descriptor. * @returns {Message} Descriptor - * @see Part of the {@link descriptor} extension (ext/descriptor) */ Method.prototype.toDescriptor = function toDescriptor() { return exports.MethodDescriptorProto.create({ diff --git a/ext/descriptor/test.js b/ext/descriptor/test.js index ed38e9ecc..ceb80f827 100644 --- a/ext/descriptor/test.js +++ b/ext/descriptor/test.js @@ -3,7 +3,7 @@ var protobuf = require("../../"), descriptor = require("."); -var proto = require("../../google/protobuf/descriptor.json")/*{ +/* var proto = { nested: { Message: { fields: { @@ -25,7 +25,7 @@ var proto = require("../../google/protobuf/descriptor.json")/*{ } } } -}*/; +}; */ // var root = protobuf.Root.fromJSON(proto).resolveAll(); var root = protobuf.loadSync("tests/data/google/protobuf/descriptor.proto").resolveAll(); @@ -49,6 +49,6 @@ if (diff) { console.log("rhs:", typeof diff.rhs, diff.rhs); console.log(); }); - process.exit(1); + process.exitCode = 1; } else console.log("no differences"); diff --git a/src/mapfield.js b/src/mapfield.js index e0f76ce1e..95a577151 100644 --- a/src/mapfield.js +++ b/src/mapfield.js @@ -119,7 +119,7 @@ MapField.d = function decorateMapField(fieldId, fieldKeyType, fieldValueType) { fieldValueType = util.decorateType(fieldValueType).name; // enum reference value: create a reflected copy of the enum and keep reuseing it - else if (fieldValueType && typeof fieldType === "object") + else if (fieldValueType && typeof fieldValueType === "object") fieldValueType = util.decorateEnum(fieldValueType).name; return function mapFieldDecorator(prototype, fieldName) { diff --git a/tests/comp_google_protobuf_any.js b/tests/comp_google_protobuf_any.js index 12b86ec89..6cafd6be7 100644 --- a/tests/comp_google_protobuf_any.js +++ b/tests/comp_google_protobuf_any.js @@ -23,7 +23,7 @@ var root = protobuf.Root.fromJSON({ } }).addJSON(protobuf.common["google/protobuf/any.proto"].nested).resolveAll(); -var Any = root.lookupType(".google.protobuf.Any"), +var Any = root.lookupType("protobuf.Any"), Foo = root.lookupType(".Foo"), Bar = root.lookupType(".Bar"); diff --git a/tests/comp_typescript.js b/tests/comp_typescript.js index a764eead7..a6c34a0e8 100644 --- a/tests/comp_typescript.js +++ b/tests/comp_typescript.js @@ -20,9 +20,6 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; -var __metadata = (this && this.__metadata) || function (k, v) { - if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); -}; exports.__esModule = true; var __1 = require(".."); // Reflection @@ -61,6 +58,11 @@ var helloBuffer = Hello.encode(helloMessage.foo()).finish(); var helloDecoded = Hello.decode(helloBuffer); // Decorators require("reflect-metadata"); +var AwesomeEnum; +(function (AwesomeEnum) { + AwesomeEnum[AwesomeEnum["ONE"] = 1] = "ONE"; + AwesomeEnum[AwesomeEnum["TWO"] = 2] = "TWO"; +})(AwesomeEnum = exports.AwesomeEnum || (exports.AwesomeEnum = {})); var AwesomeSubMessage = (function (_super) { __extends(AwesomeSubMessage, _super); function AwesomeSubMessage() { @@ -69,15 +71,18 @@ var AwesomeSubMessage = (function (_super) { return AwesomeSubMessage; }(__1.Message)); __decorate([ - __1.Field.d(1, "string"), - __metadata("design:type", String) + __1.Field.d(1, "string") ], AwesomeSubMessage.prototype, "awesomeString"); +__decorate([ + __1.MapField.d(2, "string", "string") +], AwesomeSubMessage.prototype, "awesomeMapString"); +__decorate([ + __1.MapField.d(3, "string", AwesomeEnum) +], AwesomeSubMessage.prototype, "awesomeMapEnum"); +__decorate([ + __1.MapField.d(4, "string", AwesomeSubMessage) +], AwesomeSubMessage.prototype, "awesomeMapMessage"); exports.AwesomeSubMessage = AwesomeSubMessage; -var AwesomeEnum; -(function (AwesomeEnum) { - AwesomeEnum[AwesomeEnum["ONE"] = 1] = "ONE"; - AwesomeEnum[AwesomeEnum["TWO"] = 2] = "TWO"; -})(AwesomeEnum = exports.AwesomeEnum || (exports.AwesomeEnum = {})); var AwesomeMessage = (function (_super) { __extends(AwesomeMessage, _super); function AwesomeMessage() { @@ -86,20 +91,16 @@ var AwesomeMessage = (function (_super) { return AwesomeMessage; }(__1.Message)); __decorate([ - __1.Field.d(1, "string", "optional", "awesome default string"), - __metadata("design:type", String) + __1.Field.d(1, "string", "optional", "awesome default string") ], AwesomeMessage.prototype, "awesomeField"); __decorate([ - __1.Field.d(2, AwesomeSubMessage), - __metadata("design:type", AwesomeSubMessage) + __1.Field.d(2, AwesomeSubMessage) ], AwesomeMessage.prototype, "awesomeSubMessage"); __decorate([ - __1.Field.d(3, AwesomeEnum, "optional", AwesomeEnum.ONE), - __metadata("design:type", Number) + __1.Field.d(3, AwesomeEnum, "optional", AwesomeEnum.ONE) ], AwesomeMessage.prototype, "awesomeEnum"); __decorate([ - __1.OneOf.d("awesomeSubMessage", "awesomeEnum"), - __metadata("design:type", String) + __1.OneOf.d("awesomeSubMessage", "awesomeEnum") ], AwesomeMessage.prototype, "which"); AwesomeMessage = __decorate([ __1.Type.d("SuperAwesomeMessage") diff --git a/tests/comp_typescript.ts b/tests/comp_typescript.ts index c88453888..42e6ba847 100644 --- a/tests/comp_typescript.ts +++ b/tests/comp_typescript.ts @@ -4,7 +4,7 @@ /// */ -import { Root, Message, Type, Field, OneOf } from ".."; +import { Root, Message, Type, Field, MapField, OneOf } from ".."; // Reflection const root = Root.fromJSON({ @@ -47,16 +47,25 @@ let helloDecoded = Hello.decode(helloBuffer); import "reflect-metadata"; +export enum AwesomeEnum { + ONE = 1, + TWO = 2 +} + export class AwesomeSubMessage extends Message { @Field.d(1, "string") public awesomeString: string; -} + @MapField.d(2, "string", "string") + public awesomeMapString : { [key: string]: string }; + + @MapField.d(3, "string", AwesomeEnum) + public awesomeMapEnum : { [key: string]: string }; + + @MapField.d(4, "string", AwesomeSubMessage) + public awesomeMapMessage : { [key: string]: Message }; -export enum AwesomeEnum { - ONE = 1, - TWO = 2 } @Type.d("SuperAwesomeMessage")