| 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.isGroupLike = isGroupLike; |
| 17 |
/** |
| 18 |
* Returns true if the field is structured like a proto2 group: a delimited |
| 19 |
* message field whose name is the lowercase of its message type name, declared |
| 20 |
* in the same scope as that message. |
| 21 |
* |
| 22 |
* The text format addresses such fields by their message type name (e.g. |
| 23 |
* `MyGroup`) rather than their field name. This is a faithful port of |
| 24 |
* protobuf-go's isGroupLike (internal/filedesc/desc.go), so editions delimited |
| 25 |
* fields are treated exactly like proto2 groups. |
| 26 |
* |
| 27 |
* Testing `field.message` first narrows the DescField union to its three |
| 28 |
* message-bearing variants (singular, list, and map value) — all of which carry |
| 29 |
* `delimitedEncoding` — so it is in scope below without a cast. Maps are |
| 30 |
* excluded automatically, because their `delimitedEncoding` is always false. |
| 31 |
*/ |
| 32 |
function isGroupLike(field) { |
| 33 |
// Groups are always delimited-encoded message fields. |
| 34 |
if (field.message === undefined || !field.delimitedEncoding) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
// Group fields are always named after the lowercase message type name. |
| 38 |
if (field.message.name.toLowerCase() !== field.name) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
// Groups can only be defined in the file they are used in. |
| 42 |
if (field.message.file !== field.parent.file) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
// Group messages are always defined in the same scope as the field. |
| 46 |
return field.message.parent === field.parent; |
| 47 |
} |