I am trying to get all the fields names in the go file generated from proto. Below is the generated struct.
type Action struct {
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
// Types that are valid to be assigned to ActionType:
// *Action_TaskAction
ActionType isAction_ActionType `protobuf_oneof:"action_type"`
}
As it is seen that ActionType is oneof Field in proto which is implemented as below.
type isAction_ActionType interface {
isAction_ActionType()
}
type Action_TaskAction struct {
TaskAction *TaskAction `protobuf:"bytes,16,opt,name=task_action,json=taskAction,proto3,oneof"`
}
type TaskAction struct {
Progress float32 `protobuf:"fixed32,1,opt,name=progress,proto3" json:"progress,omitempty"`
}
As I want to get the field name in TaskAction struct which is Progress.
I am using below code to get the field names but facing issue if the field type is interface(for oneof field)
func printFieldNames(t reflect.Type) error {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Type.Kind() == reflect.Struct {
printFieldNames(field.Type)
continue
}
if field.Type.Kind() == reflect.Interface {
// what to do here.
}
column := field.Tag.Get("json")
fmt.Println("column: ", column)
}
return nil
}