api设计风格: 命令式/可链式/配置式
# 命令式api
for context in (gen := merge(({"old"}, {"new"}))):
root = context.root
node = context.node
if context.diffType == "values_changed":
merge.hook_forceImmutable(context.root, context.node)
merge.hook_auto()
else:
# 可链式api
merge.hook_forceImmutable().hook_auto()
# gen.send(NEW) # gen.send(None) 无法区分业务None与默认next()的底层None
# TODO merge内部 有没有办法感知到 外部for循环内执行了任何命令?
# 直接这样也行,走默认配置
merged = return_of(merge(({"old"}, {"new"})))
# 可链式api
merged = (MergeBuilder(old, new)
.immutable(True)
.auto_for(set, "^,new-^,old-^") # 或 .strategy(set, UnionStrategy(...))
.hook("values_changed", hook_forceImmutable)
.hook("default", hook_auto)
.on_path("settings.*", override=True) # path-specific
.execute()) # 或 .merge()
# 声明式&配置式api
merged = merge(
({"old"}, {"new"}),
auto={set: "^,new-^,old-^"},
hook={"values_changed": merge.hook_auto},
)
命令式
最灵活,但也考验库设计者的设计思路。
且使用者需要阅读文档才会使用。
Builder / Fluent / 链式 API
配置式的另类写法,链式执行
配置式
静态配置,在库稳定时,对使用者调试时友好。
但难以拓展灵活的需求

浙公网安备 33010602011771号