Git Product home page Git Product logo

Comments (2)

johngrogg avatar johngrogg commented on July 3, 2024

After some initial experimentation, it appears the protobufjs, grpc, and likely typescript libraries would need to be upgraded in order to get the necessary information out of the protobuf build process.

With protobufjs v6.8.6 and grpc v1.12.3 the above `.proto` gets built into the following js:
/*eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins*/
"use strict";

var $protobuf = require("protobufjs/minimal");

// Common aliases
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Exported root namespace
var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});

$root.test = (function() {

    /**
     * Namespace test.
     * @exports test
     * @namespace
     */
    var test = {};

    test.Message = (function() {

        /**
         * Properties of a Message.
         * @memberof test
         * @interface IMessage
         * @property {Uint8Array|null} [attribute1] Message attribute1
         * @property {string|null} [attribute2] Message attribute2
         */

        /**
         * Constructs a new Message.
         * @memberof test
         * @classdesc Represents a Message.
         * @implements IMessage
         * @constructor
         * @param {test.IMessage=} [properties] Properties to set
         */
        function Message(properties) {
            if (properties)
                for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
                    if (properties[keys[i]] != null)
                        this[keys[i]] = properties[keys[i]];
        }

        /**
         * Message attribute1.
         * @member {Uint8Array} attribute1
         * @memberof test.Message
         * @instance
         */
        Message.prototype.attribute1 = $util.newBuffer([]);

        /**
         * Message attribute2.
         * @member {string} attribute2
         * @memberof test.Message
         * @instance
         */
        Message.prototype.attribute2 = "";

        // OneOf field names bound to virtual getters and setters
        var $oneOfFields;

        /**
         * Message myRef.
         * @member {"attribute1"|"attribute2"|undefined} myRef
         * @memberof test.Message
         * @instance
         */
        Object.defineProperty(Message.prototype, "myRef", {
            get: $util.oneOfGetter($oneOfFields = ["attribute1", "attribute2"]),
            set: $util.oneOfSetter($oneOfFields)
        });

        /**
         * Creates a new Message instance using the specified properties.
         * @function create
         * @memberof test.Message
         * @static
         * @param {test.IMessage=} [properties] Properties to set
         * @returns {test.Message} Message instance
         */
        Message.create = function create(properties) {
            return new Message(properties);
        };

        /**
         * Encodes the specified Message message. Does not implicitly {@link test.Message.verify|verify} messages.
         * @function encode
         * @memberof test.Message
         * @static
         * @param {test.IMessage} message Message message or plain object to encode
         * @param {$protobuf.Writer} [writer] Writer to encode to
         * @returns {$protobuf.Writer} Writer
         */
        Message.encode = function encode(message, writer) {
            if (!writer)
                writer = $Writer.create();
            if (message.attribute1 != null && message.hasOwnProperty("attribute1"))
                writer.uint32(/* id 1, wireType 2 =*/10).bytes(message.attribute1);
            if (message.attribute2 != null && message.hasOwnProperty("attribute2"))
                writer.uint32(/* id 2, wireType 2 =*/18).string(message.attribute2);
            return writer;
        };

        /**
         * Encodes the specified Message message, length delimited. Does not implicitly {@link test.Message.verify|verify} messages.
         * @function encodeDelimited
         * @memberof test.Message
         * @static
         * @param {test.IMessage} message Message message or plain object to encode
         * @param {$protobuf.Writer} [writer] Writer to encode to
         * @returns {$protobuf.Writer} Writer
         */
        Message.encodeDelimited = function encodeDelimited(message, writer) {
            return this.encode(message, writer).ldelim();
        };

        /**
         * Decodes a Message message from the specified reader or buffer.
         * @function decode
         * @memberof test.Message
         * @static
         * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
         * @param {number} [length] Message length if known beforehand
         * @returns {test.Message} Message
         * @throws {Error} If the payload is not a reader or valid buffer
         * @throws {$protobuf.util.ProtocolError} If required fields are missing
         */
        Message.decode = function decode(reader, length) {
            if (!(reader instanceof $Reader))
                reader = $Reader.create(reader);
            var end = length === undefined ? reader.len : reader.pos + length, message = new $root.test.Message();
            while (reader.pos < end) {
                var tag = reader.uint32();
                switch (tag >>> 3) {
                case 1:
                    message.attribute1 = reader.bytes();
                    break;
                case 2:
                    message.attribute2 = reader.string();
                    break;
                default:
                    reader.skipType(tag & 7);
                    break;
                }
            }
            return message;
        };

        /**
         * Decodes a Message message from the specified reader or buffer, length delimited.
         * @function decodeDelimited
         * @memberof test.Message
         * @static
         * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
         * @returns {test.Message} Message
         * @throws {Error} If the payload is not a reader or valid buffer
         * @throws {$protobuf.util.ProtocolError} If required fields are missing
         */
        Message.decodeDelimited = function decodeDelimited(reader) {
            if (!(reader instanceof $Reader))
                reader = new $Reader(reader);
            return this.decode(reader, reader.uint32());
        };

        /**
         * Verifies a Message message.
         * @function verify
         * @memberof test.Message
         * @static
         * @param {Object.<string,*>} message Plain object to verify
         * @returns {string|null} `null` if valid, otherwise the reason why it is not
         */
        Message.verify = function verify(message) {
            if (typeof message !== "object" || message === null)
                return "object expected";
            var properties = {};
            if (message.attribute1 != null && message.hasOwnProperty("attribute1")) {
                properties.myRef = 1;
                if (!(message.attribute1 && typeof message.attribute1.length === "number" || $util.isString(message.attribute1)))
                    return "attribute1: buffer expected";
            }
            if (message.attribute2 != null && message.hasOwnProperty("attribute2")) {
                if (properties.myRef === 1)
                    return "myRef: multiple values";
                properties.myRef = 1;
                if (!$util.isString(message.attribute2))
                    return "attribute2: string expected";
            }
            return null;
        };

        /**
         * Creates a Message message from a plain object. Also converts values to their respective internal types.
         * @function fromObject
         * @memberof test.Message
         * @static
         * @param {Object.<string,*>} object Plain object
         * @returns {test.Message} Message
         */
        Message.fromObject = function fromObject(object) {
            if (object instanceof $root.test.Message)
                return object;
            var message = new $root.test.Message();
            if (object.attribute1 != null)
                if (typeof object.attribute1 === "string")
                    $util.base64.decode(object.attribute1, message.attribute1 = $util.newBuffer($util.base64.length(object.attribute1)), 0);
                else if (object.attribute1.length)
                    message.attribute1 = object.attribute1;
            if (object.attribute2 != null)
                message.attribute2 = String(object.attribute2);
            return message;
        };

        /**
         * Creates a plain object from a Message message. Also converts values to other types if specified.
         * @function toObject
         * @memberof test.Message
         * @static
         * @param {test.Message} message Message
         * @param {$protobuf.IConversionOptions} [options] Conversion options
         * @returns {Object.<string,*>} Plain object
         */
        Message.toObject = function toObject(message, options) {
            if (!options)
                options = {};
            var object = {};
            if (message.attribute1 != null && message.hasOwnProperty("attribute1")) {
                object.attribute1 = options.bytes === String ? $util.base64.encode(message.attribute1, 0, message.attribute1.length) : options.bytes === Array ? Array.prototype.slice.call(message.attribute1) : message.attribute1;
                if (options.oneofs)
                    object.myRef = "attribute1";
            }
            if (message.attribute2 != null && message.hasOwnProperty("attribute2")) {
                object.attribute2 = message.attribute2;
                if (options.oneofs)
                    object.myRef = "attribute2";
            }
            return object;
        };

        /**
         * Converts this Message to JSON.
         * @function toJSON
         * @memberof test.Message
         * @instance
         * @returns {Object.<string,*>} JSON object
         */
        Message.prototype.toJSON = function toJSON() {
            return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
        };

        return Message;
    })();

    return test;
})();

