如何正确确定变量 google.protobuf.Any 的类型?

huangapple go评论93阅读模式
英文:

How to correctly determine the type of variable google.protobuf.Any?

问题

请告诉我这段代码中有什么问题,为什么变量的类型无法确定,传递的url类型是google.protobuf.Any?

package main

import (
	"fmt"

	"google.golang.org/protobuf/reflect/protoreflect"
	"google.golang.org/protobuf/reflect/protoregistry"
)

func main() {
	var res protoregistry.MessageTypeResolver = protoregistry.GlobalTypes
	typeUrl := "type.googleapis.com/google.protobuf.StringValue"
	fmt.Println(protoreflect.FullName(typeUrl))
	msgType, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName("google.protobuf.StringValue"))
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(msgType)

	msgType, err = res.FindMessageByURL(typeUrl)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(msgType)
}

proto: not found

proto: not found

英文:

Please tell me what is wrong in this code, why the type is not determined for the variable, for the transmitted url type google.protobuf.Any?

package main

import (
	"fmt"

	"google.golang.org/protobuf/reflect/protoreflect"
	"google.golang.org/protobuf/reflect/protoregistry"
)

func main() {
	var res protoregistry.MessageTypeResolver = protoregistry.GlobalTypes
	typeUrl := "type.googleapis.com/google.protobuf.StringValue"
	fmt.Println(protoreflect.FullName(typeUrl))
	msgType, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName("google.protobuf.StringValue"))
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(msgType)

	msgType, err = res.FindMessageByURL(typeUrl)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(msgType)
}

proto: not found
<nil>
proto: not found
<nil>

答案1

得分: 1

错误是protoregistry.NotFound,因为在protoregistry.GlobalTypes中没有注册消息类型。

要注册google.protobuf.Any,您应该导入包google.golang.org/protobuf/types/known/anypb。并且google.protobuf.Any是由该包的init函数注册的(请参阅源代码)。

要注册google.protobuf.StringValue,您应该导入包google.golang.org/protobuf/types/known/wrapperspb

这是更新后的演示代码,用于查找google.protobuf.StringValue的消息字节:

package main

import (
	"fmt"

	"google.golang.org/protobuf/reflect/protoregistry"
    // 仅导入该包以产生副作用(初始化)。
    // 请参阅https://go.dev/ref/spec#Import_declarations。
	_ "google.golang.org/protobuf/types/known/anypb"
	_ "google.golang.org/protobuf/types/known/wrapperspb"
)

func main() {
	res := protoregistry.GlobalTypes
	fmt.Println("NumMessages:", res.NumMessages())

	msgType, err := protoregistry.GlobalTypes.FindMessageByName("google.protobuf.StringValue")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(msgType)

	msgType, err = res.FindMessageByURL("type.googleapis.com/google.protobuf.StringValue")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(msgType)
}
英文:

The error is protoregistry.NotFound because there are not message types got registered in protoregistry.GlobalTypes.

To register google.protobuf.Any, you should import the package google.golang.org/protobuf/types/known/anypb. And google.protobuf.Any is registered by the init function of that package (see the source code).

To register google.protobuf.StringValue, you should import the package google.golang.org/protobuf/types/known/wrapperspb.

Here is the updated demo that finds the message byte of google.protobuf.StringValue:

package main

import (
	&quot;fmt&quot;

	&quot;google.golang.org/protobuf/reflect/protoregistry&quot;
    // importing the package solely for its side-effects (initialization).
    // See https://go.dev/ref/spec#Import_declarations.
	_ &quot;google.golang.org/protobuf/types/known/anypb&quot;
	_ &quot;google.golang.org/protobuf/types/known/wrapperspb&quot;
)

func main() {
	res := protoregistry.GlobalTypes
	fmt.Println(&quot;NumMessages:&quot;, res.NumMessages())

	msgType, err := protoregistry.GlobalTypes.FindMessageByName(&quot;google.protobuf.StringValue&quot;)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(msgType)

	msgType, err = res.FindMessageByURL(&quot;type.googleapis.com/google.protobuf.StringValue&quot;)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(msgType)
}

huangapple
  • 本文由 发表于 2023年3月24日 01:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75826138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定