summaryrefslogtreecommitdiffstats
path: root/acf2
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-02-20 18:28:29 +0200
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-02-20 19:28:52 +0200
commita560b76c7b46f067637a79b01f923d377339a5c3 (patch)
treeffe84d72c235ec92e28ad492bdcb92c2ec13111a /acf2
parent4cd12d27f2ddf5c5a65f43d544f3cc126b5d1d98 (diff)
downloadacf2-a560b76c7b46f067637a79b01f923d377339a5c3.tar.bz2
acf2-a560b76c7b46f067637a79b01f923d377339a5c3.tar.xz
model: allow different representation of choices in MF and PM
Diffstat (limited to 'acf2')
-rw-r--r--acf2/model/field.lua53
-rw-r--r--acf2/model/init.lua17
-rw-r--r--acf2/modules/network.lua21
3 files changed, 60 insertions, 31 deletions
diff --git a/acf2/model/field.lua b/acf2/model/field.lua
index dbd98eb..69f36c6 100644
--- a/acf2/model/field.lua
+++ b/acf2/model/field.lua
@@ -87,7 +87,11 @@ function M.Field:init(params)
end
return setdefaults(
choice,
- {enabled=true, ['ui-value']=self:auto_ui_name(choice.value)}
+ {
+ be_value=choice.value,
+ enabled=true,
+ ['ui-value']=self:auto_ui_name(choice.value)
+ }
)
end,
self.choice
@@ -117,6 +121,7 @@ function M.Field:_choice(context) return self.choice end
function M.Field:meta(context)
assert(self.dtype)
+ local choice = self:_choice(context)
return util.update(
super(self, M.Field):meta(context),
{
@@ -126,7 +131,14 @@ function M.Field:meta(context)
condition=self.condition,
required=self.required,
default=self.default,
- choice=self:_choice(context),
+ choice=choice and map(
+ function(ch)
+ ch = util.copy(ch)
+ ch.be_value = nil
+ return ch
+ end,
+ choice
+ ),
widget=self.widget
}
)
@@ -158,25 +170,25 @@ function M.Field:_validate(context, value)
return
end
- local save
- value, save = self:normalize(context, value)
+ value = self:normalize(context, value)
local committing = context.txn:committing()
local choice = self:_choice(context)
- if choice and not util.contains(
- map(
- function(ch) return ch.value end,
- util.filter(function(ch) return committing or ch.enabled end, choice)
- ),
- value
- ) then
- raise(context.path, 'Invalid value')
+ local be_value
+ if choice then
+ for _, ch in ipairs(choice) do
+ if ch.value == value and (committing or ch.enabled) then
+ be_value = ch.be_value
+ break
+ end
+ end
+ if be_value == nil then raise(context.path, 'Invalid value') end
end
self:validate(context, value)
- if save == nil then save = value end
- return save
+ if choice then return be_value end
+ return value
end
function M.Field:check_editable(context)
@@ -208,6 +220,19 @@ end
local Primitive = class(M.Field)
+function Primitive:_load(context)
+ local value = super(self, Primitive):_load(context)
+ if value == nil then return end
+
+ local choice = self:_choice(context)
+ if not choice then return value end
+
+ for _, ch in ipairs(choice) do
+ if ch.be_value == value then return ch.value end
+ end
+ assert(false)
+end
+
function Primitive:validate(context, value)
local t = self.dtype
if type(value) ~= t then raise(context.path, 'Not a '..t) end
diff --git a/acf2/model/init.lua b/acf2/model/init.lua
index 9383ad9..b614c6b 100644
--- a/acf2/model/init.lua
+++ b/acf2/model/init.lua
@@ -105,16 +105,18 @@ function M.Reference:_choice(context)
if M.path.is_subordinate(context.path, ch.ref) then ch = nil end
if ch then
ch['ui-value'] = M.path.name(ch.ref)
- ch.value = self.dereference and ch.ref or M.path.escape(
- ch['ui-value']
- )
+ ch.be_value = M.path.escape(ch['ui-value'])
+ ch.value = self.dereference and ch.ref or ch.be_value
if self.filter then
assert(isinstance(v, model.Model))
if not node.match(v, self.filter) then ch.enabled = false end
end
end
- else update(ch, {value=M.path.escape(v), ['ui-value']=v}) end
+ else
+ local ep = M.path.escape(v)
+ update(ch, {be_value=ep, value=ep, ['ui-value']=v})
+ end
if ch then table.insert(res, ch) end
end
@@ -159,15 +161,10 @@ function M.Reference:normalize(context, value)
rel = rel:sub(prefix:len() + 1, -1)
end
- -- assume one-level ref for now
- if #M.path.split(rel) > 1 then
- raise(path, 'Subtree references not yet supported')
- end
-
-- TODO check instance type
relabel(path, self.follow, self, context, rel)
- return self.dereference and value or rel, rel
+ return self.dereference and value or rel
end
function M.Reference:deleted(context, addr)
diff --git a/acf2/modules/network.lua b/acf2/modules/network.lua
index 32b9d20..186da13 100644
--- a/acf2/modules/network.lua
+++ b/acf2/modules/network.lua
@@ -46,8 +46,13 @@ M.defer(iface_aug_addr)
local IPv4 = M.new()
IPv4.method = M.String{
required=true,
- default='manual',
- choice={{'dhcp', 'DHCP'}, {'loopback', enabled=false}, 'manual', 'static'}
+ choice={
+ {'dhcp', 'DHCP'},
+ {'loopback', enabled=false},
+ {'unconfigured', be_value='manual'},
+ 'static'
+ },
+ default='unconfigured'
}
IPv4.address = M.net.IPv4Address{
condition={method='static'}, required=true, cidr=true, mask_addr='netmask'
@@ -58,8 +63,10 @@ IPv4.gateway = M.net.IPv4Address{condition={method='static'}}
local IPv6 = M.new()
IPv6.method = M.String{
required=true,
- choice={{'loopback', enabled=false}, 'manual', 'static'},
- default='manual'
+ choice={
+ {'loopback', enabled=false}, {'unconfigured', be_value='manual'}, 'static'
+ },
+ default='unconfigured'
}
IPv6.address = M.net.IPv6Address{
condition={method='static'}, required=true, cidr=true, mask_addr='netmask'
@@ -74,7 +81,7 @@ function Interface:validate()
if self.status == 'attached' then
for _, version in ipairs{4, 6} do
self['ipv'..version].method =
- self.class == 'loopback' and 'loopback' or 'manual'
+ self.class == 'loopback' and 'loopback' or 'unconfigured'
end
end
end
@@ -158,7 +165,7 @@ Interface.status = M.String{
end
for _, version in ipairs{4, 6} do
- if obj['ipv'..version].method ~= 'manual' then
+ if obj['ipv'..version].method ~= 'unconfigured' then
return 'configured'
end
end
@@ -262,7 +269,7 @@ return function(txn)
if ifaces[name].class == 'logical' then ifaces[name] = nil
else
for _, version in ipairs{4, 6} do
- ifaces[name]['ipv'..version].method = 'manual'
+ ifaces[name]['ipv'..version].method = 'unconfigured'
end
end
end