extension.go 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style.
  3. // license that can be found in the LICENSE file.
  4. package proto
  5. import (
  6. "google.golang.org/protobuf/reflect/protoreflect"
  7. )
  8. // HasExtension reports whether an extension field is populated.
  9. func HasExtension(m Message, ext protoreflect.ExtensionType) bool {
  10. return m.ProtoReflect().Has(ext.TypeDescriptor())
  11. }
  12. // ClearExtension clears an extension field such that subsequent
  13. // HasExtension calls return false.
  14. func ClearExtension(m Message, ext protoreflect.ExtensionType) {
  15. m.ProtoReflect().Clear(ext.TypeDescriptor())
  16. }
  17. // GetExtension retrieves the value for an extension field.
  18. //
  19. // If the field is unpopulated, it returns the default value for
  20. // scalars and an immutable, empty value for lists, maps, or messages.
  21. func GetExtension(m Message, ext protoreflect.ExtensionType) interface{} {
  22. return ext.InterfaceOf(m.ProtoReflect().Get(ext.TypeDescriptor()))
  23. }
  24. // SetExtension stores the value of an extension field.
  25. func SetExtension(m Message, ext protoreflect.ExtensionType, value interface{}) {
  26. m.ProtoReflect().Set(ext.TypeDescriptor(), ext.ValueOf(value))
  27. }