@bufbuild/protobuf
Advanced tools
Comparing version 2.0.0-alpha.3 to 2.0.0-alpha.4
@@ -43,6 +43,6 @@ "use strict"; | ||
case "list": | ||
// eslint-disable-next-line no-case-declarations | ||
const list = o.get(f); | ||
for (const item of i.get(f)) { | ||
// TODO fix type error | ||
// @ts-expect-error TODO | ||
const err = o.addListItem(f, cloneSingular(f, item)); | ||
const err = list.add(cloneSingular(f, item)); | ||
if (err) { | ||
@@ -54,4 +54,7 @@ throw err; | ||
case "map": | ||
// eslint-disable-next-line no-case-declarations | ||
const map = o.get(f); | ||
for (const entry of i.get(f).entries()) { | ||
const err = o.setMapEntry(f, entry[0], cloneSingular(f, entry[1])); | ||
// @ts-expect-error TODO fix type error | ||
const err = map.set(entry[0], cloneSingular(f, entry[1])); | ||
if (err) { | ||
@@ -58,0 +61,0 @@ throw err; |
@@ -30,3 +30,3 @@ import { type DescField, type DescMessage } from "./descriptors.js"; | ||
*/ | ||
export declare function mergeFromBinary<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, bytes: Uint8Array, options?: Partial<BinaryReadOptions>): void; | ||
export declare function mergeFromBinary<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, bytes: Uint8Array, options?: Partial<BinaryReadOptions>): MessageShape<Desc>; | ||
/** | ||
@@ -33,0 +33,0 @@ * @private |
@@ -48,2 +48,3 @@ "use strict"; | ||
readMessage((0, reflect_js_1.reflect)(messageDesc, target), new binary_encoding_js_1.BinaryReader(bytes), makeReadOptions(options), false, bytes.byteLength); | ||
return target; | ||
} | ||
@@ -103,6 +104,6 @@ exports.mergeFromBinary = mergeFromBinary; | ||
case "list": | ||
readListField(message, reader, options, field, wireType); | ||
readListField(reader, wireType, message.get(field), options); | ||
break; | ||
case "map": | ||
readMapEntry(message, field, reader, options); | ||
readMapEntry(reader, message.get(field), options); | ||
break; | ||
@@ -113,3 +114,4 @@ } | ||
// Read a map field, expecting key field = 1, value field = 2 | ||
function readMapEntry(message, field, reader, options) { | ||
function readMapEntry(reader, map, options) { | ||
const field = map.field(); | ||
let key, val; | ||
@@ -154,8 +156,9 @@ const end = reader.pos + reader.uint32(); | ||
} | ||
message.setMapEntry(field, key, val); | ||
map.set(key, val); | ||
} | ||
function readListField(message, reader, options, field, wireType) { | ||
function readListField(reader, wireType, list, options) { | ||
var _a; | ||
const field = list.field(); | ||
if (field.listKind === "message") { | ||
message.addListItem(field, readMessageField(reader, options, field)); | ||
list.add(readMessageField(reader, options, field)); | ||
return; | ||
@@ -168,3 +171,3 @@ } | ||
if (!packed) { | ||
message.addListItem(field, readScalar(reader, scalarType)); | ||
list.add(readScalar(reader, scalarType)); | ||
return; | ||
@@ -174,3 +177,3 @@ } | ||
while (reader.pos < e) { | ||
message.addListItem(field, readScalar(reader, scalarType)); | ||
list.add(readScalar(reader, scalarType)); | ||
} | ||
@@ -177,0 +180,0 @@ } |
@@ -34,3 +34,3 @@ import { type DescMessage } from "./descriptors.js"; | ||
*/ | ||
export declare function mergeFromJsonString<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, json: string, options?: Partial<JsonReadOptions>): void; | ||
export declare function mergeFromJsonString<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, json: string, options?: Partial<JsonReadOptions>): MessageShape<Desc>; | ||
/** | ||
@@ -49,2 +49,2 @@ * Parse a message from a JSON value. | ||
*/ | ||
export declare function mergeFromJson<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, json: JsonValue, options?: Partial<JsonReadOptions>): void; | ||
export declare function mergeFromJson<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, json: JsonValue, options?: Partial<JsonReadOptions>): MessageShape<Desc>; |
@@ -52,3 +52,3 @@ "use strict"; | ||
function mergeFromJsonString(messageDesc, target, json, options) { | ||
mergeFromJson(messageDesc, target, parseJsonString(json, messageDesc.typeName), options); | ||
return mergeFromJson(messageDesc, target, parseJsonString(json, messageDesc.typeName), options); | ||
} | ||
@@ -98,2 +98,3 @@ exports.mergeFromJsonString = mergeFromJsonString; | ||
} | ||
return target; | ||
} | ||
@@ -158,13 +159,14 @@ exports.mergeFromJson = mergeFromJson; | ||
case "list": | ||
readListField(msg, field, json, opts); | ||
readListField(msg.get(field), json, opts); | ||
break; | ||
case "map": | ||
readMapField(msg, field, json, opts); | ||
readMapField(msg.get(field), json, opts); | ||
break; | ||
} | ||
} | ||
function readMapField(msg, field, json, opts) { | ||
function readMapField(map, json, opts) { | ||
if (json === null) { | ||
return; | ||
} | ||
const field = map.field(); | ||
if (typeof json != "object" || Array.isArray(json)) { | ||
@@ -177,3 +179,3 @@ throw new error_js_1.FieldError(field, "expected object, got " + (0, reflect_check_js_1.formatVal)(json)); | ||
} | ||
const key = mapKeyFromJson(field.mapKey, jsonMapKey); | ||
let value; | ||
switch (field.mapKind) { | ||
@@ -183,36 +185,28 @@ case "message": | ||
readMessage(msgValue, jsonMapValue, opts); | ||
// TODO fix types | ||
// @ts-expect-error TODO | ||
const err = msg.setMapEntry(field, key, msgValue); | ||
if (err) { | ||
throw err; | ||
} | ||
value = msgValue; | ||
break; | ||
case "enum": | ||
const enumValue = readEnum(field.enum, jsonMapValue, opts.ignoreUnknownFields, true); | ||
if (enumValue !== tokenIgnoredUnknownEnum) { | ||
// TODO fix types | ||
// @ts-expect-error TODO | ||
const err = msg.setMapEntry(field, key, enumValue); | ||
if (err) { | ||
throw err; | ||
} | ||
value = readEnum(field.enum, jsonMapValue, opts.ignoreUnknownFields, true); | ||
if (value === tokenIgnoredUnknownEnum) { | ||
return; | ||
} | ||
break; | ||
case "scalar": | ||
const err2 = msg.setMapEntry(field, | ||
// TODO fix types | ||
// @ts-expect-error TODO | ||
key, scalarFromJson(field, jsonMapValue, true)); | ||
if (err2) { | ||
throw err2; | ||
} | ||
value = scalarFromJson(field, jsonMapValue, true); | ||
break; | ||
} | ||
const key = mapKeyFromJson(field.mapKey, jsonMapKey); | ||
// TODO fix types | ||
// @ts-expect-error TODO | ||
const err = map.set(key, value); | ||
if (err) { | ||
throw err; | ||
} | ||
} | ||
} | ||
function readListField(msg, field, json, opts) { | ||
function readListField(list, json, opts) { | ||
if (json === null) { | ||
return; | ||
} | ||
const field = list.field(); | ||
if (!Array.isArray(json)) { | ||
@@ -229,3 +223,3 @@ throw new error_js_1.FieldError(field, "expected Array, got " + (0, reflect_check_js_1.formatVal)(json)); | ||
readMessage(msgValue, jsonItem, opts); | ||
msg.addListItem(field, msgValue); | ||
list.add(msgValue); | ||
break; | ||
@@ -235,7 +229,7 @@ case "enum": | ||
if (enumValue !== tokenIgnoredUnknownEnum) { | ||
msg.addListItem(field, enumValue); | ||
list.add(enumValue); | ||
} | ||
break; | ||
case "scalar": | ||
const err = msg.addListItem(field, scalarFromJson(field, jsonItem, true)); | ||
const err = list.add(scalarFromJson(field, jsonItem, true)); | ||
if (err) { | ||
@@ -242,0 +236,0 @@ throw err; |
@@ -18,3 +18,2 @@ "use strict"; | ||
const unsafe_js_1 = require("./unsafe.js"); | ||
const is_message_js_1 = require("../is-message.js"); | ||
function isObject(arg) { | ||
@@ -75,6 +74,7 @@ return arg !== null && typeof arg == "object" && !Array.isArray(arg); | ||
unsafe_js_1.unsafeLocal in arg && | ||
"message" in arg && | ||
(0, is_message_js_1.isMessage)(arg.message) && | ||
(messageDesc === undefined || arg.message.$typeName == messageDesc.typeName)); | ||
"desc" in arg && | ||
isObject(arg.desc) && | ||
arg.desc.kind === "message" && | ||
(messageDesc === undefined || arg.desc.typeName == messageDesc.typeName)); | ||
} | ||
exports.isReflectMessage = isReflectMessage; |
@@ -113,14 +113,2 @@ import { type DescField, type DescMessage, type DescOneof } from "../descriptors.js"; | ||
/** | ||
* Add an item to a list field. | ||
*/ | ||
addListItem<Field extends DescField & { | ||
fieldKind: "list"; | ||
}>(field: Field, value: ReflectAddListItemValue<Field>): FieldError | undefined; | ||
/** | ||
* Set a map entry. | ||
*/ | ||
setMapEntry<Field extends DescField & { | ||
fieldKind: "map"; | ||
}>(field: Field, key: MapEntryKey, value: ReflectSetMapEntryValue<Field>): FieldError | undefined; | ||
/** | ||
* Returns the unknown fields of the message. | ||
@@ -259,28 +247,2 @@ */ | ||
} ? ScalarValue<T> : never); | ||
/** | ||
* The type of the "value" argument of ReflectMessage.addListItem() | ||
*/ | ||
export type ReflectAddListItemValue<Field extends DescField & { | ||
fieldKind: "list"; | ||
}> = (Field extends { | ||
listKind: "scalar"; | ||
scalar: infer T; | ||
} ? ScalarValue<T> : Field extends { | ||
listKind: "enum"; | ||
} ? enumVal : Field extends { | ||
listKind: "message"; | ||
} ? ReflectMessage : never); | ||
/** | ||
* The type of the "value" argument of ReflectMessage.setMapEntry() | ||
*/ | ||
export type ReflectSetMapEntryValue<Field extends DescField & { | ||
fieldKind: "map"; | ||
}> = (Field extends { | ||
mapKind: "enum"; | ||
} ? enumVal : Field extends { | ||
mapKind: "message"; | ||
} ? ReflectMessage : Field extends { | ||
mapKind: "scalar"; | ||
scalar: infer T; | ||
} ? ScalarValue<T> : never); | ||
export {}; |
@@ -45,2 +45,4 @@ "use strict"; | ||
check = true) { | ||
this.lists = new Map(); | ||
this.maps = new Map(); | ||
this.check = check; | ||
@@ -76,7 +78,15 @@ this.desc = messageDesc; | ||
case "list": | ||
return new ReflectListImpl(field, value, this.check); | ||
// eslint-disable-next-line no-case-declarations | ||
let list = this.lists.get(field); | ||
if (!list || list[unsafe_js_1.unsafeLocal] !== value) { | ||
this.lists.set(field, (list = new ReflectListImpl(field, value, this.check))); | ||
} | ||
return list; | ||
case "map": | ||
// TODO fix types | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return reflectMap(field, value, this.check); // eslint-disable-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return | ||
// eslint-disable-next-line no-case-declarations | ||
let map = this.maps.get(field); | ||
if (!map || map[unsafe_js_1.unsafeLocal] !== value) { | ||
this.maps.set(field, (map = new ReflectMapImpl(field, value, this.check))); | ||
} | ||
return map; | ||
case "message": | ||
@@ -122,26 +132,2 @@ if (value !== undefined && | ||
} | ||
addListItem(field, value) { | ||
assertOwn(this.message, field); | ||
assertKind(field, "list"); | ||
if (this.check) { | ||
if ((0, reflect_check_js_1.checkListItem)(field, 0, value)) { | ||
const arr = (0, unsafe_js_1.unsafeGet)(this.message, field); | ||
return (0, reflect_check_js_1.checkListItem)(field, arr.length, value); | ||
} | ||
} | ||
(0, unsafe_js_1.unsafeAddListItem)(this.message, field, listItemToLocal(field, value)); | ||
return undefined; | ||
} | ||
setMapEntry(field, key, value) { | ||
assertOwn(this.message, field); | ||
assertKind(field, "map"); | ||
if (this.check) { | ||
const err = (0, reflect_check_js_1.checkMapEntry)(field, key, value); | ||
if (err) { | ||
return err; | ||
} | ||
} | ||
(0, unsafe_js_1.unsafeSetMapEntry)(this.message, field, mapKeyToLocal(key), mapValueToLocal(field, value)); | ||
return undefined; | ||
} | ||
getUnknown() { | ||
@@ -154,7 +140,2 @@ return this.message.$unknown; | ||
} | ||
function assertKind(field, kind) { | ||
if (field.fieldKind != kind) { | ||
throw new error_js_1.FieldError(field, `${field.toString()} is ${field.fieldKind}`, "ForeignFieldError"); | ||
} | ||
} | ||
function assertOwn(owner, member) { | ||
@@ -161,0 +142,0 @@ if (member.parent.typeName !== owner.$typeName) { |
@@ -43,17 +43,1 @@ import type { DescField, DescOneof } from "../descriptors.js"; | ||
field: DescField): void; | ||
/** | ||
* Add an item to a list field. | ||
* | ||
* @private | ||
*/ | ||
export declare function unsafeAddListItem(target: Record<string, unknown>, field: DescField & { | ||
fieldKind: "list"; | ||
}, value: unknown): void; | ||
/** | ||
* Set a map entry. | ||
* | ||
* @private | ||
*/ | ||
export declare function unsafeSetMapEntry(target: Record<string, unknown>, field: DescField & { | ||
fieldKind: "map"; | ||
}, key: string | number, value: unknown): void; |
@@ -16,3 +16,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.unsafeSetMapEntry = exports.unsafeAddListItem = exports.unsafeClear = exports.unsafeSet = exports.unsafeGet = exports.unsafeIsSetExplicit = exports.unsafeIsSet = exports.unsafeOneofCase = exports.unsafeLocal = void 0; | ||
exports.unsafeClear = exports.unsafeSet = exports.unsafeGet = exports.unsafeIsSetExplicit = exports.unsafeIsSet = exports.unsafeOneofCase = exports.unsafeLocal = void 0; | ||
const scalar_js_1 = require("./scalar.js"); | ||
@@ -150,19 +150,1 @@ // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name: FeatureSet_FieldPresence.$localName = $number; | ||
exports.unsafeClear = unsafeClear; | ||
/** | ||
* Add an item to a list field. | ||
* | ||
* @private | ||
*/ | ||
function unsafeAddListItem(target, field, value) { | ||
target[field.localName].push(value); | ||
} | ||
exports.unsafeAddListItem = unsafeAddListItem; | ||
/** | ||
* Set a map entry. | ||
* | ||
* @private | ||
*/ | ||
function unsafeSetMapEntry(target, field, key, value) { | ||
target[field.localName][key] = value; | ||
} | ||
exports.unsafeSetMapEntry = unsafeSetMapEntry; |
@@ -465,27 +465,48 @@ "use strict"; | ||
/** | ||
* Assert a valid signed protobuf 32-bit integer. | ||
* Assert a valid signed protobuf 32-bit integer as a number or string. | ||
*/ | ||
function assertInt32(arg) { | ||
if (typeof arg !== "number") | ||
if (typeof arg == "string") { | ||
arg = Number(arg); | ||
} | ||
else if (typeof arg != "number") { | ||
throw new Error("invalid int32: " + typeof arg); | ||
if (!Number.isInteger(arg) || arg > exports.INT32_MAX || arg < exports.INT32_MIN) | ||
} | ||
if (!Number.isInteger(arg) || | ||
arg > exports.INT32_MAX || | ||
arg < exports.INT32_MIN) | ||
throw new Error("invalid int32: " + arg); | ||
} | ||
/** | ||
* Assert a valid unsigned protobuf 32-bit integer. | ||
* Assert a valid unsigned protobuf 32-bit integer as a number or string. | ||
*/ | ||
function assertUInt32(arg) { | ||
if (typeof arg !== "number") | ||
if (typeof arg == "string") { | ||
arg = Number(arg); | ||
} | ||
else if (typeof arg != "number") { | ||
throw new Error("invalid uint32: " + typeof arg); | ||
if (!Number.isInteger(arg) || arg > exports.UINT32_MAX || arg < 0) | ||
} | ||
if (!Number.isInteger(arg) || | ||
arg > exports.UINT32_MAX || | ||
arg < 0) | ||
throw new Error("invalid uint32: " + arg); | ||
} | ||
/** | ||
* Assert a valid protobuf float value. | ||
* Assert a valid protobuf float value as a number or string. | ||
*/ | ||
function assertFloat32(arg) { | ||
if (typeof arg !== "number") | ||
if (typeof arg == "string") { | ||
const o = arg; | ||
arg = Number(arg); | ||
if (isNaN(arg) && o !== "NaN") { | ||
throw new Error("invalid float32: " + o); | ||
} | ||
} | ||
else if (typeof arg != "number") { | ||
throw new Error("invalid float32: " + typeof arg); | ||
if (Number.isFinite(arg) && (arg > exports.FLOAT32_MAX || arg < exports.FLOAT32_MIN)) | ||
} | ||
if (Number.isFinite(arg) && | ||
(arg > exports.FLOAT32_MAX || arg < exports.FLOAT32_MIN)) | ||
throw new Error("invalid float32: " + arg); | ||
} |
@@ -38,2 +38,2 @@ import type { Message, MessageShape } from "../types.js"; | ||
*/ | ||
export declare function anyUnpackTo<Desc extends DescMessage>(any: Any, messageDesc: Desc, message: MessageShape<Desc>): void; | ||
export declare function anyUnpackTo<Desc extends DescMessage>(any: Any, messageDesc: Desc, message: MessageShape<Desc>): MessageShape<Desc> | undefined; |
@@ -39,6 +39,6 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
case "list": | ||
// eslint-disable-next-line no-case-declarations | ||
const list = o.get(f); | ||
for (const item of i.get(f)) { | ||
// TODO fix type error | ||
// @ts-expect-error TODO | ||
const err = o.addListItem(f, cloneSingular(f, item)); | ||
const err = list.add(cloneSingular(f, item)); | ||
if (err) { | ||
@@ -50,4 +50,7 @@ throw err; | ||
case "map": | ||
// eslint-disable-next-line no-case-declarations | ||
const map = o.get(f); | ||
for (const entry of i.get(f).entries()) { | ||
const err = o.setMapEntry(f, entry[0], cloneSingular(f, entry[1])); | ||
// @ts-expect-error TODO fix type error | ||
const err = map.set(entry[0], cloneSingular(f, entry[1])); | ||
if (err) { | ||
@@ -54,0 +57,0 @@ throw err; |
@@ -30,3 +30,3 @@ import { type DescField, type DescMessage } from "./descriptors.js"; | ||
*/ | ||
export declare function mergeFromBinary<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, bytes: Uint8Array, options?: Partial<BinaryReadOptions>): void; | ||
export declare function mergeFromBinary<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, bytes: Uint8Array, options?: Partial<BinaryReadOptions>): MessageShape<Desc>; | ||
/** | ||
@@ -33,0 +33,0 @@ * @private |
@@ -44,2 +44,3 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
readMessage(reflect(messageDesc, target), new BinaryReader(bytes), makeReadOptions(options), false, bytes.byteLength); | ||
return target; | ||
} | ||
@@ -98,6 +99,6 @@ /** | ||
case "list": | ||
readListField(message, reader, options, field, wireType); | ||
readListField(reader, wireType, message.get(field), options); | ||
break; | ||
case "map": | ||
readMapEntry(message, field, reader, options); | ||
readMapEntry(reader, message.get(field), options); | ||
break; | ||
@@ -107,3 +108,4 @@ } | ||
// Read a map field, expecting key field = 1, value field = 2 | ||
function readMapEntry(message, field, reader, options) { | ||
function readMapEntry(reader, map, options) { | ||
const field = map.field(); | ||
let key, val; | ||
@@ -148,8 +150,9 @@ const end = reader.pos + reader.uint32(); | ||
} | ||
message.setMapEntry(field, key, val); | ||
map.set(key, val); | ||
} | ||
function readListField(message, reader, options, field, wireType) { | ||
function readListField(reader, wireType, list, options) { | ||
var _a; | ||
const field = list.field(); | ||
if (field.listKind === "message") { | ||
message.addListItem(field, readMessageField(reader, options, field)); | ||
list.add(readMessageField(reader, options, field)); | ||
return; | ||
@@ -162,3 +165,3 @@ } | ||
if (!packed) { | ||
message.addListItem(field, readScalar(reader, scalarType)); | ||
list.add(readScalar(reader, scalarType)); | ||
return; | ||
@@ -168,3 +171,3 @@ } | ||
while (reader.pos < e) { | ||
message.addListItem(field, readScalar(reader, scalarType)); | ||
list.add(readScalar(reader, scalarType)); | ||
} | ||
@@ -171,0 +174,0 @@ } |
@@ -34,3 +34,3 @@ import { type DescMessage } from "./descriptors.js"; | ||
*/ | ||
export declare function mergeFromJsonString<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, json: string, options?: Partial<JsonReadOptions>): void; | ||
export declare function mergeFromJsonString<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, json: string, options?: Partial<JsonReadOptions>): MessageShape<Desc>; | ||
/** | ||
@@ -49,2 +49,2 @@ * Parse a message from a JSON value. | ||
*/ | ||
export declare function mergeFromJson<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, json: JsonValue, options?: Partial<JsonReadOptions>): void; | ||
export declare function mergeFromJson<Desc extends DescMessage>(messageDesc: Desc, target: MessageShape<Desc>, json: JsonValue, options?: Partial<JsonReadOptions>): MessageShape<Desc>; |
@@ -48,3 +48,3 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
export function mergeFromJsonString(messageDesc, target, json, options) { | ||
mergeFromJson(messageDesc, target, parseJsonString(json, messageDesc.typeName), options); | ||
return mergeFromJson(messageDesc, target, parseJsonString(json, messageDesc.typeName), options); | ||
} | ||
@@ -92,2 +92,3 @@ /** | ||
} | ||
return target; | ||
} | ||
@@ -151,13 +152,14 @@ function readMessage(msg, json, opts) { | ||
case "list": | ||
readListField(msg, field, json, opts); | ||
readListField(msg.get(field), json, opts); | ||
break; | ||
case "map": | ||
readMapField(msg, field, json, opts); | ||
readMapField(msg.get(field), json, opts); | ||
break; | ||
} | ||
} | ||
function readMapField(msg, field, json, opts) { | ||
function readMapField(map, json, opts) { | ||
if (json === null) { | ||
return; | ||
} | ||
const field = map.field(); | ||
if (typeof json != "object" || Array.isArray(json)) { | ||
@@ -170,3 +172,3 @@ throw new FieldError(field, "expected object, got " + formatVal(json)); | ||
} | ||
const key = mapKeyFromJson(field.mapKey, jsonMapKey); | ||
let value; | ||
switch (field.mapKind) { | ||
@@ -176,36 +178,28 @@ case "message": | ||
readMessage(msgValue, jsonMapValue, opts); | ||
// TODO fix types | ||
// @ts-expect-error TODO | ||
const err = msg.setMapEntry(field, key, msgValue); | ||
if (err) { | ||
throw err; | ||
} | ||
value = msgValue; | ||
break; | ||
case "enum": | ||
const enumValue = readEnum(field.enum, jsonMapValue, opts.ignoreUnknownFields, true); | ||
if (enumValue !== tokenIgnoredUnknownEnum) { | ||
// TODO fix types | ||
// @ts-expect-error TODO | ||
const err = msg.setMapEntry(field, key, enumValue); | ||
if (err) { | ||
throw err; | ||
} | ||
value = readEnum(field.enum, jsonMapValue, opts.ignoreUnknownFields, true); | ||
if (value === tokenIgnoredUnknownEnum) { | ||
return; | ||
} | ||
break; | ||
case "scalar": | ||
const err2 = msg.setMapEntry(field, | ||
// TODO fix types | ||
// @ts-expect-error TODO | ||
key, scalarFromJson(field, jsonMapValue, true)); | ||
if (err2) { | ||
throw err2; | ||
} | ||
value = scalarFromJson(field, jsonMapValue, true); | ||
break; | ||
} | ||
const key = mapKeyFromJson(field.mapKey, jsonMapKey); | ||
// TODO fix types | ||
// @ts-expect-error TODO | ||
const err = map.set(key, value); | ||
if (err) { | ||
throw err; | ||
} | ||
} | ||
} | ||
function readListField(msg, field, json, opts) { | ||
function readListField(list, json, opts) { | ||
if (json === null) { | ||
return; | ||
} | ||
const field = list.field(); | ||
if (!Array.isArray(json)) { | ||
@@ -222,3 +216,3 @@ throw new FieldError(field, "expected Array, got " + formatVal(json)); | ||
readMessage(msgValue, jsonItem, opts); | ||
msg.addListItem(field, msgValue); | ||
list.add(msgValue); | ||
break; | ||
@@ -228,7 +222,7 @@ case "enum": | ||
if (enumValue !== tokenIgnoredUnknownEnum) { | ||
msg.addListItem(field, enumValue); | ||
list.add(enumValue); | ||
} | ||
break; | ||
case "scalar": | ||
const err = msg.addListItem(field, scalarFromJson(field, jsonItem, true)); | ||
const err = list.add(scalarFromJson(field, jsonItem, true)); | ||
if (err) { | ||
@@ -235,0 +229,0 @@ throw err; |
@@ -15,3 +15,2 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
import { unsafeLocal } from "./unsafe.js"; | ||
import { isMessage } from "../is-message.js"; | ||
export function isObject(arg) { | ||
@@ -68,5 +67,6 @@ return arg !== null && typeof arg == "object" && !Array.isArray(arg); | ||
unsafeLocal in arg && | ||
"message" in arg && | ||
isMessage(arg.message) && | ||
(messageDesc === undefined || arg.message.$typeName == messageDesc.typeName)); | ||
"desc" in arg && | ||
isObject(arg.desc) && | ||
arg.desc.kind === "message" && | ||
(messageDesc === undefined || arg.desc.typeName == messageDesc.typeName)); | ||
} |
@@ -113,14 +113,2 @@ import { type DescField, type DescMessage, type DescOneof } from "../descriptors.js"; | ||
/** | ||
* Add an item to a list field. | ||
*/ | ||
addListItem<Field extends DescField & { | ||
fieldKind: "list"; | ||
}>(field: Field, value: ReflectAddListItemValue<Field>): FieldError | undefined; | ||
/** | ||
* Set a map entry. | ||
*/ | ||
setMapEntry<Field extends DescField & { | ||
fieldKind: "map"; | ||
}>(field: Field, key: MapEntryKey, value: ReflectSetMapEntryValue<Field>): FieldError | undefined; | ||
/** | ||
* Returns the unknown fields of the message. | ||
@@ -259,28 +247,2 @@ */ | ||
} ? ScalarValue<T> : never); | ||
/** | ||
* The type of the "value" argument of ReflectMessage.addListItem() | ||
*/ | ||
export type ReflectAddListItemValue<Field extends DescField & { | ||
fieldKind: "list"; | ||
}> = (Field extends { | ||
listKind: "scalar"; | ||
scalar: infer T; | ||
} ? ScalarValue<T> : Field extends { | ||
listKind: "enum"; | ||
} ? enumVal : Field extends { | ||
listKind: "message"; | ||
} ? ReflectMessage : never); | ||
/** | ||
* The type of the "value" argument of ReflectMessage.setMapEntry() | ||
*/ | ||
export type ReflectSetMapEntryValue<Field extends DescField & { | ||
fieldKind: "map"; | ||
}> = (Field extends { | ||
mapKind: "enum"; | ||
} ? enumVal : Field extends { | ||
mapKind: "message"; | ||
} ? ReflectMessage : Field extends { | ||
mapKind: "scalar"; | ||
scalar: infer T; | ||
} ? ScalarValue<T> : never); | ||
export {}; |
@@ -17,3 +17,3 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
import { FieldError } from "./error.js"; | ||
import { unsafeAddListItem, unsafeClear, unsafeGet, unsafeIsSet, unsafeLocal, unsafeOneofCase, unsafeSet, unsafeSetMapEntry, } from "./unsafe.js"; | ||
import { unsafeClear, unsafeGet, unsafeIsSet, unsafeLocal, unsafeOneofCase, unsafeSet, } from "./unsafe.js"; | ||
import { create } from "../create.js"; | ||
@@ -42,2 +42,4 @@ import { isWrapper, isWrapperDesc } from "../wkt/wrappers.js"; | ||
check = true) { | ||
this.lists = new Map(); | ||
this.maps = new Map(); | ||
this.check = check; | ||
@@ -73,7 +75,15 @@ this.desc = messageDesc; | ||
case "list": | ||
return new ReflectListImpl(field, value, this.check); | ||
// eslint-disable-next-line no-case-declarations | ||
let list = this.lists.get(field); | ||
if (!list || list[unsafeLocal] !== value) { | ||
this.lists.set(field, (list = new ReflectListImpl(field, value, this.check))); | ||
} | ||
return list; | ||
case "map": | ||
// TODO fix types | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return reflectMap(field, value, this.check); // eslint-disable-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return | ||
// eslint-disable-next-line no-case-declarations | ||
let map = this.maps.get(field); | ||
if (!map || map[unsafeLocal] !== value) { | ||
this.maps.set(field, (map = new ReflectMapImpl(field, value, this.check))); | ||
} | ||
return map; | ||
case "message": | ||
@@ -119,26 +129,2 @@ if (value !== undefined && | ||
} | ||
addListItem(field, value) { | ||
assertOwn(this.message, field); | ||
assertKind(field, "list"); | ||
if (this.check) { | ||
if (checkListItem(field, 0, value)) { | ||
const arr = unsafeGet(this.message, field); | ||
return checkListItem(field, arr.length, value); | ||
} | ||
} | ||
unsafeAddListItem(this.message, field, listItemToLocal(field, value)); | ||
return undefined; | ||
} | ||
setMapEntry(field, key, value) { | ||
assertOwn(this.message, field); | ||
assertKind(field, "map"); | ||
if (this.check) { | ||
const err = checkMapEntry(field, key, value); | ||
if (err) { | ||
return err; | ||
} | ||
} | ||
unsafeSetMapEntry(this.message, field, mapKeyToLocal(key), mapValueToLocal(field, value)); | ||
return undefined; | ||
} | ||
getUnknown() { | ||
@@ -151,7 +137,2 @@ return this.message.$unknown; | ||
} | ||
function assertKind(field, kind) { | ||
if (field.fieldKind != kind) { | ||
throw new FieldError(field, `${field.toString()} is ${field.fieldKind}`, "ForeignFieldError"); | ||
} | ||
} | ||
function assertOwn(owner, member) { | ||
@@ -158,0 +139,0 @@ if (member.parent.typeName !== owner.$typeName) { |
@@ -43,17 +43,1 @@ import type { DescField, DescOneof } from "../descriptors.js"; | ||
field: DescField): void; | ||
/** | ||
* Add an item to a list field. | ||
* | ||
* @private | ||
*/ | ||
export declare function unsafeAddListItem(target: Record<string, unknown>, field: DescField & { | ||
fieldKind: "list"; | ||
}, value: unknown): void; | ||
/** | ||
* Set a map entry. | ||
* | ||
* @private | ||
*/ | ||
export declare function unsafeSetMapEntry(target: Record<string, unknown>, field: DescField & { | ||
fieldKind: "map"; | ||
}, key: string | number, value: unknown): void; |
@@ -140,17 +140,1 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
} | ||
/** | ||
* Add an item to a list field. | ||
* | ||
* @private | ||
*/ | ||
export function unsafeAddListItem(target, field, value) { | ||
target[field.localName].push(value); | ||
} | ||
/** | ||
* Set a map entry. | ||
* | ||
* @private | ||
*/ | ||
export function unsafeSetMapEntry(target, field, key, value) { | ||
target[field.localName][key] = value; | ||
} |
@@ -460,27 +460,48 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
/** | ||
* Assert a valid signed protobuf 32-bit integer. | ||
* Assert a valid signed protobuf 32-bit integer as a number or string. | ||
*/ | ||
function assertInt32(arg) { | ||
if (typeof arg !== "number") | ||
if (typeof arg == "string") { | ||
arg = Number(arg); | ||
} | ||
else if (typeof arg != "number") { | ||
throw new Error("invalid int32: " + typeof arg); | ||
if (!Number.isInteger(arg) || arg > INT32_MAX || arg < INT32_MIN) | ||
} | ||
if (!Number.isInteger(arg) || | ||
arg > INT32_MAX || | ||
arg < INT32_MIN) | ||
throw new Error("invalid int32: " + arg); | ||
} | ||
/** | ||
* Assert a valid unsigned protobuf 32-bit integer. | ||
* Assert a valid unsigned protobuf 32-bit integer as a number or string. | ||
*/ | ||
function assertUInt32(arg) { | ||
if (typeof arg !== "number") | ||
if (typeof arg == "string") { | ||
arg = Number(arg); | ||
} | ||
else if (typeof arg != "number") { | ||
throw new Error("invalid uint32: " + typeof arg); | ||
if (!Number.isInteger(arg) || arg > UINT32_MAX || arg < 0) | ||
} | ||
if (!Number.isInteger(arg) || | ||
arg > UINT32_MAX || | ||
arg < 0) | ||
throw new Error("invalid uint32: " + arg); | ||
} | ||
/** | ||
* Assert a valid protobuf float value. | ||
* Assert a valid protobuf float value as a number or string. | ||
*/ | ||
function assertFloat32(arg) { | ||
if (typeof arg !== "number") | ||
if (typeof arg == "string") { | ||
const o = arg; | ||
arg = Number(arg); | ||
if (isNaN(arg) && o !== "NaN") { | ||
throw new Error("invalid float32: " + o); | ||
} | ||
} | ||
else if (typeof arg != "number") { | ||
throw new Error("invalid float32: " + typeof arg); | ||
if (Number.isFinite(arg) && (arg > FLOAT32_MAX || arg < FLOAT32_MIN)) | ||
} | ||
if (Number.isFinite(arg) && | ||
(arg > FLOAT32_MAX || arg < FLOAT32_MIN)) | ||
throw new Error("invalid float32: " + arg); | ||
} |
@@ -38,2 +38,2 @@ import type { Message, MessageShape } from "../types.js"; | ||
*/ | ||
export declare function anyUnpackTo<Desc extends DescMessage>(any: Any, messageDesc: Desc, message: MessageShape<Desc>): void; | ||
export declare function anyUnpackTo<Desc extends DescMessage>(any: Any, messageDesc: Desc, message: MessageShape<Desc>): MessageShape<Desc> | undefined; |
{ | ||
"name": "@bufbuild/protobuf", | ||
"version": "2.0.0-alpha.3", | ||
"version": "2.0.0-alpha.4", | ||
"license": "(Apache-2.0 AND BSD-3-Clause)", | ||
@@ -5,0 +5,0 @@ "description": "A complete implementation of Protocol Buffers in TypeScript, suitable for web browsers and Node.js.", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1150414
29809