You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

extension.go 1.4 kB

123456789101112131415161718192021222324252627282930313233343536
  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. // It panics if ext does not extend m.
  10. func HasExtension(m Message, ext protoreflect.ExtensionType) bool {
  11. return m.ProtoReflect().Has(ext.TypeDescriptor())
  12. }
  13. // ClearExtension clears an extension field such that subsequent
  14. // HasExtension calls return false.
  15. // It panics if ext does not extend m.
  16. func ClearExtension(m Message, ext protoreflect.ExtensionType) {
  17. m.ProtoReflect().Clear(ext.TypeDescriptor())
  18. }
  19. // GetExtension retrieves the value for an extension field.
  20. // If the field is unpopulated, it returns the default value for
  21. // scalars and an immutable, empty value for lists, maps, or messages.
  22. // It panics if ext does not extend m.
  23. func GetExtension(m Message, ext protoreflect.ExtensionType) interface{} {
  24. return ext.InterfaceOf(m.ProtoReflect().Get(ext.TypeDescriptor()))
  25. }
  26. // SetExtension stores the value of an extension field.
  27. // It panics if ext does not extend m or if value type is invalid for the field.
  28. func SetExtension(m Message, ext protoreflect.ExtensionType, value interface{}) {
  29. m.ProtoReflect().Set(ext.TypeDescriptor(), ext.ValueOf(value))
  30. }