@protobuf-ts/runtime
Advanced tools
Comparing version 2.0.0-alpha.28 to 2.0.0-alpha.29
@@ -70,3 +70,3 @@ "use strict"; | ||
Object.defineProperty(exports, "ReflectionJsonWriter", { enumerable: true, get: function () { return reflection_json_writer_1.ReflectionJsonWriter; } }); | ||
// Type guarding at runtime, may be interesting to the user for his reflection ops | ||
// Oneof helpers | ||
var oneof_1 = require("./oneof"); | ||
@@ -77,2 +77,3 @@ Object.defineProperty(exports, "isOneofGroup", { enumerable: true, get: function () { return oneof_1.isOneofGroup; } }); | ||
Object.defineProperty(exports, "clearOneofValue", { enumerable: true, get: function () { return oneof_1.clearOneofValue; } }); | ||
Object.defineProperty(exports, "getSelectedOneofValue", { enumerable: true, get: function () { return oneof_1.getSelectedOneofValue; } }); | ||
// Enum object type guard and reflection util, may be interesting to the user. | ||
@@ -79,0 +80,0 @@ var enum_object_1 = require("./enum-object"); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.clearOneofValue = exports.setOneofValue = exports.getOneofValue = exports.isOneofGroup = void 0; | ||
exports.getSelectedOneofValue = exports.clearOneofValue = exports.setUnknownOneofValue = exports.setOneofValue = exports.getOneofValue = exports.isOneofGroup = void 0; | ||
/** | ||
@@ -48,23 +48,6 @@ * Is the given value a valid oneof group? | ||
/** | ||
* Returns the selected value of the given oneof group. | ||
* | ||
* Not that the recommended way to access a oneof group is to check | ||
* the "oneofKind" property and let TypeScript narrow down the union | ||
* type for you: | ||
* | ||
* ```ts | ||
* if (message.result.oneofKind === "error") { | ||
* message.result.error; // string | ||
* } | ||
* ``` | ||
* | ||
* In the rare case you just need the value, and do not care about | ||
* which protobuf field is selected, you can use this function | ||
* for convenience. | ||
* Returns the value of the given field in a oneof group. | ||
*/ | ||
function getOneofValue(oneof) { | ||
if (oneof.oneofKind === undefined) { | ||
return undefined; | ||
} | ||
return oneof[oneof.oneofKind]; | ||
function getOneofValue(oneof, kind) { | ||
return oneof[kind]; | ||
} | ||
@@ -82,4 +65,21 @@ exports.getOneofValue = getOneofValue; | ||
exports.setOneofValue = setOneofValue; | ||
function setUnknownOneofValue(oneof, kind, value) { | ||
if (oneof.oneofKind !== undefined) { | ||
delete oneof[oneof.oneofKind]; | ||
} | ||
oneof.oneofKind = kind; | ||
if (value !== undefined && kind !== undefined) { | ||
oneof[kind] = value; | ||
} | ||
} | ||
exports.setUnknownOneofValue = setUnknownOneofValue; | ||
/** | ||
* Sets a oneof group to `{ oneofKind: undefined }` | ||
* Removes the selected field in a oneof group. | ||
* | ||
* Note that the recommended way to modify a oneof group is to set | ||
* a new object: | ||
* | ||
* ```ts | ||
* message.result = { oneofKind: undefined }; | ||
* ``` | ||
*/ | ||
@@ -93,1 +93,25 @@ function clearOneofValue(oneof) { | ||
exports.clearOneofValue = clearOneofValue; | ||
/** | ||
* Returns the selected value of the given oneof group. | ||
* | ||
* Not that the recommended way to access a oneof group is to check | ||
* the "oneofKind" property and let TypeScript narrow down the union | ||
* type for you: | ||
* | ||
* ```ts | ||
* if (message.result.oneofKind === "error") { | ||
* message.result.error; // string | ||
* } | ||
* ``` | ||
* | ||
* In the rare case you just need the value, and do not care about | ||
* which protobuf field is selected, you can use this function | ||
* for convenience. | ||
*/ | ||
function getSelectedOneofValue(oneof) { | ||
if (oneof.oneofKind === undefined) { | ||
return undefined; | ||
} | ||
return oneof[oneof.oneofKind]; | ||
} | ||
exports.getSelectedOneofValue = getSelectedOneofValue; |
@@ -34,4 +34,4 @@ // Public API of the protobuf-ts runtime. | ||
export { ReflectionJsonWriter } from './reflection-json-writer'; | ||
// Type guarding at runtime, may be interesting to the user for his reflection ops | ||
export { isOneofGroup, setOneofValue, getOneofValue, clearOneofValue } from './oneof'; | ||
// Oneof helpers | ||
export { isOneofGroup, setOneofValue, getOneofValue, clearOneofValue, getSelectedOneofValue } from './oneof'; | ||
// Enum object type guard and reflection util, may be interesting to the user. | ||
@@ -38,0 +38,0 @@ export { listEnumValues, listEnumNames, listEnumNumbers, isEnumObject } from './enum-object'; |
@@ -44,2 +44,42 @@ /** | ||
/** | ||
* Returns the value of the given field in a oneof group. | ||
*/ | ||
export function getOneofValue(oneof, kind) { | ||
return oneof[kind]; | ||
} | ||
export function setOneofValue(oneof, kind, value) { | ||
if (oneof.oneofKind !== undefined) { | ||
delete oneof[oneof.oneofKind]; | ||
} | ||
oneof.oneofKind = kind; | ||
if (value !== undefined) { | ||
oneof[kind] = value; | ||
} | ||
} | ||
export function setUnknownOneofValue(oneof, kind, value) { | ||
if (oneof.oneofKind !== undefined) { | ||
delete oneof[oneof.oneofKind]; | ||
} | ||
oneof.oneofKind = kind; | ||
if (value !== undefined && kind !== undefined) { | ||
oneof[kind] = value; | ||
} | ||
} | ||
/** | ||
* Removes the selected field in a oneof group. | ||
* | ||
* Note that the recommended way to modify a oneof group is to set | ||
* a new object: | ||
* | ||
* ```ts | ||
* message.result = { oneofKind: undefined }; | ||
* ``` | ||
*/ | ||
export function clearOneofValue(oneof) { | ||
if (oneof.oneofKind !== undefined) { | ||
delete oneof[oneof.oneofKind]; | ||
} | ||
oneof.oneofKind = undefined; | ||
} | ||
/** | ||
* Returns the selected value of the given oneof group. | ||
@@ -61,3 +101,3 @@ * | ||
*/ | ||
export function getOneofValue(oneof) { | ||
export function getSelectedOneofValue(oneof) { | ||
if (oneof.oneofKind === undefined) { | ||
@@ -68,19 +108,1 @@ return undefined; | ||
} | ||
export function setOneofValue(oneof, kind, value) { | ||
if (oneof.oneofKind !== undefined) { | ||
delete oneof[oneof.oneofKind]; | ||
} | ||
oneof.oneofKind = kind; | ||
if (value !== undefined) { | ||
oneof[kind] = value; | ||
} | ||
} | ||
/** | ||
* Sets a oneof group to `{ oneofKind: undefined }` | ||
*/ | ||
export function clearOneofValue(oneof) { | ||
if (oneof.oneofKind !== undefined) { | ||
delete oneof[oneof.oneofKind]; | ||
} | ||
oneof.oneofKind = undefined; | ||
} |
@@ -22,5 +22,5 @@ export { JsonValue, JsonObject, typeofJsonValue, isJsonObject } from './json-typings'; | ||
export { ReflectionJsonWriter } from './reflection-json-writer'; | ||
export { isOneofGroup, setOneofValue, getOneofValue, clearOneofValue } from './oneof'; | ||
export { isOneofGroup, setOneofValue, getOneofValue, clearOneofValue, getSelectedOneofValue } from './oneof'; | ||
export { EnumObjectValue, listEnumValues, listEnumNames, listEnumNumbers, isEnumObject } from './enum-object'; | ||
export { lowerCamelCase } from './lower-camel-case'; | ||
export { assert, assertNever, assertInt32, assertUInt32, assertFloat32 } from './assert'; |
@@ -1,2 +0,2 @@ | ||
import type { UnknownOneofGroup } from "./unknown-types"; | ||
import type { UnknownEnum, UnknownMessage, UnknownOneofGroup, UnknownScalar } from "./unknown-types"; | ||
/** | ||
@@ -31,21 +31,9 @@ * Is the given value a valid oneof group? | ||
/** | ||
* Returns the selected value of the given oneof group. | ||
* | ||
* Not that the recommended way to access a oneof group is to check | ||
* the "oneofKind" property and let TypeScript narrow down the union | ||
* type for you: | ||
* | ||
* ```ts | ||
* if (message.result.oneofKind === "error") { | ||
* message.result.error; // string | ||
* } | ||
* ``` | ||
* | ||
* In the rare case you just need the value, and do not care about | ||
* which protobuf field is selected, you can use this function | ||
* for convenience. | ||
* Returns the value of the given field in a oneof group. | ||
*/ | ||
export declare function getOneofValue<T extends UnknownOneofGroup, V extends string extends keyof T ? UnknownOneofGroup[string] : T extends { | ||
export declare function getOneofValue<T extends UnknownOneofGroup, K extends T extends { | ||
oneofKind: keyof T; | ||
} ? T[T["oneofKind"]] : never>(oneof: T): V | undefined; | ||
} ? T["oneofKind"] : never, V extends T extends { | ||
oneofKind: K; | ||
} ? T[K] : never>(oneof: T, kind: K): V | undefined; | ||
/** | ||
@@ -71,4 +59,37 @@ * Selects the given field in a oneof group. | ||
/** | ||
* Sets a oneof group to `{ oneofKind: undefined }` | ||
* Selects the given field in a oneof group, just like `setOneofValue()`, | ||
* but works with unknown oneof groups. | ||
*/ | ||
export declare function setUnknownOneofValue(oneof: UnknownOneofGroup, kind: string, value: UnknownScalar | UnknownEnum | UnknownMessage): void; | ||
export declare function setUnknownOneofValue(oneof: UnknownOneofGroup, kind: undefined, value?: undefined): void; | ||
/** | ||
* Removes the selected field in a oneof group. | ||
* | ||
* Note that the recommended way to modify a oneof group is to set | ||
* a new object: | ||
* | ||
* ```ts | ||
* message.result = { oneofKind: undefined }; | ||
* ``` | ||
*/ | ||
export declare function clearOneofValue<T extends UnknownOneofGroup>(oneof: T): void; | ||
/** | ||
* Returns the selected value of the given oneof group. | ||
* | ||
* Not that the recommended way to access a oneof group is to check | ||
* the "oneofKind" property and let TypeScript narrow down the union | ||
* type for you: | ||
* | ||
* ```ts | ||
* if (message.result.oneofKind === "error") { | ||
* message.result.error; // string | ||
* } | ||
* ``` | ||
* | ||
* In the rare case you just need the value, and do not care about | ||
* which protobuf field is selected, you can use this function | ||
* for convenience. | ||
*/ | ||
export declare function getSelectedOneofValue<T extends UnknownOneofGroup, V extends string extends keyof T ? UnknownOneofGroup[string] : T extends { | ||
oneofKind: keyof T; | ||
} ? T[T["oneofKind"]] : never>(oneof: T): V | undefined; |
{ | ||
"name": "@protobuf-ts/runtime", | ||
"version": "2.0.0-alpha.28", | ||
"version": "2.0.0-alpha.29", | ||
"description": "Runtime library for code generated by the protoc plugin \"protobuf-ts\"", | ||
@@ -40,3 +40,3 @@ "license": "(Apache-2.0 AND BSD-3-Clause)", | ||
}, | ||
"gitHead": "dfd0e333bc8ce4c2e045d443953ee7dcf7d50cca" | ||
"gitHead": "4b0c1f85efab2be637ba09422c00aff522d07e4f" | ||
} |
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
336465
8574