module.exports = $root;
And then the `.d.ts` file generated by `pbts` is:
import * as $protobuf from "protobufjs";

/** Namespace test. */
export namespace test {

    /** Properties of a Message. */
    interface IMessage {

        /** Message attribute1 */
        attribute1?: (Uint8Array|null);

        /** Message attribute2 */
        attribute2?: (string|null);
    }

    /** Represents a Message. */
    class Message implements IMessage {

        /**
         * Constructs a new Message.
         * @param [properties] Properties to set
         */
        constructor(properties?: test.IMessage);

        /** Message attribute1. */
        public attribute1: Uint8Array;

        /** Message attribute2. */
        public attribute2: string;

        /** Message myRef. */
        public myRef?: ("attribute1"|"attribute2");

        /**
         * Creates a new Message instance using the specified properties.
         * @param [properties] Properties to set
         * @returns Message instance
         */
        public static create(properties?: test.IMessage): test.Message;

        /**
         * Encodes the specified Message message. Does not implicitly {@link test.Message.verify|verify} messages.
         * @param message Message message or plain object to encode
         * @param [writer] Writer to encode to
         * @returns Writer
         */
        public static encode(message: test.IMessage, writer?: $protobuf.Writer): $protobuf.Writer;

        /**
         * Encodes the specified Message message, length delimited. Does not implicitly {@link test.Message.verify|verify} messages.
         * @param message Message message or plain object to encode
         * @param [writer] Writer to encode to
         * @returns Writer
         */
        public static encodeDelimited(message: test.IMessage, writer?: $protobuf.Writer): $protobuf.Writer;

