Skip to content

Commit

Permalink
Fix some deviations from pbjs generated code
Browse files Browse the repository at this point in the history
- Add missing inverse enum mapping (number => string)
- Add missing map initializer in fromObject
- Remove incorrect map field initialization to empty array in toObject
  • Loading branch information
bduffany committed Jul 13, 2023
1 parent 5cc5e01 commit c73441e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 8 additions & 4 deletions codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ func (c *Codegen) generate(file *descriptorpb.FileDescriptorProto, sourcePath []
j.Lf(`if (typeof object.%s !== "object") {`, f.GetJsonName())
j.Lf(`throw new TypeError(".%s.%s.%s: object expected, but got " + (typeof object.%s));`, ns, messageType.GetName(), f.GetJsonName(), f.GetJsonName())
j.L("}")
j.Lf("message.%s = {};", f.GetJsonName())
j.Lf("for (let keys = Object.keys(object.%s), i = 0; i < keys.length; ++i) {", f.GetJsonName())
src := fmt.Sprintf("object.%s[keys[i]]", f.GetJsonName())
dest := fmt.Sprintf("message.%s[keys[i]]", f.GetJsonName())
Expand Down Expand Up @@ -600,7 +601,7 @@ func (c *Codegen) generate(file *descriptorpb.FileDescriptorProto, sourcePath []
// Init arrays
j.L("if (options.arrays || options.defaults) {")
for _, f := range messageType.GetField() {
if f.GetLabel() == descriptorpb.FieldDescriptorProto_LABEL_REPEATED {
if f.GetLabel() == descriptorpb.FieldDescriptorProto_LABEL_REPEATED && !c.isMapField(f) {
j.Lf("object.%s = [];", f.GetJsonName())
}
}
Expand Down Expand Up @@ -728,15 +729,18 @@ func (c *Codegen) generate(file *descriptorpb.FileDescriptorProto, sourcePath []

d.BlockComment(c.Comments[file.GetName()][fmt.Sprint(enumPath)])
d.Lf("export enum %s {", enumType.GetName())
j.Lf("%s.%s = {", curNS, enumType.GetName())
j.Lf("%s.%s = (function() {", curNS, enumType.GetName())
j.Lf("const valuesById = {};")
j.Lf("const values = Object.create(valuesById);")
for valueIndex, value := range enumType.GetValue() {
valuePath := append(enumPath, enumDescriptorValueTagNumber, int32(valueIndex))
d.BlockComment(c.Comments[file.GetName()][fmt.Sprint(valuePath)])
d.Lf("%s = %d,", value.GetName(), value.GetNumber())
j.Lf("%s: %d,", value.GetName(), value.GetNumber())
j.Lf("values[valuesById[%d] = %q] = %d;", value.GetNumber(), value.GetName(), value.GetNumber())
}
d.L("}")
j.L("};")
j.Lf("return values;")
j.L("})();")
}

// Generate services
Expand Down
18 changes: 17 additions & 1 deletion test/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,18 @@ function encode<T extends object>(

// Test encode/decode
const buffer = type.encode(message).finish();
checkEqual(message, type.decode(buffer), pbjsType.decode(buffer));
const decoded = type.decode(buffer);
const decodedPbjs = pbjsType.decode(buffer);
checkEqual(message, decoded, decodedPbjs);

// Test toJSON
const json = JSON.stringify(decoded.toJSON(), null, 2);
const jsonPbjs = JSON.stringify(decodedPbjs.toJSON(), null, 2);
if (json !== jsonPbjs) {
console.log('toJSON() does not match protobufjs!');
console.log('protobufjs-cli: ' + jsonPbjs);
console.log('protoc-gen-protobufjs: ' + json);
}
}

encode<trivial.ITrivialMessage>(
Expand Down Expand Up @@ -74,5 +85,10 @@ encode<types.IAllSimpleTypes>(
}),
bytes: new Uint8Array([12, 0, 1, 2]),
mapWithInt64: { foo: new Long(3, 5) },

// TODO: this fails due to fields being out of order, but is otherwise handled OK.
// Need to debug this and uncomment.

// repeatedSomeEnumValue: [1, 2],
}
);

0 comments on commit c73441e

Please sign in to comment.