54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
import { Message } from "../message.js";
|
|
import { ScalarType } from "../scalar.js";
|
|
import { isMessage } from "../is-message.js";
|
|
/**
|
|
* Wrap a primitive message field value in its corresponding wrapper
|
|
* message. This function is idempotent.
|
|
*/
|
|
export function wrapField(type, value) {
|
|
if (isMessage(value) || !type.fieldWrapper) {
|
|
return value;
|
|
}
|
|
return type.fieldWrapper.wrapField(value);
|
|
}
|
|
/**
|
|
* If the given field uses one of the well-known wrapper types, return
|
|
* the primitive type it wraps.
|
|
*/
|
|
export function getUnwrappedFieldType(field) {
|
|
if (field.fieldKind !== "message") {
|
|
return undefined;
|
|
}
|
|
if (field.repeated) {
|
|
return undefined;
|
|
}
|
|
if (field.oneof != undefined) {
|
|
return undefined;
|
|
}
|
|
return wktWrapperToScalarType[field.message.typeName];
|
|
}
|
|
const wktWrapperToScalarType = {
|
|
"google.protobuf.DoubleValue": ScalarType.DOUBLE,
|
|
"google.protobuf.FloatValue": ScalarType.FLOAT,
|
|
"google.protobuf.Int64Value": ScalarType.INT64,
|
|
"google.protobuf.UInt64Value": ScalarType.UINT64,
|
|
"google.protobuf.Int32Value": ScalarType.INT32,
|
|
"google.protobuf.UInt32Value": ScalarType.UINT32,
|
|
"google.protobuf.BoolValue": ScalarType.BOOL,
|
|
"google.protobuf.StringValue": ScalarType.STRING,
|
|
"google.protobuf.BytesValue": ScalarType.BYTES,
|
|
};
|