blob: 809c12def6f328a1bcaab961e9f6fe48636b0a84 [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 {
Damien Neil79bfdbe2019-08-28 11:08:22 -070013 return m.ProtoReflect().Has(ext.TypeDescriptor())
Damien Neil92f76182019-08-02 16:58:08 -070014}
15
16// ClearExtension clears an extension field such that subsequent
17// HasExtension calls return false.
18func ClearExtension(m Message, ext protoreflect.ExtensionType) {
Damien Neil79bfdbe2019-08-28 11:08:22 -070019 m.ProtoReflect().Clear(ext.TypeDescriptor())
Damien Neil92f76182019-08-02 16:58:08 -070020}
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{} {
Damien Neil79bfdbe2019-08-28 11:08:22 -070027 return ext.InterfaceOf(m.ProtoReflect().Get(ext.TypeDescriptor()))
Damien Neil92f76182019-08-02 16:58:08 -070028}
29
30// SetExtension stores the value of an extension field.
31func SetExtension(m Message, ext protoreflect.ExtensionType, value interface{}) {
Damien Neil79bfdbe2019-08-28 11:08:22 -070032 m.ProtoReflect().Set(ext.TypeDescriptor(), ext.ValueOf(value))
Damien Neil92f76182019-08-02 16:58:08 -070033}