Skip to content

Commit 6aac10a

Browse files
committed
cfg: introduce enum type
Two existing enum types are ported to the new type descriptor. In a next commit it will also be used for a new cfg option about user management. Needed for #435 NO_DOC=internal
1 parent 6a4b2ec commit 6aac10a

File tree

3 files changed

+60
-21
lines changed

3 files changed

+60
-21
lines changed

test/unit-luatest/config_test.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,33 @@ g.test_replica_listen = function()
9191
t.assert_equals(rep_1_a.uri, url1, 'uri value')
9292
t.assert_equals(rep_1_a.listen, {url1, url2}, 'listen value')
9393
end
94+
95+
g.test_enum = function()
96+
local config = {
97+
sharding = {
98+
storage_1_uuid = {
99+
replicas = {}
100+
},
101+
},
102+
}
103+
t.assert(vcfg.check(config), 'normal config')
104+
105+
config.sharding.storage_1_uuid.master = 'auto'
106+
t.assert(vcfg.check(config))
107+
108+
config.sharding.storage_1_uuid.master = 'bad'
109+
t.assert_error_msg_content_equals(
110+
"Master search mode must be enum {'auto', nil}",
111+
vcfg.check, config)
112+
113+
config.sharding.storage_1_uuid.master = nil
114+
for _, v in pairs({'on', 'off', 'once'}) do
115+
config.discovery_mode = v
116+
t.assert(vcfg.check(config))
117+
end
118+
config.discovery_mode = 'bad'
119+
t.assert_error_msg_content_equals(
120+
"Discovery mode must be enum {'on', 'off', 'once', nil}",
121+
vcfg.check, config)
122+
config.discovery_mode = nil
123+
end

test/unit/config.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,14 +642,14 @@ replicaset.master = 'non-auto'
642642
...
643643
util.check_error(lcfg.check, cfg)
644644
---
645-
- Only "auto" master is supported
645+
- Master search mode must be enum {'auto', nil}
646646
...
647647
replicaset.master = 123
648648
---
649649
...
650650
util.check_error(lcfg.check, cfg)
651651
---
652-
- Master search mode must be string
652+
- Master search mode must be enum {'auto', nil}
653653
...
654654
replica.master = true
655655
---

vshard/cfg.lua

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ local function check_replica_master(master, ctx)
3838
end
3939
end
4040

41-
local function check_replicaset_master(master)
42-
if master ~= 'auto' then
43-
error('Only "auto" master is supported')
44-
end
45-
end
46-
4741
local function is_number(v)
4842
return type(v) == 'number' and v == v
4943
end
@@ -70,7 +64,7 @@ end
7064

7165
local type_descriptors = {
7266
-- check(self, template, value)
73-
-- tostring(self)
67+
-- tostring(self, template)
7468

7569
['string'] = {
7670
check = function(self, _, v) return type(v) == 'string' end,
@@ -108,6 +102,27 @@ local type_descriptors = {
108102
check = function(self, _, v) return type(v) == 'table' end,
109103
tostring = type_tostring_trivial,
110104
},
105+
['enum'] = {
106+
check = function(self, tv, v)
107+
for _, vi in pairs(tv.enum) do
108+
if vi == v then
109+
return true
110+
end
111+
end
112+
return false
113+
end,
114+
tostring = function(self, tv)
115+
local values = {}
116+
for _, v in pairs(tv.enum) do
117+
assert(type(v) == 'string')
118+
table.insert(values, ("'%s'"):format(v))
119+
end
120+
if tv.is_optional then
121+
table.insert(values, 'nil')
122+
end
123+
return ('enum {%s}'):format(table.concat(values, ', '))
124+
end,
125+
},
111126
}
112127
for name, td in pairs(type_descriptors) do
113128
td.name = name
@@ -138,7 +153,7 @@ local function validate_config(config, template, check_arg)
138153
if type(expected_type) == 'string' then
139154
local td = type_descriptors[expected_type]
140155
if not td:check(tv, value) then
141-
error(string.format('%s must be %s', name, td:tostring()))
156+
error(string.format('%s must be %s', name, td:tostring(tv)))
142157
end
143158
local max = tv.max
144159
if max and value > max then
@@ -156,7 +171,7 @@ local function validate_config(config, template, check_arg)
156171
if not is_valid_type_found then
157172
local types = {}
158173
for _, t in pairs(expected_type) do
159-
table.insert(types, type_descriptors[t]:tostring())
174+
table.insert(types, type_descriptors[t]:tostring(tv))
160175
end
161176
types = table.concat(types, ', ')
162177
error(string.format('%s must be one of the following '..
@@ -205,8 +220,8 @@ local replicaset_template = {
205220
},
206221
lock = {type = 'boolean', name = 'Lock', is_optional = true},
207222
master = {
208-
type = 'string', name = 'Master search mode', is_optional = true,
209-
check = check_replicaset_master
223+
type = 'enum', name = 'Master search mode', is_optional = true,
224+
enum = {'auto'},
210225
},
211226
}
212227

@@ -239,12 +254,6 @@ local function cfg_check_weights(weights)
239254
end
240255
end
241256

242-
local function check_discovery_mode(value)
243-
if value ~= 'on' and value ~= 'off' and value ~= 'once' then
244-
error("Expected 'on', 'off', or 'once' for discovery_mode")
245-
end
246-
end
247-
248257
local function check_sharding(sharding)
249258
local uuids = {}
250259
local uris = {}
@@ -356,8 +365,8 @@ local cfg_template = {
356365
is_optional = true, default = consts.DEFAULT_FAILOVER_PING_TIMEOUT
357366
},
358367
discovery_mode = {
359-
type = 'string', name = 'Discovery mode: on, off, once',
360-
is_optional = true, default = 'on', check = check_discovery_mode
368+
type = 'enum', name = 'Discovery mode',
369+
is_optional = true, default = 'on', enum = {'on', 'off', 'once'},
361370
},
362371
sched_ref_quota = {
363372
name = 'Scheduler storage ref quota', type = 'non-negative number',

0 commit comments

Comments
 (0)