Skip to content

Commit

Permalink
lib.yang.data: rework 19072b4 (remove order parameter) add tests
Browse files Browse the repository at this point in the history
This removes the order parameter in the affected functions altogether. Test
suite passes so I am assuming this was dead code from the past.
  • Loading branch information
eugeneia committed Dec 12, 2019
1 parent 05b495e commit e96ae1a
Showing 1 changed file with 36 additions and 46 deletions.
82 changes: 36 additions & 46 deletions src/lib/yang/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ function xpath_printer_from_grammar(production, print_default, root)
print_yang_string(k, file)
file:write(' ')
end
local function body_printer(productions, order)
local function body_printer(productions)
-- Iterate over productions trying to translate to other statements. This
-- is used for example in choice statements raising the lower statements
-- in case blocks up to the level of the choice, in place of the choice.
Expand All @@ -880,17 +880,14 @@ function xpath_printer_from_grammar(production, print_default, root)
if translator ~= nil then
local statements = translator(keyword, production)
for k,v in pairs(statements) do translated[k] = v end
order = nil
else
translated[keyword] = production
end
end
productions = translated
if not order then
order = {}
for k,_ in pairs(productions) do table.insert(order, k) end
table.sort(order)
end
local order = {}
for k,_ in pairs(productions) do table.insert(order, k) end
table.sort(order)
local printers = {}
for keyword,production in pairs(productions) do
local printer = printer(keyword, production, printers)
Expand All @@ -905,8 +902,8 @@ function xpath_printer_from_grammar(production, print_default, root)
end
end
end
local function key_composer (productions, order)
local printer = body_printer(productions, order)
local function key_composer (productions)
local printer = body_printer(productions)
local file = {t={}}
function file:write (str)
str = str:match("([^%s]+)")
Expand Down Expand Up @@ -959,13 +956,8 @@ function xpath_printer_from_grammar(production, print_default, root)
-- As a special case, the table handler allows the keyword to be nil,
-- for printing tables at the top level without keywords.
function handlers.table(keyword, production)
local key_order, value_order = {}, {}
for k,_ in pairs(production.keys) do table.insert(key_order, k) end
for k,_ in pairs(production.values) do table.insert(value_order, k) end
table.sort(key_order)
table.sort(value_order)
local compose_key = key_composer(production.keys, key_order)
local print_value = body_printer(production.values, value_order)
local compose_key = key_composer(production.keys)
local print_value = body_printer(production.values)
if production.key_ctype and production.value_ctype then
return function(data, file, path)
path = path or ''
Expand Down Expand Up @@ -1121,7 +1113,7 @@ function influxdb_printer_from_grammar(production, print_default, root)
file:write(file.is_tag and value or ' value='..value)
file:write('\n')
end
local function body_printer(productions, order)
local function body_printer(productions)
-- Iterate over productions trying to translate to other statements. This
-- is used for example in choice statements raising the lower statements
-- in case blocks up to the level of the choice, in place of the choice.
Expand All @@ -1131,17 +1123,14 @@ function influxdb_printer_from_grammar(production, print_default, root)
if translator ~= nil then
local statements = translator(keyword, production)
for k,v in pairs(statements) do translated[k] = v end
order = nil
else
translated[keyword] = production
end
end
productions = translated
if not order then
order = {}
for k,_ in pairs(productions) do table.insert(order, k) end
table.sort(order)
end
local order = {}
for k,_ in pairs(productions) do table.insert(order, k) end
table.sort(order)
local printers = {}
for keyword,production in pairs(productions) do
local printer = printer(keyword, production, printers)
Expand All @@ -1161,8 +1150,8 @@ function influxdb_printer_from_grammar(production, print_default, root)
:gsub(',', '\\,')
:gsub(' ', '\\ ')
end
local function key_composer (productions, order)
local printer = body_printer(productions, order)
local function key_composer (productions)
local printer = body_printer(productions)
local file = {t={}, is_tag=true}
function file:write (str)
str = str:match("([^%s]+)")
Expand Down Expand Up @@ -1226,14 +1215,9 @@ function influxdb_printer_from_grammar(production, print_default, root)
-- As a special case, the table handler allows the keyword to be nil,
-- for printing tables at the top level without keywords.
function handlers.table(keyword, production)
local key_order, value_order = {}, {}
for k,_ in pairs(production.keys) do table.insert(key_order, k) end
for k,_ in pairs(production.values) do table.insert(value_order, k) end
table.sort(key_order)
table.sort(value_order)
local is_key_unique = is_key_unique(production)
local compose_key = key_composer(production.keys, key_order)
local print_value = body_printer(production.values, value_order)
local compose_key = key_composer(production.keys)
local print_value = body_printer(production.values)
if production.key_ctype and production.value_ctype then
return function(data, file, path)
path = path or ''
Expand Down Expand Up @@ -1363,7 +1347,7 @@ function data_printer_from_grammar(production, print_default)
print_yang_string(k, file)
file:write(' ')
end
local function body_printer(productions, order)
local function body_printer(productions)
-- Iterate over productions trying to translate to other statements. This
-- is used for example in choice statements raising the lower statements
-- in case blocks up to the level of the choice, in place of the choice.
Expand All @@ -1373,17 +1357,14 @@ function data_printer_from_grammar(production, print_default)
if translator ~= nil then
local statements = translator(keyword, production)
for k,v in pairs(statements) do translated[k] = v end
order = nil
else
translated[keyword] = production
end
end
productions = translated
if not order then
order = {}
for k,_ in pairs(productions) do table.insert(order, k) end
table.sort(order)
end
local order = {}
for k,_ in pairs(productions) do table.insert(order, k) end
table.sort(order)
local printers = {}
for keyword,production in pairs(productions) do
local printer = printer(keyword, production, printers)
Expand Down Expand Up @@ -1429,13 +1410,8 @@ function data_printer_from_grammar(production, print_default)
-- As a special case, the table handler allows the keyword to be nil,
-- for printing tables at the top level without keywords.
function handlers.table(keyword, production)
local key_order, value_order = {}, {}
for k,_ in pairs(production.keys) do table.insert(key_order, k) end
for k,_ in pairs(production.values) do table.insert(value_order, k) end
table.sort(key_order)
table.sort(value_order)
local print_key = body_printer(production.keys, key_order)
local print_value = body_printer(production.values, value_order)
local print_key = body_printer(production.keys)
local print_value = body_printer(production.values)
if production.key_ctype and production.value_ctype then
return function(data, file, indent)
for entry in data:iterate() do
Expand Down Expand Up @@ -1764,6 +1740,15 @@ function selftest()
description
"Address prefixes bound to this interface.";
}
list choices {
key id;
leaf id { type string; }
choice choice {
leaf red { type string; }
leaf blue { type string; }
}
}
}]])

local data = load_config_for_schema(test_schema,
Expand All @@ -1777,6 +1762,9 @@ function selftest()
}
addr 1.2.3.4;
address 1.2.3.4/24;
choices { id "one"; blue "hey"; }
choices { id "two"; red "bye"; }
]])
for i =1,2 do
assert(data.fruit_bowl.description == 'ohai')
Expand All @@ -1789,6 +1777,8 @@ function selftest()
assert(contents.baz.score == 9)
assert(contents.baz.tree_grown == true)
assert(data.addr == util.ipv4_pton('1.2.3.4'))
assert(data.choices.one.blue == "hey")
assert(data.choices.two.red == "bye")

local stream = mem.tmpfile()
print_config_for_schema(test_schema, data, stream)
Expand Down

0 comments on commit e96ae1a

Please sign in to comment.