{"id":207908,"date":"2025-07-08T11:33:03","date_gmt":"2025-07-08T03:33:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207908\/"},"modified":"2025-07-08T11:33:03","modified_gmt":"2025-07-08T03:33:03","slug":"%e5%9c%a8%e7%bb%93%e6%9e%84%e4%bd%93%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%b8%a6%e6%9c%89-go-interface-%e5%ad%97%e6%ae%b5%e7%9a%84-protobuf-%e6%96%87%e4%bb%b6%e4%b8%ad%e7%9a%84-google-protobuf-value","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207908\/","title":{"rendered":"\u5728\u7ed3\u6784\u4f53\u4e2d\u4f7f\u7528\u5e26\u6709 go interface{} \u5b57\u6bb5\u7684 protobuf \u6587\u4ef6\u4e2d\u7684 google.protobuf.Value \uff0c\u53cd\u4e4b\u4ea6\u7136"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5728\u7ed3\u6784\u4f53\u4e2d\u4f7f\u7528\u5e26\u6709 go interface{} \u5b57\u6bb5\u7684 protobuf \u6587\u4ef6\u4e2d\u7684 google.protobuf.Value \uff0c\u53cd\u4e4b\u4ea6\u7136<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 17:18:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u76ee\u524d\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728\u7ed3\u6784\u4f53\u4e2d\u4f7f\u7528\u5e26\u6709 go interface{} \u5b57\u6bb5\u7684 protobuf \u6587\u4ef6\u4e2d\u7684 google.protobuf.Value \uff0c\u53cd\u4e4b\u4ea6\u7136\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709 protobuf \u6d88\u606f\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\/\/ rule : includes an entity , an operation and a comparison value\nmessage rule {\n  string entity = 1;\n  string operator = 2;\n  google.protobuf.value value = 3;\n}<\/pre>\n<p>\u6211\u5728\u8fd9\u91cc\u4f7f\u7528 value \u662f\u56e0\u4e3a value \u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u3002 <\/p>\n<p>\u5728\u6211\u7684 golang \u4ee3\u7801\u4e2d\uff0c\u6211\u4f7f\u7528\u751f\u6210\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>type Rule struct {\n Entity string\n Operator string\n Value interface{}\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u7684\u95ee\u9898\u662f\uff1a \u5982\u4f55\u5c06 kind of google.protobuf.value (nil, number, string, bool, struct, list) \u8868\u793a\u7684\u7c7b\u578b\u8f6c\u6362\u4e3a interface{} \u5e76\u5728\u8fd0\u884c\u65f6\u5c06\u63a5\u53e3\u7684\u52a8\u6001\u7c7b\u578b\u8f6c\u6362\u56de google.protobuf.value \u7c7b\u578b\uff1f<\/p>\n<p>\u6211\u786e\u5b9e\u9700\u8981\u4e00\u4e9b\u5e2e\u52a9\uff0c\u5728\u8fd9\u91cc\u627e\u5230\u4e86\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\uff08https:\/\/go.googlesource.com\/protobuf\/+\/4c057caa84dc5e1bc6bf0d9fe8af9180a6151e07\/internal\/value\/convert.go\uff09\uff0c\u4f46\u5e0c\u671b\u6709\u4e00\u79cd\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u5173\u4e8e\u8fd9\u4e2a\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u4e3a\u4e86\u5b8c\u6574\u6027\u800c\u53d1\u8868\u8bc4\u8bba\uff1a<\/p>\n<pre>func converttovalue(v interface{}, from reflect.type) (*types.value, error) {\n    switch from.kind() {\n    case reflect.string:\n        if reflect.valueof(v).string() == \"true\" || reflect.valueof(v).string() == \"false\" {\n            boolval, err := strconv.parsebool(reflect.valueof(v).string())\n            if err != nil {\n                return nil, err\n            }\n            return &amp;types.value{kind:&amp;types.value_boolvalue{boolvalue:boolval}}, nil\n        }\n        return &amp;types.value{kind:&amp;types.value_stringvalue{stringvalue:reflect.valueof(v).string()}}, nil\n    case reflect.int64:\n    case reflect.float64:\n        return &amp;types.value{kind:&amp;types.value_numbervalue{numbervalue:reflect.valueof(v).float()}}, nil\n    case reflect.slice:\n        list := reflect.valueof(v)\n        outputlist := make([]*types.value,0)\n        for i:=0 ; i &lt; list.len(); i++ {\n            val, err := converttovalue(list.index(i).interface(), reflect.typeof(list.index(i).interface()))\n            if err != nil {\n                return nil, err\n            }\n            outputlist = append(outputlist, val)\n        }\n        return &amp;types.value{kind:&amp;types.value_listvalue{listvalue:&amp;types.listvalue{values:outputlist}}}, nil\n    case reflect.struct:\n        valstruct := reflect.valueof(v)\n        outputmap := make(map[string]*types.value)\n        numoffield := valstruct.numfield()\n        for i := 0; i &lt; numoffield; i++ {\n            val, err := converttovalue(valstruct.field(i).interface(), reflect.typeof(valstruct.field(i).interface()))\n            if err != nil {\n                return nil, err\n            }\n            outputmap[valstruct.field(i).type().name()] = val\n        }\n\n        return &amp;types.value{kind:&amp;types.value_structvalue{structvalue:&amp;types.struct{fields:outputmap}}}, nil\n    default:\n        return nil, nil\n}<\/pre>\n<p>\u548c<\/p>\n<pre>func convertValueToInterface(value types.Value) (interface{}, error) {\n\n    switch {\n    case _ , ok := value.Kind.(*types.Value_NullValue); ok:\n        return nil, nil\n    case x , ok := value.Kind.(*types.Value_StringValue); ok:\n        return x.StringValue, nil\n    case x , ok := value.Kind.(*types.Value_NumberValue); ok:\n        return x.NumberValue, nil\n    case x , ok := value.Kind.(*types.Value_BoolValue); ok:\n        return strconv.FormatBool(x.BoolValue), nil\n    case x , ok := value.Kind.(*types.Value_ListValue); ok:\n        if x == nil || x.ListValue == nil || x.ListValue.Values == nil {\n            return nil, nil\n        }\n        listValue := x.ListValue.Values\n        if len(listValue) == 0 {\n            return nil, nil\n        }\n        val, err := convertValueToInterface(*listValue[0])\n        if err != nil {\n            return nil, err\n        }\n        typ := reflect.TypeOf(val)\n        outputList := reflect.MakeSlice(reflect.SliceOf(typ),0,0)\n        for _, value := range listValue {\n            if value != nil {\n                val, err := convertValueToInterface(*value)\n                if err != nil {\n                    return nil, err\n                }\n                outputList = reflect.Append(outputList, reflect.ValueOf(val))\n            }\n        }\n        return outputList.Interface(), nil\n    case x , ok := value.Kind.(*types.Value_StructValue); ok:\n        if x == nil || x.StructValue == nil || x.StructValue.Fields == nil {\n            return nil, nil\n        }\n        mapValue := x.StructValue.Fields\n        var keyTyp reflect.Type\n        var typ reflect.Type\n        for key,value := range mapValue {\n            if value != nil {\n                val, err := convertValueToInterface(*value)\n                if err != nil {\n                    return nil, err\n                }\n                keyTyp = reflect.TypeOf(key)\n                typ = reflect.TypeOf(val)\n                break\n            } else {\n                return nil, nil\n            }\n        }\n        outputMap := reflect.MakeMap(reflect.MapOf(keyTyp, typ))\n        for key,value := range mapValue {\n            if value != nil {\n                val, err := convertValueToInterface(*value)\n                if err != nil {\n                    return nil, err\n                }\n                outputMap.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(val))\n            }\n        }\n        return outputMap.Interface(), nil\n    default:\n        return nil, nil\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5728\u7ed3\u6784\u4f53\u4e2d\u4f7f\u7528\u5e26\u6709 go interface{} \u5b57\u6bb5\u7684 protobuf \u6587\u4ef6\u4e2d\u7684 google.protobuf.Value \uff0c\u53cd\u4e4b\u4ea6\u7136\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207908","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207908"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207908\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}