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