reflection.go 480 B

12345678910111213141516171819202122232425262728
  1. package shared
  2. import (
  3. "reflect"
  4. "google.golang.org/protobuf/proto"
  5. )
  6. func extractVal(r proto.Message) reflect.Value {
  7. val := reflect.ValueOf(r)
  8. if val.Kind() == reflect.Interface {
  9. val = val.Elem()
  10. }
  11. if val.Kind() == reflect.Ptr {
  12. val = val.Elem()
  13. }
  14. return val
  15. }
  16. // Has returns true if the provided Message has the a field fld.
  17. func Has(msg proto.Message, fld string) bool {
  18. val := extractVal(msg)
  19. return val.IsValid() &&
  20. val.FieldByName(fld).IsValid()
  21. }