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

Fix generated code compatibility with strict null checks #2

Closed

Conversation

yunyu
Copy link

@yunyu yunyu commented Dec 11, 2019

For this proto file:

syntax = "proto3";

message JoinMessage { string channel = 1; }

message ChatMessage {
  uint32 id = 1;
  string message = 2;
}

message RPCMessage {
  oneof msg {
    JoinMessage join = 1;
    ChatMessage chat = 2;
  }
}

This change generates:

// code generated by pbf v3.2.0
/* tslint:disable */

import Pbf from 'pbf';

export interface IJoinMessage {
    channel?: string;
}

export interface IChatMessage {
    id?: number;
    message?: string;
}

export interface IRPCMessage {
    msg?: "join" | "chat";
    join?: IJoinMessage;
    chat?: IChatMessage;
}

export const JoinMessage = {
    read(pbf: Pbf, end?: number): IJoinMessage {
        return pbf.readFields(JoinMessage._readField, {channel: ""}, end);
    },
    _readField(tag: number, obj: any, pbf?: Pbf): void {
        if (tag === 1 && pbf) obj.channel = pbf.readString();
    },
    write(obj: IJoinMessage, pbf?: Pbf): void {
        if (obj.channel && pbf) pbf.writeStringField(1, obj.channel);
    }
};

export const ChatMessage = {
    read(pbf: Pbf, end?: number): IChatMessage {
        return pbf.readFields(ChatMessage._readField, {id: 0, message: ""}, end);
    },
    _readField(tag: number, obj: any, pbf?: Pbf): void {
        if (tag === 1 && pbf) obj.id = pbf.readVarint();
        else if (tag === 2 && pbf) obj.message = pbf.readString();
    },
    write(obj: IChatMessage, pbf?: Pbf): void {
        if (obj.id && pbf) pbf.writeVarintField(1, obj.id);
        if (obj.message && pbf) pbf.writeStringField(2, obj.message);
    }
};

export const RPCMessage = {
    read(pbf: Pbf, end?: number): IRPCMessage {
        return pbf.readFields(RPCMessage._readField, {join: undefined, msg: undefined, chat: undefined}, end);
    },
    _readField(tag: number, obj: any, pbf?: Pbf): void {
        if (tag === 1 && pbf) obj.join = JoinMessage.read(pbf, pbf.readVarint() + pbf.pos), obj.msg = "join";
        else if (tag === 2 && pbf) obj.chat = ChatMessage.read(pbf, pbf.readVarint() + pbf.pos), obj.msg = "chat";
    },
    write(obj: IRPCMessage, pbf?: Pbf): void {
        if (obj.join && pbf) pbf.writeMessage(1, JoinMessage.write, obj.join);
        if (obj.chat && pbf) pbf.writeMessage(2, ChatMessage.write, obj.chat);
    }
};

Note the optional pbf argument and if check. This change makes the generated files compatible with @types/pbf with --strictNullChecks.

@yunyu
Copy link
Author

yunyu commented Dec 11, 2019

It looks like the issue that this fixes was discussed higher up in the thread:

mapbox#107 (comment)

Your call, it might not be worth merging this workaround and PRing a fix to @types/pbf.

@yunyu yunyu force-pushed the ylin/fix-strict-null-checks branch from 8dc37a3 to 2ca1cfe Compare December 11, 2019 14:49
@yunyu yunyu force-pushed the ylin/fix-strict-null-checks branch from 2ca1cfe to 3dc1f6d Compare December 11, 2019 14:51
@yunyu yunyu closed this Jun 30, 2020
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

Successfully merging this pull request may close these issues.

1 participant