MCPcopy
hub / github.com/golang/protobuf / HasExtension

Function HasExtension

proto/extensions.go:45–69  ·  view source on GitHub ↗

HasExtension reports whether the extension field is present in m either as an explicitly populated field or as an unknown field.

(m Message, xt *ExtensionDesc)

Source from the content-addressed store, hash-verified

43// HasExtension reports whether the extension field is present in m
44// either as an explicitly populated field or as an unknown field.
45func HasExtension(m Message, xt *ExtensionDesc) (has bool) {
46 mr := MessageReflect(m)
47 if mr == nil || !mr.IsValid() {
48 return false
49 }
50
51 // Check whether any populated known field matches the field number.
52 xtd := xt.TypeDescriptor()
53 if isValidExtension(mr.Descriptor(), xtd) {
54 has = mr.Has(xtd)
55 } else {
56 mr.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
57 has = int32(fd.Number()) == xt.Field
58 return !has
59 })
60 }
61
62 // Check whether any unknown field matches the field number.
63 for b := mr.GetUnknown(); !has && len(b) > 0; {
64 num, _, n := protowire.ConsumeField(b)
65 has = int32(num) == xt.Field
66 b = b[n:]
67 }
68 return has
69}
70
71// ClearExtension removes the extension field from m
72// either as an explicitly populated field or as an unknown field.

Callers 3

TestNilMessageFunction · 0.92
TestExtensionsRoundTripFunction · 0.92
TestClearAllExtensionsFunction · 0.92

Calls 4

MessageReflectFunction · 0.85
isValidExtensionFunction · 0.85
HasMethod · 0.80
DescriptorMethod · 0.65

Tested by 3

TestNilMessageFunction · 0.74
TestExtensionsRoundTripFunction · 0.74
TestClearAllExtensionsFunction · 0.74