| 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 |
export function isWrapper(arg) { |
| 15 |
return isWrapperTypeName(arg.$typeName); |
| 16 |
} |
| 17 |
export function isWrapperDesc(messageDesc) { |
| 18 |
const f = messageDesc.fields[0]; |
| 19 |
return (isWrapperTypeName(messageDesc.typeName) && |
| 20 |
f !== undefined && |
| 21 |
f.fieldKind == "scalar" && |
| 22 |
f.name == "value" && |
| 23 |
f.number == 1); |
| 24 |
} |
| 25 |
/** |
| 26 |
* Returns true if the descriptor is a well-known type with a custom JSON |
| 27 |
* representation per the protobuf JSON spec. Examples: Timestamp as an |
| 28 |
* RFC 3339 string, Duration as "5s", wrappers as the unwrapped scalar. |
| 29 |
* |
| 30 |
* When packed inside `google.protobuf.Any`, these messages are serialized |
| 31 |
* as `{"@type": ..., "value": <custom form>}`; all other messages embed |
| 32 |
* their fields directly. |
| 33 |
*/ |
| 34 |
export function hasCustomJsonRepresentation(desc) { |
| 35 |
switch (desc.typeName) { |
| 36 |
case "google.protobuf.Any": |
| 37 |
case "google.protobuf.Timestamp": |
| 38 |
case "google.protobuf.Duration": |
| 39 |
case "google.protobuf.FieldMask": |
| 40 |
case "google.protobuf.Struct": |
| 41 |
case "google.protobuf.Value": |
| 42 |
case "google.protobuf.ListValue": |
| 43 |
return true; |
| 44 |
default: |
| 45 |
return isWrapperDesc(desc); |
| 46 |
} |
| 47 |
} |
| 48 |
const wrapperTypeNames = /*@__PURE__*/ new Set([ |
| 49 |
"google.protobuf.DoubleValue", |
| 50 |
"google.protobuf.FloatValue", |
| 51 |
"google.protobuf.Int64Value", |
| 52 |
"google.protobuf.UInt64Value", |
| 53 |
"google.protobuf.Int32Value", |
| 54 |
"google.protobuf.UInt32Value", |
| 55 |
"google.protobuf.BoolValue", |
| 56 |
"google.protobuf.StringValue", |
| 57 |
"google.protobuf.BytesValue", |
| 58 |
]); |
| 59 |
function isWrapperTypeName(name) { |
| 60 |
return wrapperTypeNames.has(name); |
| 61 |
} |