Debian Codegraph @

node/@bufbuild/protobuf/dist/esm/txtpb/is-group-like.js

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