【原文】http://leafo.net/lapis/reference/lua_creating_configurations.html

配置事例

Lapis 的配置模块支持递归合并 table

比如我们可以先定义一个基础配置,然后里面的值可以后期覆盖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- config.moon
local config = require("lapis.config")

config({"development", "production"}, {
host = "example.com",
email_enabled = false,
postgres = {
host = "localhost",
port = "5432",
database = "my_app"
}
})

config("production", {
email_enabled = true,
postgres = {
database = "my_app_prod"
}
})

以下是上面的配置的解析出来的结果(默认值省略):

1
2
3
4
5
6
7
8
9
10
11
-- "development"
{
host = "example.com",
email_enabled = false,
postgres = {
host = "localhost",
port = "5432",
database = "my_app",
},
_name = "development"
}
1
2
3
4
5
6
7
8
9
10
11
12
-- "production"

{
host = "example.com",
email_enabled = true,
postgres = {
host = "localhost",
port = "5432",
database = "my_app_prod"
},
_name = "production"
}

你可以通过 config 函数自定义配置你的应用配置,最终这些数据都会和配置的数据合并。