Debian Codegraph @

node/@bufbuild/protobuf/dist/commonjs/reflect/message.js

raw
1 "use strict";
2 // Copyright 2021-2026 Buf Technologies, Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 Object.defineProperty(exports, "__esModule", { value: true });
16 exports.localMessageMapper = localMessageMapper;
17 exports.wktStructToReflect = wktStructToReflect;
18 exports.wktStructToLocal = wktStructToLocal;
19 const create_js_1 = require("../create.js");
20 const guard_js_1 = require("./guard.js");
21 const wrappers_js_1 = require("../wkt/wrappers.js");
22 // google.protobuf.NullValue.NULL_VALUE;
23 const NULL_VALUE = 0;
24 /**
25 * Return the conversions between the local representation of the field
26 * value and the message it represents.
27 *
28 * @private
29 */
30 function localMessageMapper(field) {
31 // google.protobuf.Struct fields are stored as JsonObject.
32 if (usesJsonRepresentation(field)) {
33 return {
34 toMessage: (local) => wktStructToReflect(local),
35 toLocal: (message) => wktStructToLocal(message),
36 };
37 }
38 // Singular wrapper fields outside a oneof are unwrapped to the scalar value.
39 if (field.fieldKind == "message" &&
40 !field.oneof &&
41 (0, wrappers_js_1.isWrapperDesc)(field.message)) {
42 const wrapperDesc = field.message;
43 const valueLocalName = wrapperDesc.fields[0].localName;
44 return {
45 toMessage: (local) => {
46 const message = (0, create_js_1.create)(wrapperDesc);
47 if (local !== undefined) {
48 message[valueLocalName] = local;
49 }
50 return message;
51 },
52 toLocal: (message) => message[valueLocalName],
53 };
54 }
55 // For all other fields, the local value is the message itself.
56 const childDesc = field.message;
57 return {
58 toMessage: (local) => (local === undefined ? (0, create_js_1.create)(childDesc) : local),
59 toLocal: (message) => message,
60 };
61 }
62 /**
63 * Returns true if values of this field are stored as JsonValue instead of
64 * a message: google.protobuf.Struct is represented with JsonObject when
65 * used in a field, except when used in google.protobuf.Value.
66 */
67 function usesJsonRepresentation(field) {
68 return (field.message.typeName == "google.protobuf.Struct" &&
69 field.parent.typeName != "google.protobuf.Value");
70 }
71 /**
72 * Convert the JsonValue representation of a google.protobuf.Struct to the
73 * message representation.
74 *
75 * @private
76 */
77 function wktStructToReflect(json) {
78 const struct = {
79 $typeName: "google.protobuf.Struct",
80 fields: {},
81 };
82 if ((0, guard_js_1.isObject)(json)) {
83 for (const k of Object.keys(json)) {
84 struct.fields[k] = wktValueToReflect(json[k]);
85 }
86 }
87 return struct;
88 }
89 /**
90 * Convert a google.protobuf.Struct message to its JsonValue representation.
91 *
92 * @private
93 */
94 function wktStructToLocal(val) {
95 const json = {};
96 for (const k of Object.keys(val.fields)) {
97 json[k] = wktValueToLocal(val.fields[k]);
98 }
99 return json;
100 }
101 function wktValueToLocal(val) {
102 switch (val.kind.case) {
103 case "structValue":
104 return wktStructToLocal(val.kind.value);
105 case "listValue":
106 return val.kind.value.values.map(wktValueToLocal);
107 case "nullValue":
108 case undefined:
109 return null;
110 default:
111 return val.kind.value;
112 }
113 }
114 function wktValueToReflect(json) {
115 const value = {
116 $typeName: "google.protobuf.Value",
117 kind: { case: undefined },
118 };
119 switch (typeof json) {
120 case "number":
121 value.kind = { case: "numberValue", value: json };
122 break;
123 case "string":
124 value.kind = { case: "stringValue", value: json };
125 break;
126 case "boolean":
127 value.kind = { case: "boolValue", value: json };
128 break;
129 case "object":
130 if (json === null) {
131 value.kind = { case: "nullValue", value: NULL_VALUE };
132 }
133 else if (Array.isArray(json)) {
134 const listValue = {
135 $typeName: "google.protobuf.ListValue",
136 values: [],
137 };
138 if (Array.isArray(json)) {
139 for (const e of json) {
140 listValue.values.push(wktValueToReflect(e));
141 }
142 }
143 value.kind = {
144 case: "listValue",
145 value: listValue,
146 };
147 }
148 else {
149 value.kind = {
150 case: "structValue",
151 value: wktStructToReflect(json),
152 };
153 }
154 break;
155 }
156 return value;
157 }