现有 protobuf 数据结构

enum Phase {
	Pending = 1;
    Running = 0;
}
message Status {
    Phase status = 1;
}

实际生成代码,对应的类型

type Phase int32
const (
	Phase_Pending    Phase = 0
	Phase_Running    Phase = 1
}

但我们希望解析的数据如下

{
	"status":"Pending"
}
encoding/json
github.com/golang/protobuf/jsonpb
	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	json := jsonpb.Unmarshaler{}
	wf := &workflow.Workflow{}
	err = json.Unmarshal(f, wf)
	if err != nil {
		return nil, err
	}

通过 jsonpb 可以解析 json 到 protobuf 的枚举值
如果希望兼容 json 库,则需要重写对象的 MarshalJSON/UnmarshalJSON 方法,里面使用 jsonpb 包来操作