Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long issue #921

Closed
sm2017 opened this issue Oct 3, 2017 · 6 comments
Closed

Long issue #921

sm2017 opened this issue Oct 3, 2017 · 6 comments

Comments

@sm2017
Copy link

sm2017 commented Oct 3, 2017

protobuf.js version: <6.8.0>

In the following code I use --force-long and MyProto.id is uint64 field
In generated code , in comments every thing is ok and type is Long but when you log id it is number not long (Please see generated code too)

var pbjs = require("protobufjs/cli/pbjs"); // or require("protobufjs/cli").pbjs / .pbts

pbjs.main([
    '--target', 'static-module',
    '--out', './bundle.js',
    '--es6',
    '--force-long',
    '--force-message',
    '--no-delimited',
    '--no-convert',
    '--path', __dirname + '/src/',
    'path/to/myproto.proto'
], function(err, output) {
    if (err)
        throw err;
    // do something with output
});

myproto.proto

syntax = "proto3";
package proto;

message MyProto {
    uint64 id = 1;
}

bundle.js

/*eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins*/
import * as $protobuf from "protobufjs/minimal";

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

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

export const proto = $root.proto = (() => {

    /**
     * Namespace proto.
     * @exports proto
     * @namespace
     */
    const proto = {};

    proto.MyProto = (function() {

        /**
         * Properties of a MyProto.
         * @memberof proto
         * @interface IMyProto
         * @property {Long} [id] MyProto id
         */

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

        /**
         * MyProto id.
         * @member {Long}id
         * @memberof proto.MyProto
         * @instance
         */
        MyProto.prototype.id = $util.Long ? $util.Long.fromBits(0,0,true) : 0;

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

        /**
         * Encodes the specified MyProto message. Does not implicitly {@link proto.MyProto.verify|verify} messages.
         * @function encode
         * @memberof proto.MyProto
         * @static
         * @param {proto.MyProto} message MyProto message or plain object to encode
         * @param {$protobuf.Writer} [writer] Writer to encode to
         * @returns {$protobuf.Writer} Writer
         */
        MyProto.encode = function encode(message, writer) {
            if (!writer)
                writer = $Writer.create();
            if (message.id != null && message.hasOwnProperty("id"))
                writer.uint32(/* id 1, wireType 0 =*/8).uint64(message.id);
            return writer;
        };

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

        /**
         * Verifies a MyProto message.
         * @function verify
         * @memberof proto.MyProto
         * @static
         * @param {Object.<string,*>} message Plain object to verify
         * @returns {string|null} `null` if valid, otherwise the reason why it is not
         */
        MyProto.verify = function verify(message) {
            if (typeof message !== "object" || message === null)
                return "object expected";
            if (message.id != null && message.hasOwnProperty("id"))
                if (!$util.isInteger(message.id) && !(message.id && $util.isInteger(message.id.low) && $util.isInteger(message.id.high)))
                    return "id: integer|Long expected";
            return null;
        };

        return MyProto;
    })();

    return proto;
})();

export { $root as default };

In the MyProto.decode you can see message.id = reader.uint64(); , I think it must be Long , may be not , but if you decode a serialized buffer and console.log(myProto.id); it is number not Long

@dcodeIO
Copy link
Member

dcodeIO commented Oct 3, 2017

If it returns a number instead of a Long, this implies that long.js hasn't been loaded properly. See also

@sm2017
Copy link
Author

sm2017 commented Oct 4, 2017

The following is part of my package.json

"dependencies": {
    "long": "^3.2.0",
    "protobufjs": "~6.8.0"
  }

Is there additional steps for installing long.js ?
Currently I can use Long in my codes for example the following code has correct result

import Long from 'long';
consloe.log(Long.ZERO);

@sm2017
Copy link
Author

sm2017 commented Oct 7, 2017

@dcodeIO , What is wrong here?

@dcodeIO
Copy link
Member

dcodeIO commented Oct 7, 2017

Try:

var Long = ...;

protobuf.util.Long = Long;
protobuf.configure();

@sm2017
Copy link
Author

sm2017 commented Oct 8, 2017

Fixed

@sm2017 sm2017 closed this as completed Oct 8, 2017
@sandogeek
Copy link

If you are using angular, you can insert these code to main.ts.

import * as protobuf from 'protobufjs/minimal';
import * as Long from 'long';


if (environment.production) {
  enableProdMode();
}
protobuf.util.Long = Long;
protobuf.configure();

It works fine for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants