Browse Source

add _ operator to escape value as literal

tags/0.3.0^2
rucciva noneedtoknow 9 months ago
parent
commit
82f4eee0dd
3 changed files with 1520 additions and 1296 deletions
  1. 20
    12
      logic.lua
  2. 13
    1
      readme.md
  3. 1487
    1283
      spec/logic_spec.lua

+ 20
- 12
logic.lua View File

@@ -380,7 +380,7 @@ operations['merge'] = function(closure, ...)
380 380
 end
381 381
 
382 382
 operations['var'] = function(closure, attr, default)
383
-    local data = closure.data    
383
+    local data = closure.data
384 384
     if closure.opts.is_nil(attr) or attr == '' then
385 385
         return data
386 386
     end
@@ -483,10 +483,10 @@ local function get_operator(tab)
483 483
     return nil
484 484
 end
485 485
 
486
-local function table_copy_zeroed(source, opts)
486
+local function table_copy_fill(source, opts)
487 487
     local target = {}
488 488
     for i, _ in pairs(source) do
489
-        target[i] = 0
489
+        target[i] = opts.null() or true
490 490
     end
491 491
     if opts.is_array(source) then
492 492
         opts.mark_as_array(target)
@@ -504,7 +504,7 @@ function recurse_array(stack, closure, last_child_result)
504 504
     closure.state.length = closure.state.length or #closure.logic
505 505
     closure.state.index = closure.state.index or 1
506 506
     closure.state.recursed = closure.state.recursed or {}
507
-    closure.state.normalized = closure.state.normalized or table_copy_zeroed(closure.logic, closure.opts)
507
+    closure.state.normalized = closure.state.normalized or table_copy_fill(closure.logic, closure.opts)
508 508
     --
509 509
 
510 510
     -- recurse if necessary
@@ -625,7 +625,10 @@ recurser['and'] =
625 625
     closure.state.normalized[op][closure.state.index] = last_child_result
626 626
     --
627 627
 
628
-    if js_to_boolean(closure, closure.state.normalized[op][closure.state.index]) and closure.state.index < closure.state.length then
628
+    if
629
+        js_to_boolean(closure, closure.state.normalized[op][closure.state.index]) and
630
+            closure.state.index < closure.state.length
631
+     then
629 632
         -- closure condition is true
630 633
         closure.state.index = closure.state.index + 1
631 634
     else
@@ -638,7 +641,7 @@ recurser['and'] =
638 641
 end
639 642
 
640 643
 recurser['or'] =
641
-    function(stack, closure, last_child_result, opts)
644
+    function(stack, closure, last_child_result)
642 645
     local op = get_operator(closure.logic)
643 646
 
644 647
     -- zero length
@@ -764,7 +767,7 @@ recurser['map'] =
764 767
     closure.state.recursed = closure.state.recursed or false
765 768
     closure.state.scoped = closure.state.scoped or false
766 769
     closure.state.mapped = closure.state.mapped or {}
767
-    closure.state.result = closure.state.result or table_copy_zeroed(closure.logic[op], closure.opts)
770
+    closure.state.result = closure.state.result or table_copy_fill(closure.logic[op], closure.opts)
768 771
     closure.state.normalized[op] = closure.state.normalized[op] or closure.opts.array()
769 772
     --
770 773
 
@@ -1179,9 +1182,7 @@ function JsonLogic.apply(logic, data, opts)
1179 1182
         },
1180 1183
         opts = nil
1181 1184
     }
1182
-    if type(opts) ~= 'table' or opts == nil then
1183
-        opts = {}
1184
-    end
1185
+    opts = opts or nil
1185 1186
     if type(opts.is_array) ~= 'function' then
1186 1187
         opts.is_array = is_array
1187 1188
     end
@@ -1219,8 +1220,15 @@ function JsonLogic.apply(logic, data, opts)
1219 1220
             end
1220 1221
             --
1221 1222
 
1222
-            -- check for blacklist or non-whitelisted operations
1223
+            -- literal operator
1223 1224
             local op = get_operator(closure.logic)
1225
+            if op == '_' then
1226
+                last_child_result = closure.logic[op]
1227
+                closure = table.remove(stack)
1228
+                break
1229
+            end
1230
+
1231
+            -- check for blacklist or non-whitelisted operations
1224 1232
             if type(closure.opts.blacklist) == 'table' and closure.opts.blacklist[op] then
1225 1233
                 return closure.logic, 'blacklisted operations'
1226 1234
             elseif type(closure.opts.whitelist) == 'table' and not closure.opts.whitelist[op] then
@@ -1235,7 +1243,7 @@ function JsonLogic.apply(logic, data, opts)
1235 1243
             else
1236 1244
                 closure, last_child_result, err = recurse_others(stack, closure, last_child_result)
1237 1245
             end
1238
-            
1246
+
1239 1247
             -- if the result is nil then return the specified null value
1240 1248
             if last_child_result == nil then
1241 1249
                 last_child_result = opts.null()

+ 13
- 1
readme.md View File

@@ -32,4 +32,16 @@ Luarocks url: <https://luarocks.org/modules/rucciva/json-logic-lua>.
32 32
         - contents: a lua table that contains list of operation names that will be disabled. **all else will be enabled**
33 33
     - whitelist mode (can only be activated when **blacklist** key does not exist in the third parameter)
34 34
         - key : **whitelist**
35
-        - contents: a lua table that contains list of operation names that will be enabled. **all else will be disabled**
35
+        - contents: a lua table that contains list of operation names that will be enabled. **all else will be disabled**
36
+1. operator `_` are used to escape operand and treat it as literal. e.g.: 
37
+
38
+```javascript
39
+    {
40
+        "_" : {
41
+            "map" : [
42
+                41.40338,
43
+                2.17403
44
+            ]
45
+        }
46
+    }
47
+```

+ 1487
- 1283
spec/logic_spec.lua
File diff suppressed because it is too large
View File


Loading…
Cancel
Save