goenumsprotocol-buffersprotobuf-go

Protobuffer API for dynamic Enum Access


I want to know how to set an Enum value dynamically.

I have the following .proto file:

syntax = "proto3";
package garden;

option go_package = "proto/garden";

message Garden {
  enum Flower {
    Rose = 0;
    Acacia = 1;
    Firethorn = 2;
    Nemophila = 3;
  }
  Flower flower = 1;
}

One way to set the enum is the following:

garden := pb.Garden{}
garden.Flower = pb.Garden_Rose

I want to generate my garden dynamically and only have the value "Rose". There is the value mapping but the following does not work:

garden := pb.Garden{}
garden.Flower = pb.Garden_Flower_value["Rose"] // gives error: cannot use garden.Garden_Flower_value[flower] (type int32) as type garden.Garden_Flower in assignment

garden.Flower = 0 // works somehow??

I assume that I can use the protoreflect package to set the value. Unfortunately, it is not clear to me yet how it works.

garden.Flower = protoreflect.EnumNumber(pb.Garden_Rose)

Furthermore, I want to set ".Flower" dynamically. I figured out how to set fields in a struct dynamically but lack the knowledge how to cast a protoreflect.Value to a reflect.Value type.


Solution

  • Have you already tried this approach?

    flower := "Rose"
    garden := pb.Garden{}
    garden.Flower = pb.Garden_Flower(pb.Garden_Flower_value[flower])
    fmt.Println(garden.Flower.String()) // Rose