@ngx-grpc/protoc-gen-ng
Advanced tools
Comparing version 0.2.3 to 0.3.0
@@ -0,1 +1,18 @@ | ||
# [0.3.0](https://github.com/ngx-grpc/protoc-gen-ng/compare/v0.2.3...v0.3.0) (2019-12-05) | ||
### Bug Fixes | ||
* treat proto2 group as message type ([ffe8410](https://github.com/ngx-grpc/protoc-gen-ng/commit/ffe8410dd350d3e1e68cbca9293fdedd8e66e8ff)) | ||
### Features | ||
* add possibility to cast from and to snapshot ([c1a75dd](https://github.com/ngx-grpc/protoc-gen-ng/commit/c1a75dd07f807b53e4b8849ca9dc216eaf72d843)) | ||
### BREAKING CHANGES | ||
* the object that is provided as message constructor argument is always cloned; the message is always initialized with proto default values | ||
## [0.2.3](https://github.com/ngx-grpc/protoc-gen-ng/compare/v0.2.2...v0.2.3) (2019-11-25) | ||
@@ -2,0 +19,0 @@ |
@@ -24,2 +24,3 @@ "use strict"; | ||
var enum_1 = require("./enum"); | ||
var js_doc_1 = require("./js-doc"); | ||
var types_1 = require("./types"); | ||
@@ -34,3 +35,3 @@ var FieldTypesConfig = (_a = {}, | ||
_a[types_1.MessageFieldType.float] = { type: 'number', read: 'Float', write: 'Float', defaultExpression: '0' }, | ||
// [MessageFieldType.group]: null, // does not exist in v3 | ||
// [MessageFieldType.group]: null, // does not exist in v3, automatically converted to MessageFieldType.message | ||
_a[types_1.MessageFieldType.int32] = { type: 'number', read: 'Int32', write: 'Int32', defaultExpression: '0' }, | ||
@@ -58,2 +59,9 @@ _a[types_1.MessageFieldType.int64] = { type: 'number', read: 'Int64', write: 'Int64', defaultExpression: '0' }, | ||
} | ||
Object.defineProperty(MessageField.prototype, "isMessage", { | ||
get: function () { | ||
return this.type === types_1.MessageFieldType.message || this.type === types_1.MessageFieldType.group; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return MessageField; | ||
@@ -77,3 +85,3 @@ }()); | ||
Message.prototype.isFieldMap = function (field) { | ||
if (field.type === types_1.MessageFieldType.message) { | ||
if (field.isMessage) { | ||
var msg = this.proto.resolveTypeMetadata(field.typeName).message; | ||
@@ -95,4 +103,4 @@ if (msg && msg.options.mapEntry) { | ||
var processName = function (name) { | ||
var escaped = ['default', 'var', 'let', 'const', 'function', 'class'].includes(name) ? 'pb_' + name : name; | ||
return utils_1.camelize(escaped); | ||
var camelized = utils_1.camelize(name); | ||
return ['default', 'var', 'let', 'const', 'function', 'class', 'toObject', 'toJSON'].includes(camelized) ? 'pb_' + camelized : camelized; | ||
}; | ||
@@ -105,3 +113,3 @@ var getDataType = function (field) { | ||
var suffix = field.label === types_1.MessageFieldCardinality.repeated ? '[]' : ''; | ||
if (field.type === types_1.MessageFieldType.enum || field.type === types_1.MessageFieldType.message) { | ||
if (field.type === types_1.MessageFieldType.enum || field.isMessage) { | ||
return _this.proto.getRelativeTypeName(field.typeName) + suffix; | ||
@@ -115,3 +123,3 @@ } | ||
var repeated = field.label === types_1.MessageFieldCardinality.repeated ? 'Repeated' : ''; | ||
if (field.type === types_1.MessageFieldType.message) { | ||
if (field.isMessage) { | ||
var subType = _this.proto.getRelativeTypeName(field.typeName); | ||
@@ -133,3 +141,3 @@ if (_this.isFieldMap(field)) { | ||
var config = FieldTypesConfig[field.type]; | ||
if (field.type === types_1.MessageFieldType.message) { | ||
if (field.isMessage) { | ||
var subType = _this.proto.getRelativeTypeName(field.typeName); | ||
@@ -175,6 +183,50 @@ if (_this.isFieldMap(field)) { | ||
}); | ||
var attributeInitializers = this.fieldList.map(function (f) { return "this." + processName(f.name) + " = value." + processName(f.name); }); | ||
var attributeInitializers = this.fieldList.map(function (field) { | ||
var fieldName = processName(field.name); | ||
if (field.isMessage) { | ||
var subType = _this.proto.getRelativeTypeName(field.typeName); | ||
if (_this.isFieldMap(field)) { | ||
return "this." + fieldName + " = {...(value." + fieldName + " || {})};"; // TODO properly clone submessages | ||
} | ||
if (field.label === types_1.MessageFieldCardinality.repeated) { | ||
return "this." + fieldName + " = (value." + fieldName + " || []).map(m => new " + subType + "(m));"; | ||
} | ||
return "this." + fieldName + " = value." + fieldName + " ? new " + subType + "(value." + fieldName + ") : undefined;"; | ||
} | ||
if (field.type === types_1.MessageFieldType.bytes) { | ||
if (field.label === types_1.MessageFieldCardinality.repeated) { | ||
return "this." + fieldName + " = (value." + fieldName + " || []).map(b => b ? b.subarray(0) : new Uint8Array());"; | ||
} | ||
return "this." + fieldName + " = value." + fieldName + " ? value." + fieldName + ".subarray(0) : undefined;"; | ||
} | ||
if (field.label === types_1.MessageFieldCardinality.repeated) { | ||
return "this." + fieldName + " = (value." + fieldName + " || []).slice();"; | ||
} | ||
return "this." + fieldName + " = value." + fieldName; | ||
}); | ||
var toObjectMappings = this.fieldList.map(function (field) { | ||
var fieldName = processName(field.name); | ||
if (field.isMessage) { | ||
if (_this.isFieldMap(field)) { | ||
return fieldName + ": {...(this." + fieldName + " || {})},"; // TODO properly clone submessages | ||
} | ||
if (field.label === types_1.MessageFieldCardinality.repeated) { | ||
return fieldName + ": (this." + fieldName + " || []).map(m => m.toObject()),"; | ||
} | ||
return fieldName + ": this." + fieldName + " ? this." + fieldName + ".toObject() : undefined,"; | ||
} | ||
if (field.type === types_1.MessageFieldType.bytes) { | ||
if (field.label === types_1.MessageFieldCardinality.repeated) { | ||
return fieldName + ": (this." + fieldName + " || []).map(b => b ? b.subarray(0) : new Uint8Array()),"; | ||
} | ||
return fieldName + ": this." + fieldName + " ? this." + fieldName + ".subarray(0) : new Uint8Array(),"; | ||
} | ||
if (field.label === types_1.MessageFieldCardinality.repeated) { | ||
return fieldName + ": (this." + fieldName + " || []).slice(),"; | ||
} | ||
return fieldName + ": this." + fieldName + ","; | ||
}); | ||
var serializeAttributes = this.fieldList.map(function (f) { return getWriteCall(f); }); | ||
var deserializeAttributes = this.fieldList.map(function (f) { return getReadCall(f); }); | ||
var afterReadInitializers = this.fieldList | ||
var defaultValueCheckers = this.fieldList | ||
.filter(function (f) { return typeof f.oneofIndex !== 'number'; }) // we do not want to add default initializers to oneOf properties cause it against its logic | ||
@@ -188,3 +240,6 @@ .map(function (f) { return "instance." + processName(f.name) + " = instance." + processName(f.name) + " || " + (_this.isFieldMap(f) ? | ||
: 'undefined') + "\n "; }); | ||
return "export class " + this.name + " {\n\n static fromBinaryReader(instance: " + this.name + ", reader: BinaryReader) {\n while (reader.nextField()) {\n if (reader.isEndGroup()) {\n break;\n }\n\n switch (reader.getFieldNumber()) {\n " + deserializeAttributes.join('\n') + "\n default: reader.skipField();\n }\n }\n\n " + afterReadInitializers.join(';') + "\n }\n\n static fromBinary(bytes: ByteSource) {\n const instance = new " + this.name + "();\n\n " + this.name + ".fromBinaryReader(instance, new BinaryReader(bytes));\n\n return instance;\n }\n\n static toBinaryWriter(instance: " + this.name + ", writer: BinaryWriter) {\n " + serializeAttributes.join('\n ') + "\n }\n\n static toBinary(instance: " + this.name + ") {\n const writer = new BinaryWriter();\n\n " + this.name + ".toBinaryWriter(instance, writer);\n\n return writer.getResultBuffer();\n }\n\n " + attributes.join('\n') + "\n\n " + oneOfCaseAttrubutes.join('\n') + "\n\n constructor(value: Partial<" + this.name + "> = {}) {\n " + attributeInitializers.join(';') + "\n }\n\n " + attributeGetters.join('\n') + "\n " + attributeSetters.join('\n') + "\n\n " + oneOfCaseAttributeGetters.join('\n') + "\n\n}\n\nexport module " + this.name + " {\n " + __spreadArrays(this.oneofDeclList.map(function (od, i) { | ||
var constructorDoc = new js_doc_1.JSDoc(); | ||
constructorDoc.setDescription('Creates an object and applies default Protobuf values'); | ||
constructorDoc.addParam({ name: 'value', type: "" + this.name, description: "Initial values object or instance of " + this.name + " to clone from (deep cloning)" }); | ||
return "export class " + this.name + " {\n\n static refineValues(instance: " + this.name + ") {\n " + defaultValueCheckers.join(';') + "\n }\n\n static fromBinaryReader(instance: " + this.name + ", reader: BinaryReader) {\n while (reader.nextField()) {\n if (reader.isEndGroup()) {\n break;\n }\n\n switch (reader.getFieldNumber()) {\n " + deserializeAttributes.join('\n') + "\n default: reader.skipField();\n }\n }\n\n " + this.name + ".refineValues(instance);\n }\n\n static fromBinary(bytes: ByteSource) {\n const instance = new " + this.name + "();\n\n " + this.name + ".fromBinaryReader(instance, new BinaryReader(bytes));\n\n return instance;\n }\n\n static toBinaryWriter(instance: " + this.name + ", writer: BinaryWriter) {\n " + serializeAttributes.join('\n ') + "\n }\n\n static toBinary(instance: " + this.name + ") {\n const writer = new BinaryWriter();\n\n " + this.name + ".toBinaryWriter(instance, writer);\n\n return writer.getResultBuffer();\n }\n\n " + attributes.join('\n') + "\n\n " + oneOfCaseAttrubutes.join('\n') + "\n\n " + constructorDoc + "\n constructor(value?: Partial<" + this.name + ">) {\n value = value || {};\n " + attributeInitializers.join(';') + "\n " + this.name + ".refineValues(this);\n }\n\n " + attributeGetters.join('\n') + "\n " + attributeSetters.join('\n') + "\n\n " + oneOfCaseAttributeGetters.join('\n') + "\n\n toObject() {\n return {\n " + toObjectMappings.join('\n') + "\n };\n }\n\n toJSON() {\n return this.toObject();\n }\n\n}\n\nexport module " + this.name + " {\n " + __spreadArrays(this.oneofDeclList.map(function (od, i) { | ||
return new enum_1.Enum({ | ||
@@ -191,0 +246,0 @@ name: _this.createCaseEnumName(od.name), |
{ | ||
"name": "@ngx-grpc/protoc-gen-ng", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"author": "smnbbrv", | ||
@@ -28,3 +28,3 @@ "license": "MIT", | ||
"test:generate": "rimraf ./test/out/* && protoc --plugin=protoc-gen-ng=$(pwd)/dist/main.js --ng_out=./test/out -I test/proto $(find test/proto -iname \"*.proto\")", | ||
"test:generate-grpc-web": "rimraf ./test/out-grpc-web/* && protoc -I=test/proto --js_out=import_style=commonjs:test/out-grpc-web --grpc-web_out=import_style=typescript,mode=grpcwebtext:test/out-grpc-web $(find test/proto -iname \"*.proto\")", | ||
"test:generate-grpc-web": "rimraf ./test/out-grpc-web/* && protoc -I=test/proto --js_out=import_style=commonjs:test/out-grpc-web --grpc-web_out=import_style=typescript,mode=grpcweb:test/out-grpc-web $(find test/proto -iname \"*.proto\")", | ||
"release": "semantic-release" | ||
@@ -31,0 +31,0 @@ }, |
42792
644