Go build command throws – panic: interface conversion: interface {} is []uint8, not *validator.FieldValidator

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

Go build command throws - panic: interface conversion: interface {} is []uint8, not *validator.FieldValidator

问题

在下面的Go函数中,当我尝试运行构建命令以生成pb.go文件时,出现错误。
panic: 接口转换: interface {} 是 []uint8,而不是 *validator.FieldValidator
github.com/mygithub/myproject/plugin.getFieldValidatorIfAny(0xc0001d4b60, 0x5b5020)

有什么建议可以解决这个问题吗?

func getFieldValidatorIfAny(field *descriptor.FieldDescriptorProto) *validator.FieldValidator {
	if field.Options != nil {
		v, err := proto.GetExtension(field.Options, validator.E_Field)
		if err == nil && v.(*validator.FieldValidator) != nil {
			return (v.(*validator.FieldValidator))
		}
	}
	return nil
}

我正在尝试使用 https://github.com/mwitkow/go-proto-validators 添加验证。

英文:

In below Go function I am getting error when I tried to run build command to generate pb.go file.
panic: interface conversion: interface {} is []uint8, not *validator.FieldValidator
github.com/mygithub/myproject/plugin.getFieldValidatorIfAny(0xc0001d4b60, 0x5b5020)

Any suggestion on how to resolve this

func getFieldValidatorIfAny(field *descriptor.FieldDescriptorProto) *validator.FieldValidator {
	if field.Options != nil {
		v, err := proto.GetExtension(field.Options, validator.E_Field)
		if err == nil && v.(*validator.FieldValidator) != nil {
			return (v.(*validator.FieldValidator))
		}
	}
	return nil
}

I am trying to add validation using from https://github.com/mwitkow/go-proto-validators

答案1

得分: 1

根据 https://beta.pkg.go.dev/github.com/golang/protobuf/proto#GetExtension(我强调):

如果描述符是完整类型(即ExtensionDesc.ExtensionType非nil),则GetExtension解析编码字段并返回指定类型的Go值。如果字段不存在,则返回默认值(如果指定了默认值),否则报告ErrMissingExtension。

如果描述符是不完整类型(即ExtensionDesc.ExtensionType为nil),则GetExtension返回扩展字段的原始编码字节。

因此,这里看起来validator.E_Field是“不完整类型”。您可能需要添加对定义扩展的包的依赖,以便其类型将被注册 - 可能通过使用import _ "example.com/some/proto"将其链接到您的二进制文件中。

英文:

Per https://beta.pkg.go.dev/github.com/golang/protobuf/proto#GetExtension (emphasis mine):
> If the descriptor is type complete (i.e., ExtensionDesc.ExtensionType is non-nil), then GetExtension parses the encoded field and returns a Go value of the specified type. If the field is not present, then the default value is returned (if one is specified), otherwise ErrMissingExtension is reported.
>
> If the descriptor is type incomplete (i.e., ExtensionDesc.ExtensionType is nil), then GetExtension returns the raw encoded bytes for the extension field.

So, here it appears that validator.E_Field is “type incomplete”. You may need to add a dependency on the package that defines the extension so that its type will be registered — perhaps by using import _ "example.com/some/proto" to link it in to your binary.

huangapple
  • 本文由 发表于 2021年8月27日 21:46:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/68954507.html
匿名

发表评论

匿名网友

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

确定