YAML 具有继承性。我见过的最清晰的例子在这里:http ://blog.101ideas.cz/posts/dry-your-yaml-files.html
我需要更复杂的东西:我需要覆盖对象的对象属性。这是一个例子:
database: &default
server:
ip: 192.168.1.5
port: 2000
db_name: test
user:
name: root
password: root
# database foo differs from default by only its port and user password
foo_database:
<<: *default
server:
port: 2001
db_name: foo
user:
password: foo_root
我想得到这个结果:
foo_database.server.ip -> 192.168.1.5
foo_database.server.port -> 2001
foo_database.db_name -> foo
foo_database.user.name -> root
foo_database.user.password -> foo_root
但是如果你这样声明,你会得到这些属性不正确(根据预期值):
foo_database.server.ip -> will be None
foo_database.user.name -> will be None
因为新的“服务器”对象只有“端口”属性,它会覆盖整个旧的“服务器”对象。
如何获得我想要实现的那种继承?
编辑
这是我在LiveScript中使用工作代码的确切意图:
config =
default:
ip: \192.168.1.5
port: 2000
name: \root
password: \root
db:
name: \default
location: \LA
foo-database:~ -> @default `merge` do
ip: \11.11.11.11
db:
name: \my-foo
bar-database:~ -> @foo-database `merge` do
password: \1234
db:
location: \SF
config.default
# => {"ip":"192.168.1.5","port":2000,"name":"root","password":"root","db":{"name":"default","location":"LA"}}
config.foo-database
# => {"ip":"11.11.11.11","port":2000,"name":"root","password":"root","db":{"name":"my-foo","location":"LA"}}
config.bar-database
# => {"ip":"11.11.11.11","port":2000,"name":"root","password":"1234","db":{"name":"my-foo","location":"SF"}}