我正在测试Goa的应用程序接口。我想使用uuid作为ID数据类型。我在controller.go中修改了以下函数:
// Show runs the show action.
func (c *PersonnelController) Show(ctx *app.ShowPersonnelContext) error {
// v4 UUID validate regex
var validate = `^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[8|9|aA|bB][a-f0-9]{3}-[a-f0-9]{12}$`
uuidValidator := regexp.MustCompile(validate)
if !uuidValidator.Match(ctx.MemberID) {
return ctx.NotFound()
}
// Build the resource using the generated data structure
personnel := app.GoaCrewWorkforce{
MemberID: ctx.MemberID,
FirstName: fmt.Sprintf("First-Name #%s", ctx.MemberID),
LastName: fmt.Sprintf("Last-Name #%s", ctx.MemberID),
}我想要做的是使用Regexp在我的控制器中验证一个v4 uuid,这样它就不会在没有验证的情况下轮询服务器。据我所知,uuid是一个16字节的切片。Regexp有一个Match []字节函数。然而,我似乎不能理解为什么会出现以下错误:
cannot use ctx.MemberID (type uuid.UUID) as type []byte in argument to uuidValidator.Match如何输入assert ctx.MemberID?我认为在这种情况下不可能进行强制转换?任何指导都是值得感谢的。
发布于 2016-06-30 02:56:35
因为16个字节中的大多数都是随机的,所以没有太多需要验证的地方,但是您可以检查第6个字节的前4位作为版本号,或者检查第8个字节的前3位作为变体。
// enforce only V4 uuids
if ctx.MemberID[6] >> 4 != 4 {
log.Fatal("not a V4 UUID")
}
// enforce only RFC4122 type UUIDS
if (ctx.MemberID[8]&0xc0)|0x80 != 0x80 {
log.Fatal("not an RFC4122 UUID")
}https://stackoverflow.com/questions/38107565
复制相似问题