        /**
         * Decodes a Message message from the specified reader or buffer.
         * @param reader Reader or buffer to decode from
         * @param [length] Message length if known beforehand
         * @returns Message
         * @throws {Error} If the payload is not a reader or valid buffer
         * @throws {$protobuf.util.ProtocolError} If required fields are missing
         */
        public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): test.Message;

        /**
         * Decodes a Message message from the specified reader or buffer, length delimited.
         * @param reader Reader or buffer to decode from
         * @returns Message
         * @throws {Error} If the payload is not a reader or valid buffer
         * @throws {$protobuf.util.ProtocolError} If required fields are missing
         */
        public static decodeDelimited(reader: ($protobuf.Reader|Uint8Array)): test.Message;

        /**
         * Verifies a Message message.
         * @param message Plain object to verify
         * @returns `null` if valid, otherwise the reason why it is not
         */
        public static verify(message: { [k: string]: any }): (string|null);

        /**
         * Creates a Message message from a plain object. Also converts values to their respective internal types.
         * @param object Plain object
         * @returns Message
         */
        public static fromObject(object: { [k: string]: any }): test.Message;

        /**
         * Creates a plain object from a Message message. Also converts values to other types if specified.
         * @param message Message
         * @param [options] Conversion options
         * @returns Plain object
         */
        public static toObject(message: test.Message, options?: $protobuf.IConversionOptions): { [k: string]: any };

        /**
         * Converts this Message to JSON.
         * @returns JSON object
         */
        public toJSON(): { [k: string]: any };
    }
}

The one issue at that point is that the Class now implements the IMessage interface, and the resulting `grpc-namespaces.ts` file then converts that class into an Interface, which then results in typescript compilation result in an `Interface declaration cannot have 'implements' clause.` error.

I'm not certain how to fix that problem. I think the `cli.ts#constructorsToInterfaces` function needs to be updated to somehow deal with the new `class X implements IX` pattern, but I have no clue how to do so. Perhaps there's a way to just use the generated `.t.ds` file instead of converting it into the namespaces file?

