| 1 |
import { type DescExtension, type DescField, type DescMessage, type DescOneof } from "../descriptors.js"; |
| 2 |
import type { Registry } from "../registry.js"; |
| 3 |
/** |
| 4 |
* A path to a (nested) member of a Protobuf message, such as a field, oneof, |
| 5 |
* extension, list element, or map entry. |
| 6 |
* |
| 7 |
* Note that we may add additional types to this union in the future to support |
| 8 |
* more use cases. |
| 9 |
*/ |
| 10 |
export type Path = (DescField | DescExtension | DescOneof | { |
| 11 |
kind: "list_sub"; |
| 12 |
index: number; |
| 13 |
} | { |
| 14 |
kind: "map_sub"; |
| 15 |
key: string | number | bigint | boolean; |
| 16 |
})[]; |
| 17 |
/** |
| 18 |
* Builds a Path. |
| 19 |
*/ |
| 20 |
export type PathBuilder = { |
| 21 |
/** |
| 22 |
* The root message of the path. |
| 23 |
*/ |
| 24 |
readonly schema: DescMessage; |
| 25 |
/** |
| 26 |
* Add field access. |
| 27 |
* |
| 28 |
* Throws an InvalidPathError if the field cannot be added to the path. |
| 29 |
*/ |
| 30 |
field(field: DescField): PathBuilder; |
| 31 |
/** |
| 32 |
* Access a oneof. |
| 33 |
* |
| 34 |
* Throws an InvalidPathError if the oneof cannot be added to the path. |
| 35 |
* |
| 36 |
*/ |
| 37 |
oneof(oneof: DescOneof): PathBuilder; |
| 38 |
/** |
| 39 |
* Access an extension. |
| 40 |
* |
| 41 |
* Throws an InvalidPathError if the extension cannot be added to the path. |
| 42 |
*/ |
| 43 |
extension(extension: DescExtension): PathBuilder; |
| 44 |
/** |
| 45 |
* Access a list field by index. |
| 46 |
* |
| 47 |
* Throws an InvalidPathError if the list access cannot be added to the path. |
| 48 |
*/ |
| 49 |
list(index: number): PathBuilder; |
| 50 |
/** |
| 51 |
* Access a map field by key. |
| 52 |
* |
| 53 |
* Throws an InvalidPathError if the map access cannot be added to the path. |
| 54 |
*/ |
| 55 |
map(key: string | number | bigint | boolean): PathBuilder; |
| 56 |
/** |
| 57 |
* Append a path. |
| 58 |
* |
| 59 |
* Throws an InvalidPathError if the path cannot be added. |
| 60 |
*/ |
| 61 |
add(path: Path | PathBuilder): PathBuilder; |
| 62 |
/** |
| 63 |
* Return the path. |
| 64 |
*/ |
| 65 |
toPath(): Path; |
| 66 |
/** |
| 67 |
* Create a copy of this builder. |
| 68 |
*/ |
| 69 |
clone(): PathBuilder; |
| 70 |
/** |
| 71 |
* Get the current container - a list, map, or message. |
| 72 |
*/ |
| 73 |
getLeft(): DescMessage | (DescField & { |
| 74 |
fieldKind: "list"; |
| 75 |
}) | (DescField & { |
| 76 |
fieldKind: "map"; |
| 77 |
}) | undefined; |
| 78 |
}; |
| 79 |
/** |
| 80 |
* Create a PathBuilder. |
| 81 |
*/ |
| 82 |
export declare function buildPath(schema: DescMessage): PathBuilder; |
| 83 |
/** |
| 84 |
* Parse a Path from a string. |
| 85 |
* |
| 86 |
* Throws an InvalidPathError if the path is invalid. |
| 87 |
* |
| 88 |
* Note that a Registry must be provided via the options argument to parse |
| 89 |
* paths that refer to an extension. |
| 90 |
*/ |
| 91 |
export declare function parsePath(schema: DescMessage, path: string, options?: { |
| 92 |
registry?: Registry | undefined; |
| 93 |
}): Path; |
| 94 |
/** |
| 95 |
* Stringify a path. |
| 96 |
*/ |
| 97 |
export declare function pathToString(path: Path): string; |
| 98 |
/** |
| 99 |
* InvalidPathError is thrown for invalid Paths, for example during parsing from |
| 100 |
* a string, or when a new Path is built. |
| 101 |
*/ |
| 102 |
export declare class InvalidPathError extends Error { |
| 103 |
name: string; |
| 104 |
readonly schema: DescMessage; |
| 105 |
readonly path: Path | string; |
| 106 |
constructor(schema: DescMessage, message: string, path: string | Path); |
| 107 |
} |