Scott Baker | 105df15 | 2020-04-13 15:55:14 -0700 | [diff] [blame^] | 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 | |
| 5 | package proto |
| 6 | |
| 7 | import ( |
| 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. |
| 13 | func 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. |
| 20 | func 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. |
| 28 | func 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. |
| 34 | func SetExtension(m Message, ext protoreflect.ExtensionType, value interface{}) { |
| 35 | m.ProtoReflect().Set(ext.TypeDescriptor(), ext.ValueOf(value)) |
| 36 | } |