Reputation: 828
I have protobuf message which looks like this :
// Rule : Includes an entity , an operation and a comparison value
message Rule {
string entity = 1;
string operator = 2;
google.protobuf.Value value = 3;
}
I am using Value here because value can be of any type.
In my Golang code where I am using the generated code :
type Rule struct {
Entity string
Operator string
Value interface{}
}
Now, My question is : How do I convert type represented by Kind of google.protobuf.Value (nil, number, string, bool, struct, list) to interface{} and convert the dynamic type of interface{} in runtime back to type google.protobuf.Value ?
I can really use some help, found a sorta-solution here ( https://go.googlesource.com/protobuf/+/4c057caa84dc5e1bc6bf0d9fe8af9180a6151e07/internal/value/convert.go ) but was hoping of a simpler way to go about this.
Upvotes: 3
Views: 4894
Reputation: 828
Commenting for the sake of completeness :
func convertToValue(v interface{}, from reflect.Type) (*types.Value, error) {
switch from.Kind() {
case reflect.String:
if reflect.ValueOf(v).String() == "true" || reflect.ValueOf(v).String() == "false" {
boolVal, err := strconv.ParseBool(reflect.ValueOf(v).String())
if err != nil {
return nil, err
}
return &types.Value{Kind:&types.Value_BoolValue{BoolValue:boolVal}}, nil
}
return &types.Value{Kind:&types.Value_StringValue{StringValue:reflect.ValueOf(v).String()}}, nil
case reflect.Int64:
case reflect.Float64:
return &types.Value{Kind:&types.Value_NumberValue{NumberValue:reflect.ValueOf(v).Float()}}, nil
case reflect.Slice:
list := reflect.ValueOf(v)
outputList := make([]*types.Value,0)
for i:=0 ; i < list.Len(); i++ {
val, err := convertToValue(list.Index(i).Interface(), reflect.TypeOf(list.Index(i).Interface()))
if err != nil {
return nil, err
}
outputList = append(outputList, val)
}
return &types.Value{Kind:&types.Value_ListValue{ListValue:&types.ListValue{Values:outputList}}}, nil
case reflect.Struct:
valStruct := reflect.ValueOf(v)
outputMap := make(map[string]*types.Value)
numOfField := valStruct.NumField()
for i := 0; i < numOfField; i++ {
val, err := convertToValue(valStruct.Field(i).Interface(), reflect.TypeOf(valStruct.Field(i).Interface()))
if err != nil {
return nil, err
}
outputMap[valStruct.Field(i).Type().Name()] = val
}
return &types.Value{Kind:&types.Value_StructValue{StructValue:&types.Struct{Fields:outputMap}}}, nil
default:
return nil, nil
}
and
func convertValueToInterface(value types.Value) (interface{}, error) {
switch {
case _ , ok := value.Kind.(*types.Value_NullValue); ok:
return nil, nil
case x , ok := value.Kind.(*types.Value_StringValue); ok:
return x.StringValue, nil
case x , ok := value.Kind.(*types.Value_NumberValue); ok:
return x.NumberValue, nil
case x , ok := value.Kind.(*types.Value_BoolValue); ok:
return strconv.FormatBool(x.BoolValue), nil
case x , ok := value.Kind.(*types.Value_ListValue); ok:
if x == nil || x.ListValue == nil || x.ListValue.Values == nil {
return nil, nil
}
listValue := x.ListValue.Values
if len(listValue) == 0 {
return nil, nil
}
val, err := convertValueToInterface(*listValue[0])
if err != nil {
return nil, err
}
typ := reflect.TypeOf(val)
outputList := reflect.MakeSlice(reflect.SliceOf(typ),0,0)
for _, value := range listValue {
if value != nil {
val, err := convertValueToInterface(*value)
if err != nil {
return nil, err
}
outputList = reflect.Append(outputList, reflect.ValueOf(val))
}
}
return outputList.Interface(), nil
case x , ok := value.Kind.(*types.Value_StructValue); ok:
if x == nil || x.StructValue == nil || x.StructValue.Fields == nil {
return nil, nil
}
mapValue := x.StructValue.Fields
var keyTyp reflect.Type
var typ reflect.Type
for key,value := range mapValue {
if value != nil {
val, err := convertValueToInterface(*value)
if err != nil {
return nil, err
}
keyTyp = reflect.TypeOf(key)
typ = reflect.TypeOf(val)
break
} else {
return nil, nil
}
}
outputMap := reflect.MakeMap(reflect.MapOf(keyTyp, typ))
for key,value := range mapValue {
if value != nil {
val, err := convertValueToInterface(*value)
if err != nil {
return nil, err
}
outputMap.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(val))
}
}
return outputMap.Interface(), nil
default:
return nil, nil
}
Upvotes: 8