0007-model.uci-add-add_to_set-remove_from_set.patch 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. From: Nils Schneider <nils@nilsschneider.net>
  2. Date: Mon, 17 Aug 2015 20:39:58 +0200
  3. Subject: model.uci: add add_to_set / remove_from_set
  4. Adds two functions to simplify working with UCI lists:
  5. - add_to_set, which ensures a given value will be present in a list, and
  6. - remove_from_set, which removes a value from list.
  7. I've called these methods "set" because they treat the list as a set,
  8. i.e. duplicated values will be removed. Also, order is not preserved.
  9. Signed-off-by: Nils Schneider <nils@nilsschneider.net>
  10. diff --git a/modules/luci-base/luasrc/model/uci.lua b/modules/luci-base/luasrc/model/uci.lua
  11. index 1659137..d35b9d7 100644
  12. --- a/modules/luci-base/luasrc/model/uci.lua
  13. +++ b/modules/luci-base/luasrc/model/uci.lua
  14. @@ -9,7 +9,7 @@ local table = require "table"
  15. local setmetatable, rawget, rawset = setmetatable, rawget, rawset
  16. local require, getmetatable = require, getmetatable
  17. -local error, pairs, ipairs = error, pairs, ipairs
  18. +local error, pairs, ipairs, next = error, pairs, ipairs, next
  19. local type, tostring, tonumber, unpack = type, tostring, tonumber, unpack
  20. -- The typical workflow for UCI is: Get a cursor instance from the
  21. @@ -147,6 +147,40 @@ function Cursor.set_list(self, config, section, option, value)
  22. return false
  23. end
  24. +function Cursor.add_to_set(self, config, section, option, value, remove)
  25. + local list = self:get_list(config, section, option)
  26. +
  27. + if not list then
  28. + return false
  29. + end
  30. +
  31. + local set = {}
  32. + for _, l in ipairs(list) do
  33. + set[l] = true
  34. + end
  35. +
  36. + if remove then
  37. + set[value] = nil
  38. + else
  39. + set[value] = true
  40. + end
  41. +
  42. + list = {}
  43. + for k, _ in pairs(set) do
  44. + table.insert(list, k)
  45. + end
  46. +
  47. + if next(list) == nil then
  48. + return self:delete(config, section, option)
  49. + else
  50. + return self:set(config, section, option, list)
  51. + end
  52. +end
  53. +
  54. +function Cursor.remove_from_set(self, config, section, option, value)
  55. + self:add_to_set(config, section, option, value, true)
  56. +end
  57. +
  58. -- Return a list of initscripts affected by configuration changes.
  59. function Cursor._affected(self, configlist)
  60. configlist = type(configlist) == "table" and configlist or {configlist}
  61. diff --git a/modules/luci-base/luasrc/model/uci.luadoc b/modules/luci-base/luasrc/model/uci.luadoc
  62. index 1c20866..281bdb2 100644
  63. --- a/modules/luci-base/luasrc/model/uci.luadoc
  64. +++ b/modules/luci-base/luasrc/model/uci.luadoc
  65. @@ -116,6 +116,30 @@ Set given values as list.
  66. ]]
  67. ---[[
  68. +Add a given value to a list of unique values.
  69. +
  70. +@class function
  71. +@name Cursor.add_to_set
  72. +@param config UCI config
  73. +@param section UCI section name
  74. +@param option UCI option
  75. +@param value UCI value
  76. +@return Boolean whether operation succeeded
  77. +]]
  78. +
  79. +---[[
  80. +Remove a given value from a list of unique values.
  81. +
  82. +@class function
  83. +@name Cursor.add_to_set
  84. +@param config UCI config
  85. +@param section UCI section name
  86. +@param option UCI option
  87. +@param value UCI value
  88. +@return Boolean whether operation succeeded
  89. +]]
  90. +
  91. +---[[
  92. Create a sub-state of this cursor. The sub-state is tied to the parent
  93. curser, means it the parent unloads or loads configs, the sub state will