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

V2: Rely on ReflectMessage for jstype=JS_STRING #823

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/protobuf-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ server would usually do.

| code generator | bundle size | minified | compressed |
|---------------------|------------------------:|-----------------------:|-------------------:|
| protobuf-es | 126,590 b | 65,292 b | 15,928 b |
| protobuf-es | 126,284 b | 65,142 b | 15,854 b |
| protobuf-javascript | 394,384 b | 288,654 b | 45,122 b |
25 changes: 6 additions & 19 deletions packages/protobuf/src/from-binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ export function readField(
) {
switch (field.fieldKind) {
case "scalar":
message.set(field, readScalar(reader, field.scalar, field.longType));
message.set(field, readScalar(reader, field.scalar));
break;
case "enum":
message.set(field, readScalar(reader, ScalarType.INT32));
message.set(field, readScalar(reader, ScalarType.INT32) as number);
break;
case "message":
message.set(
Expand Down Expand Up @@ -178,7 +178,7 @@ function readMapEntry(
const [fieldNo] = reader.tag();
switch (fieldNo) {
case 1:
key = readScalar(reader, field.mapKey);
key = readScalar(reader, field.mapKey) as MapEntryKey;
break;
case 2:
switch (field.mapKind) {
Expand Down Expand Up @@ -229,18 +229,17 @@ function readListField(
return;
}
const scalarType = field.scalar ?? ScalarType.INT32;
const longType = field.listKind == "scalar" ? field.longType : undefined;
const packed =
wireType == WireType.LengthDelimited &&
scalarType != ScalarType.STRING &&
scalarType != ScalarType.BYTES;
if (!packed) {
message.addListItem(field, readScalar(reader, scalarType, longType));
message.addListItem(field, readScalar(reader, scalarType));
return;
}
const e = reader.uint32() + reader.pos;
while (reader.pos < e) {
message.addListItem(field, readScalar(reader, scalarType, longType));
message.addListItem(field, readScalar(reader, scalarType));
}
}

Expand All @@ -262,19 +261,7 @@ function readMessageField(
return message;
}

function readScalar<Scalar extends ScalarType, L extends LongType>(
reader: BinaryReader,
type: Scalar,
longType?: L,
): ScalarValue<Scalar, L> {
let v = readScalarValue(reader, type);
if (longType === LongType.STRING) {
v = typeof v == "bigint" ? v.toString() : v;
}
return v as ScalarValue<Scalar, L>;
}

function readScalarValue(reader: BinaryReader, type: ScalarType): ScalarValue {
function readScalar(reader: BinaryReader, type: ScalarType): ScalarValue {
switch (type) {
case ScalarType.STRING:
return reader.string();
Expand Down
53 changes: 20 additions & 33 deletions packages/protobuf/src/from-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,33 @@ import { assertFloat32, assertInt32, assertUInt32 } from "./reflect/assert.js";
import { protoInt64 } from "./proto-int64.js";
import { create } from "./create.js";
import type { Registry } from "./reflect/registry.js";
import type { ReflectMessage, MapEntryKey } from "./reflect/reflect-types.js";
import type { MapEntryKey, ReflectMessage } from "./reflect/reflect-types.js";
import { reflect } from "./reflect/reflect.js";
import { formatVal } from "./reflect/reflect-check.js";
import {
scalarZeroValue,
LongType,
ScalarType,
type ScalarValue,
scalarZeroValue,
} from "./reflect/scalar.js";
import type { MessageShape } from "./types.js";
import { base64Decode } from "./wire/base64-encoding.js";
import { getTextEncoding } from "./wire/text-encoding.js";
import {
ListValueDesc,
NullValue,
StructDesc,
ValueDesc,
anyPack,
} from "./wkt/index.js";
import type {
Any,
Timestamp,
Duration,
Struct,
Value,
FieldMask,
ListValue,
Struct,
Timestamp,
Value,
} from "./wkt/index.js";
import {
anyPack,
ListValueDesc,
NullValue,
StructDesc,
ValueDesc,
} from "./wkt/index.js";
import { isWrapperDesc } from "./wkt/wrappers.js";
import { createExtensionContainer, setExtension } from "./extensions.js";
Expand Down Expand Up @@ -293,7 +293,7 @@ function readMapField(
msg.setMapEntry(
field,
key,
readScalar(field.scalar, jsonMapValue, LongType.BIGINT, true),
readScalar(field.scalar, jsonMapValue, true),
);
} catch (e) {
let m = `cannot decode map value for field ${msg.desc.typeName}.${field.name} from JSON: ${formatVal(jsonMapValue)}`;
Expand Down Expand Up @@ -346,10 +346,7 @@ function readListField(
break;
case "scalar":
try {
msg.addListItem(
field,
readScalar(field.scalar, jsonItem, field.longType, true),
);
msg.addListItem(field, readScalar(field.scalar, jsonItem, true));
} catch (e) {
let m = `cannot decode field ${msg.desc.typeName}.${field.name} from JSON: ${formatVal(jsonItem)}`;
if (e instanceof Error && e.message.length > 0) {
Expand Down Expand Up @@ -397,7 +394,7 @@ function readScalarField(
json: JsonValue,
) {
try {
const scalarValue = readScalar(field.scalar, json, field.longType, false);
const scalarValue = readScalar(field.scalar, json, false);
if (scalarValue === tokenNull) {
msg.clear(field);
} else {
Expand All @@ -424,7 +421,7 @@ function readMapKey(type: ScalarType, json: JsonValue) {
break;
}
}
return readScalar(type, json, LongType.BIGINT, true) as ScalarValue<
return readScalar(type, json, true) as ScalarValue<
Exclude<ScalarType, ScalarType.BYTES | ScalarType.DOUBLE | ScalarType.FLOAT>
>;
}
Expand Down Expand Up @@ -482,24 +479,21 @@ const tokenNull = Symbol();
function readScalar(
type: ScalarType,
json: JsonValue,
longType: LongType,
nullAsZeroValue: true,
): ScalarValue;
function readScalar(
type: ScalarType,
json: JsonValue,
longType: LongType,
nullAsZeroValue: false,
): ScalarValue | typeof tokenNull;
function readScalar(
type: ScalarType,
json: JsonValue,
longType: LongType,
nullAsZeroValue: boolean,
): ScalarValue | typeof tokenNull {
if (json === null) {
if (nullAsZeroValue) {
return scalarZeroValue(type, longType);
return scalarZeroValue(type, LongType.BIGINT);
}
return tokenNull;
}
Expand Down Expand Up @@ -558,15 +552,11 @@ function readScalar(
case ScalarType.SFIXED64:
case ScalarType.SINT64:
if (typeof json != "number" && typeof json != "string") break;
const long = protoInt64.parse(json);
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
return longType ? long.toString() : long;
return protoInt64.parse(json);
case ScalarType.FIXED64:
case ScalarType.UINT64:
if (typeof json != "number" && typeof json != "string") break;
const uLong = protoInt64.uParse(json);
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
return longType ? uLong.toString() : uLong;
return protoInt64.uParse(json);

// bool:
case ScalarType.BOOL:
Expand Down Expand Up @@ -642,10 +632,7 @@ function tryWktFromJson(
if (jsonValue === null) {
msg.clear(valueField);
} else {
msg.set(
valueField,
readScalar(valueField.scalar, jsonValue, valueField.longType, true),
);
msg.set(valueField, readScalar(valueField.scalar, jsonValue, true));
}
return true;
}
Expand Down
Loading