【问题标题】:Is it possible to extend definition in CueLang是否可以在 CueLang 中扩展定义
【发布时间】:2021-10-10 13:03:46
【问题描述】:

是否可以扩展定义?

例如,假设我们想要这样的连接定义

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定义,并且无法指定任何未在此定义中指定的变量?

【问题讨论】:

    标签: schema cuelang


    【解决方案1】:

    您可以使用如下定义嵌入

    Connection :: {
        protocol: "tcp" | "udp"
        port: int
        host: string
    }
    
    SecureConnection :: {
        Connection
        username: string & >""
        password: string & >""
    }
    

    【讨论】:

    • 不知道为什么我没有想到这一点。奇迹般有效。谢谢!
    • 有时会发生这种情况,我对 cuelang 也很陌生,刚开始玩它。
    猜你喜欢
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 2018-08-16
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 2016-01-06
    相关资源
    最近更新 更多