blob: 73f2431f1a23e9ba2c2cdaec581caae8e189a673 [file] [log] [blame]
Scott Baker105df152020-04-13 15:55:14 -07001// 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
5package proto
6
7import (
8 "google.golang.org/protobuf/reflect/protoreflect"
9)
10
11// HasExtension reports whether an extension field is populated.
12// It panics if ext does not extend m.
13func HasExtension(m Message, ext protoreflect.ExtensionType) bool {
14 return m.ProtoReflect().Has(ext.TypeDescriptor())
15}
16
17// ClearExtension clears an extension field such that subsequent
18// HasExtension calls return false.
19// It panics if ext does not extend m.
20func ClearExtension(m Message, ext protoreflect.ExtensionType) {
21 m.ProtoReflect().Clear(ext.TypeDescriptor())
22}
23
24// GetExtension retrieves the value for an extension field.
25// If the field is unpopulated, it returns the default value for
26// scalars and an immutable, empty value for lists, maps, or messages.
27// It panics if ext does not extend m.
28func GetExtension(m Message, ext protoreflect.ExtensionType) interface{} {
29 return ext.InterfaceOf(m.ProtoReflect().Get(ext.TypeDescriptor()))
30}
31
32// SetExtension stores the value of an extension field.
33// It panics if ext does not extend m or if value type is invalid for the field.
34func SetExtension(m Message, ext protoreflect.ExtensionType, value interface{}) {
35 m.ProtoReflect().Set(ext.TypeDescriptor(), ext.ValueOf(value))
36}