schema - 是否可以在 CueLang 中扩展定义
问题描述
是否可以扩展定义?
例如,假设我们想要这样的连接定义
Connection :: {
protocol: "tcp" | "udp"
port: int
host: string
}
我们也希望SecureConnection
拥有一切Connection
,但我们也喜欢添加用户名和密码。如何做到这一点?
我可以这样做
Connection :: {
protocol: "tcp" | "udp"
port: int
host: string
...
}
SecureConnection :: Connection & {
username: string & >""
password: string & >""
}
它会起作用,但这也意味着它没有关闭。由于Connection
定义中的三个点,我们可以添加任何我们想要保护连接的内容
例如
tcp: SecureConnection & {
protocol: "tcp"
port: 8080
host: "localhost"
username: "guest"
password: "guest"
test: "testing"
oneUnimportantVariable: "I am not important"
}
当我运行cue export myfile.cue
这将给我以下 JSON
{
"tcp": {
"protocol": "tcp",
"port": 8080,
"host": "localhost",
"username": "guest",
"password": "guest",
"test": "testing",
"oneUnimportantVariable": "I am not important"
}
}
那么,如何扩展Connection
定义和创建SecureConnection
定义,并且不能指定任何未在此定义中指定的变量呢?
解决方案
您可以使用如下定义嵌入
Connection :: {
protocol: "tcp" | "udp"
port: int
host: string
}
SecureConnection :: {
Connection
username: string & >""
password: string & >""
}
推荐阅读
- javascript - Angular 4 dblclick 事件在移动视图中不起作用
- android - 如何从带有房间的外部数据库中读取不断更新的值?
- mysql - 在数据库集群前使用 proxysql 时是否需要复制用户凭据?
- javascript - vuex-persistedstate:如何在 vuex-persistedstate 中使用 getter?
- java - 我用于确定适当字体大小的 while 循环太过分了……有时?
- email - 更改@记录会破坏电子邮件?
- apache-kafka - Ksql, GROUP BY 返回 ServerError:java.lang.NullPointerException
- cordova-plugins - 无法在 Visual Studio 2017 中安装 Apache Cordova 插件
- python - 宏程序不工作
- amazon-web-services - AWS Cognito /aws-amplify 错误 - 加密未定义