Go 构建命令抛出 - 恐慌:接口转换:接口 {} 是 []uint8,而不是 *validator.FieldValidator

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

在下面的 Go 函数中,当我尝试 运行 构建命令生成 pb.go 文件时出现错误。 恐慌:接口转换:接口 {} 是 []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
}

我正在尝试使用 from https://github.com/mwitkow/go-proto-validators

添加验证

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

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.

所以,这里 validator.E_Field 似乎是“类型不完整”。您可能需要在定义扩展的包上添加依赖项,以便注册它的类型——也许通过使用 import _ "example.com/some/proto" 到 link 将其添加到您的二进制文件中。