0

How can I test if an element of a structure is of type Any (protobuf): *any.Any, in Go? I want to go through each element of a structure, and do a switch case depending on the type of the element. Here is the field descriptor of the message:

FieldDescriptor{Syntax: proto3, FullName:, Number: 3, Cardinality: optional, Kind: message, HasJSONName: true, JSONName:, HasPresence: true, Oneof:, Message: google.protobuf.Any}

Here is my code:

func doSomething(src protoreflect.Message) {
    src.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
            switch {
            case fd.IsList():
                // do something
            case fd.IsMap():
                // do something
            case fd.Message() != nil:
                // do something
            case fd.Kind() == protoreflect.BytesKind:
                // do something
            case *test if message is Any* :
                // do something
            default:
                // do something
            }
        return true
        })
}

I would like a more correct way than for ex:

if fd.Message() != nil{
     fd.Message().FullName() == "google.protobuf.Any"
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

0

You can have a switch case to type check.

 switch v := fd.(type) { 
    default:
        fmt.Printf("unexpected type %T", v)
    case string:
        //do something
    } 
Surya
  • 2,429
  • 1
  • 21
  • 42