我想对从 Golang 模块生成的提示模式中的某些结构字段进行额外的约束。
下面的schema.cue
是从相应的Golang模块生成的
package app #App: { version: string @go(Version) } ...
我尝试在源 Golang 模块中指定提示注释(下面的示例),但这并没有在生成的 schema.cue
中产生正确的约束。
type App struct { Version string `cue:"'version: =~core.myapp.dev/v1'" yaml:"Version"` }
我尝试的另一种方法是使用连词来扩展架构的实例。
CB79515A7EDA1BA1F8290B9FA56F2A98但是,cue vet schema.cue测试/badVersionValue.yaml
,并没有对stdout
产生任何错误,其中badVersionValue.yaml
包含Version:badstring
。
我可以通过以下手动编写的 schema.cue
获得一个有效的解决方案
package app version: =~"core.myapp.dev/v1"
这样 cue vet schema.cuetests/badVersionValue.yaml
就会产生
Version: invalid value "badstring" (out of bound =~"core.myapp.dev/v1"): ...
这种方法的缺点是我会从头开始编写提示模式文件,而不是使用和迭代从 Golang 源代码/事实来源生成的文件。
当涉及 definitions
时,我误解了如何正确使用提示进行审查。
基于来自cuetorials.com的此示例,您可以指定 -d “#Schema”
所在位置
我不仅看到以下成功,其中 yaml 文件内容为 Version: 5
。
cue vet badVersionStr.yaml schema.cue -d '#App' Version: conflicting values 5 and string (mismatched types int and string):
而且还根据OP向现有字段附加约束:
package app #App: { version: string @go(Version) version: =~ "core.myapp.dev/v1" } ...
现在产生预期的输出
cue vet badVersionStr.yaml schema.cue -d '#App' Version: invalid value "badString" (out of bound =~"core.myapp.dev/v1"):
编辑: 对于使用 Golang 模块处理所有事情的人们来说,这是一个更好的方法。您可以在 Golang 模块中将约束作为结构字段注释放置。
# app.go type App struct { Version string `cue:"=~"core.myapp.dev/v1"" yaml:"Version"` ...
使用 cue get go app.go
你会得到一个相应的 app_go_gen.cue
,它应该看起来像这样
#App: { Version: string & =~"core.myapp.dev/v1" @go(Version) ...
这在执行上面引用的 cue vet ...
命令时有效。这意味着您只需要在 Golang 模块中指定约束,然后生成 Cue 架构,该架构会将这些注释解释为结构字段上的约束。