blob: 2e1c78f02e89cf35c3f57d03f7f64bf68d070a94 [file] [log] [blame]
Damien Neil92f76182019-08-02 16:58:08 -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.
12func HasExtension(m Message, ext protoreflect.ExtensionType) bool {
13 return m.ProtoReflect().Has(ext.Descriptor())
14}
15
16// ClearExtension clears an extension field such that subsequent
17// HasExtension calls return false.
18func ClearExtension(m Message, ext protoreflect.ExtensionType) {
19 m.ProtoReflect().Clear(ext.Descriptor())
20}
21
22// GetExtension retrieves the value for an extension field.
23//
24// If the field is unpopulated, it returns the default value for
25// scalars and an immutable, empty value for lists, maps, or messages.
26func GetExtension(m Message, ext protoreflect.ExtensionType) interface{} {
27 return ext.InterfaceOf(m.ProtoReflect().Get(ext.Descriptor()))
28}
29
30// SetExtension stores the value of an extension field.
31func SetExtension(m Message, ext protoreflect.ExtensionType, value interface{}) {
32 m.ProtoReflect().Set(ext.Descriptor(), ext.ValueOf(value))
33}