gpt4 book ai didi

Clojure 规范 - 命名实体关键字

转载 作者:行者123 更新时间:2023-12-05 08:07:48 29 4
gpt4 key购买 nike

使用带有不存在的命名空间的命名空间限定关键字来定义规范是否被认为是不好的做法?我想在公共(public) domain 命名空间中定义实体映射...所以为了避免在合并规范时丢失数据,我使用约定 :entity/attribute 而不是::entity-attribute 用于属性,标准 ::entity 用于实体。它更好地对齐数据库表和列。单独命名空间中的每个实体让我想起了 Java 类,这听起来不是个好主意。

(s/def :country/id   ::nilable-nat-int)
(s/def :country/name ::non-empty-string)

(s/def ::country
(s/keys :req [:country/id
:country/name]))

;; ----------------------------------------

(s/def :location/id ::nilable-nat-int)
(s/def :location/name ::non-empty-string)
(s/def :location/zipcode ::nilable-non-empty-string)

(s/def ::location
(s/merge
(s/keys :req [:location/id
:location/name
:location/zipcode])
(s/or :country ::country
:country-id
(s/keys :req [:country/id]))))

最佳答案

作为@glts评论,这是正确的答案:mailing list .

我决定使关键字更具体,将其添加到域命名空间:

(doseq [ns ["entity-1" ,,, "entity-n"]]
(->> (str "project.domain." ns)
(symbol)
(create-ns)
(alias (symbol ns))))

然后 ::entity-n/attribute 计算为 :project.domain.entity-n/attribute

问题示例中的属性只需要一个额外的::

(s/def ::location/id ::nilable-nat-int)

关于Clojure 规范 - 命名实体关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53279505/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com