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)
| 43 | // HasExtension reports whether the extension field is present in m |
| 44 | // either as an explicitly populated field or as an unknown field. |
| 45 | func 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. |