For reference, here's the old typescript output of `pbts`:
import * as $protobuf from "protobufjs";

/**
 * Namespace test.
 * @exports test
 * @namespace
 */
export namespace test {

    /**
     * Constructs a new Message.
     * @exports test.Message
     * @constructor
     * @param {Object} [properties] Properties to set
     */
    class Message {

        /**
         * Constructs a new Message.
         * @exports test.Message
         * @constructor
         * @param {Object} [properties] Properties to set
         */
        constructor(properties?: Object);

        /**
         * Message attribute1.
         * @type {Uint8Array|undefined}
         */
        attribute1?: Uint8Array;

        /**
         * Message attribute2.
         * @type {string|undefined}
         */
        attribute2?: string;

        /**
         * Message myRef.
         * @name test.Message#myRef
         * @type {string|undefined}
         */
        myRef?: string;

        /**
         * Creates a new Message instance using the specified properties.
         * @param {Object} [properties] Properties to set
         * @returns {test.Message} Message instance
         */
        static create(properties?: Object): test.Message;

        /**
         * Encodes the specified Message message.
         * @param {test.Message|Object} message Message message or plain object to encode
         * @param {$protobuf.Writer} [writer] Writer to encode to
         * @returns {$protobuf.Writer} Writer
         */
        static encode(message: (test.Message|Object), writer?: $protobuf.Writer): $protobuf.Writer;

        /**
         * Encodes the specified Message message, length delimited.
         * @param {test.Message|Object} message Message message or plain object to encode
         * @param {$protobuf.Writer} [writer] Writer to encode to
         * @returns {$protobuf.Writer} Writer
         */
        static encodeDelimited(message: (test.Message|Object), writer?: $protobuf.Writer): $protobuf.Writer;

        /**
         * Decodes a Message message from the specified reader or buffer.
         * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
         * @param {number} [length] Message length if known beforehand
         * @returns {test.Message} Message
         */
        static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): test.Message;

        /**
         * Decodes a Message message from the specified reader or buffer, length delimited.
         * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
         * @returns {test.Message} Message
         */
        static decodeDelimited(reader: ($protobuf.Reader|Uint8Array)): test.Message;

        /**
         * Verifies a Message message.
         * @param {test.Message|Object} message Message message or plain object to verify
         * @returns {?string} `null` if valid, otherwise the reason why it is not
         */
        static verify(message: (test.Message|Object)): string;

        /**
         * Creates a Message message from a plain object. Also converts values to their respective internal types.
         * @param {Object.<string,*>} object Plain object
         * @returns {test.Message} Message
         */
        static fromObject(object: { [k: string]: any }): test.Message;

        /**
         * Creates a Message message from a plain object. Also converts values to their respective internal types.
         * This is an alias of {@link test.Message.fromObject}.
         * @function
         * @param {Object.<string,*>} object Plain object
         * @returns {test.Message} Message
         */
        static from(object: { [k: string]: any }): test.Message;

        /**
         * Creates a plain object from a Message message. Also converts values to other types if specified.
         * @param {test.Message} message Message
         * @param {$protobuf.ConversionOptions} [options] Conversion options
         * @returns {Object.<string,*>} Plain object
         */
        static toObject(message: test.Message, options?: $protobuf.ConversionOptions): { [k: string]: any };

        /**
         * Creates a plain object from this Message message. Also converts values to other types if specified.
         * @param {$protobuf.ConversionOptions} [options] Conversion options
         * @returns {Object.<string,*>} Plain object
         */
        toObject(options?: $protobuf.ConversionOptions): { [k: string]: any };

        /**
         * Converts this Message to JSON.
         * @returns {Object.<string,*>} JSON object
         */
        toJSON(): { [k: string]: any };
    }
}

from rxjs-grpc.

kondi avatar kondi commented on July 3, 2024

Released in v0.2.0.

from rxjs-grpc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.