File: function.rb

package info (click to toggle)
ruby-jmespath 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: ruby: 2,039; makefile: 4
file content (653 lines) | stat: -rw-r--r-- 19,757 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# frozen_string_literal: true
module JMESPath
  # @api private
  module Nodes
    class Function < Node
      FUNCTIONS = {}

      def initialize(children, options = {})
        @children = children
        @options = options
        @disable_visit_errors = @options[:disable_visit_errors]
      end

      def self.create(name, children, options = {})
        if (type = FUNCTIONS[name])
          type.new(children, options)
        else
          raise Errors::UnknownFunctionError, "unknown function #{name}()"
        end
      end

      def visit(value)
        call(@children.map { |child| child.visit(value) })
      end

      def optimize
        self.class.new(@children.map(&:optimize), @options)
      end

      class FunctionName
        attr_reader :name

        def initialize(name)
          @name = name
        end
      end

      private

      def maybe_raise(error_type, message)
        raise error_type, message unless @disable_visit_errors
      end

      def call(_args)
        nil
      end
    end

    module TypeChecker
      def get_type(value)
        if value.respond_to?(:to_str)
          STRING_TYPE
        elsif value == true || value == false
          BOOLEAN_TYPE
        elsif value.nil?
          NULL_TYPE
        elsif value.is_a?(Numeric)
          NUMBER_TYPE
        elsif value.respond_to?(:to_hash) || value.is_a?(Struct)
          OBJECT_TYPE
        elsif value.respond_to?(:to_ary)
          ARRAY_TYPE
        elsif value.is_a?(Expression)
          EXPRESSION_TYPE
        end
      end

      ARRAY_TYPE = 0
      BOOLEAN_TYPE = 1
      EXPRESSION_TYPE = 2
      NULL_TYPE = 3
      NUMBER_TYPE = 4
      OBJECT_TYPE = 5
      STRING_TYPE = 6

      TYPE_NAMES = {
        ARRAY_TYPE => 'array',
        BOOLEAN_TYPE => 'boolean',
        EXPRESSION_TYPE => 'expression',
        NULL_TYPE => 'null',
        NUMBER_TYPE => 'number',
        OBJECT_TYPE => 'object',
        STRING_TYPE => 'string'
      }.freeze
    end

    class AbsFunction < Function
      FUNCTIONS['abs'] = self

      def call(args)
        if args.count == 1
          value = args.first
        else
          return maybe_raise Errors::InvalidArityError, 'function abs() expects one argument'
        end
        if Numeric === value
          value.abs
        else
          return maybe_raise Errors::InvalidTypeError, 'function abs() expects a number'
        end
      end
    end

    class AvgFunction < Function
      FUNCTIONS['avg'] = self

      def call(args)
        if args.count == 1
          values = args.first
        else
          return maybe_raise Errors::InvalidArityError, 'function avg() expects one argument'
        end
        if values.respond_to?(:to_ary)
          values = values.to_ary
          return nil if values.empty?
          values.inject(0) do |total, n|
            if Numeric === n
              total + n
            else
              return maybe_raise Errors::InvalidTypeError, 'function avg() expects numeric values'
            end
          end / values.size.to_f
        else
          return maybe_raise Errors::InvalidTypeError, 'function avg() expects a number'
        end
      end
    end

    class CeilFunction < Function
      FUNCTIONS['ceil'] = self

      def call(args)
        if args.count == 1
          value = args.first
        else
          return maybe_raise Errors::InvalidArityError, 'function ceil() expects one argument'
        end
        if Numeric === value
          value.ceil
        else
          return maybe_raise Errors::InvalidTypeError, 'function ceil() expects a numeric value'
        end
      end
    end

    class ContainsFunction < Function
      FUNCTIONS['contains'] = self

      def call(args)
        if args.count == 2
          haystack = args[0]
          needle = Util.as_json(args[1])
          if haystack.respond_to?(:to_str)
            haystack.to_str.include?(needle)
          elsif haystack.respond_to?(:to_ary)
            haystack.to_ary.any? { |e| Util.as_json(e) == needle }
          else
            return maybe_raise Errors::InvalidTypeError, 'contains expects 2nd arg to be a list'
          end
        else
          return maybe_raise Errors::InvalidArityError, 'function contains() expects 2 arguments'
        end
      end
    end

    class FloorFunction < Function
      FUNCTIONS['floor'] = self

      def call(args)
        if args.count == 1
          value = args.first
        else
          return maybe_raise Errors::InvalidArityError, 'function floor() expects one argument'
        end
        if Numeric === value
          value.floor
        else
          return maybe_raise Errors::InvalidTypeError, 'function floor() expects a numeric value'
        end
      end
    end

    class LengthFunction < Function
      FUNCTIONS['length'] = self

      def call(args)
        if args.count == 1
          value = args.first
        else
          return maybe_raise Errors::InvalidArityError, 'function length() expects one argument'
        end
        if value.respond_to?(:to_hash)
          value.to_hash.size
        elsif value.respond_to?(:to_ary)
          value.to_ary.size
        elsif value.respond_to?(:to_str)
          value.to_str.size
        else
          return maybe_raise Errors::InvalidTypeError, 'function length() expects string, array or object'
        end
      end
    end

    class Map < Function
      FUNCTIONS['map'] = self

      def call(args)
        if args.count != 2
          return maybe_raise Errors::InvalidArityError, 'function map() expects two arguments'
        end
        if Nodes::Expression === args[0]
          expr = args[0]
        else
          return maybe_raise Errors::InvalidTypeError, 'function map() expects the first argument to be an expression'
        end
        if args[1].respond_to?(:to_ary)
          list = args[1].to_ary
        else
          return maybe_raise Errors::InvalidTypeError, 'function map() expects the second argument to be an list'
        end
        list.map { |value| expr.eval(value) }
      end
    end

    class MaxFunction < Function
      include TypeChecker

      FUNCTIONS['max'] = self

      def call(args)
        if args.count == 1
          values = args.first
        else
          return maybe_raise Errors::InvalidArityError, 'function max() expects one argument'
        end
        if values.respond_to?(:to_ary)
          values = values.to_ary
          return nil if values.empty?
          first = values.first
          first_type = get_type(first)
          unless first_type == NUMBER_TYPE || first_type == STRING_TYPE
            msg = String.new('function max() expects numeric or string values')
            return maybe_raise Errors::InvalidTypeError, msg
          end
          values.inject([first, first_type]) do |(max, max_type), v|
            v_type = get_type(v)
            if max_type == v_type
              v > max ? [v, v_type] : [max, max_type]
            else
              msg = String.new('function max() encountered a type mismatch in sequence: ')
              msg << "#{max_type}, #{v_type}"
              return maybe_raise Errors::InvalidTypeError, msg
            end
          end.first
        else
          return maybe_raise Errors::InvalidTypeError, 'function max() expects an array'
        end
      end
    end

    class MinFunction < Function
      include TypeChecker

      FUNCTIONS['min'] = self

      def call(args)
        if args.count == 1
          values = args.first
        else
          return maybe_raise Errors::InvalidArityError, 'function min() expects one argument'
        end
        if values.respond_to?(:to_ary)
          values = values.to_ary
          return nil if values.empty?
          first = values.first
          first_type = get_type(first)
          unless first_type == NUMBER_TYPE || first_type == STRING_TYPE
            msg = String.new('function min() expects numeric or string values')
            return maybe_raise Errors::InvalidTypeError, msg
          end
          values.inject([first, first_type]) do |(min, min_type), v|
            v_type = get_type(v)
            if min_type == v_type
              v < min ? [v, v_type] : [min, min_type]
            else
              msg = String.new('function min() encountered a type mismatch in sequence: ')
              msg << "#{min_type}, #{v_type}"
              return maybe_raise Errors::InvalidTypeError, msg
            end
          end.first
        else
          return maybe_raise Errors::InvalidTypeError, 'function min() expects an array'
        end
      end
    end

    class TypeFunction < Function
      include TypeChecker

      FUNCTIONS['type'] = self

      def call(args)
        if args.count == 1
          TYPE_NAMES[get_type(args.first)]
        else
          return maybe_raise Errors::InvalidArityError, 'function type() expects one argument'
        end
      end
    end

    class KeysFunction < Function
      FUNCTIONS['keys'] = self

      def call(args)
        if args.count == 1
          value = args.first
          if value.respond_to?(:to_hash)
            value.to_hash.keys.map(&:to_s)
          elsif value.is_a?(Struct)
            value.members.map(&:to_s)
          else
            return maybe_raise Errors::InvalidTypeError, 'function keys() expects a hash'
          end
        else
          return maybe_raise Errors::InvalidArityError, 'function keys() expects one argument'
        end
      end
    end

    class ValuesFunction < Function
      FUNCTIONS['values'] = self

      def call(args)
        if args.count == 1
          value = args.first
          if value.respond_to?(:to_hash)
            value.to_hash.values
          elsif value.is_a?(Struct)
            value.values
          elsif value.respond_to?(:to_ary)
            value.to_ary
          else
            return maybe_raise Errors::InvalidTypeError, 'function values() expects an array or a hash'
          end
        else
          return maybe_raise Errors::InvalidArityError, 'function values() expects one argument'
        end
      end
    end

    class JoinFunction < Function
      FUNCTIONS['join'] = self

      def call(args)
        if args.count == 2
          glue = args[0]
          values = args[1]
          if !glue.respond_to?(:to_str)
            return maybe_raise Errors::InvalidTypeError, 'function join() expects the first argument to be a string'
          elsif values.respond_to?(:to_ary) && values.to_ary.all? { |v| v.respond_to?(:to_str) }
            values.to_ary.join(glue)
          else
            return maybe_raise Errors::InvalidTypeError, 'function join() expects values to be an array of strings'
          end
        else
          return maybe_raise Errors::InvalidArityError, 'function join() expects an array of strings'
        end
      end
    end

    class ToStringFunction < Function
      FUNCTIONS['to_string'] = self

      def call(args)
        if args.count == 1
          value = args.first
          value.respond_to?(:to_str) ? value.to_str : value.to_json
        else
          return maybe_raise Errors::InvalidArityError, 'function to_string() expects one argument'
        end
      end
    end

    class ToNumberFunction < Function
      FUNCTIONS['to_number'] = self

      def call(args)
        if args.count == 1
          begin
            value = Float(args.first)
            Integer(value) === value ? value.to_i : value
          rescue
            nil
          end
        else
          return maybe_raise Errors::InvalidArityError, 'function to_number() expects one argument'
        end
      end
    end

    class SumFunction < Function
      FUNCTIONS['sum'] = self

      def call(args)
        if args.count == 1 && args.first.respond_to?(:to_ary)
          args.first.to_ary.inject(0) do |sum, n|
            if Numeric === n
              sum + n
            else
              return maybe_raise Errors::InvalidTypeError, 'function sum() expects values to be numeric'
            end
          end
        else
          return maybe_raise Errors::InvalidArityError, 'function sum() expects one argument'
        end
      end
    end

    class NotNullFunction < Function
      FUNCTIONS['not_null'] = self

      def call(args)
        if args.count > 0
          args.find { |value| !value.nil? }
        else
          return maybe_raise Errors::InvalidArityError, 'function not_null() expects one or more arguments'
        end
      end
    end

    class SortFunction < Function
      include TypeChecker

      FUNCTIONS['sort'] = self

      def call(args)
        if args.count == 1
          value = args.first
          if value.respond_to?(:to_ary)
            value = value.to_ary
            # every element in the list must be of the same type
            array_type = get_type(value[0])
            if array_type == STRING_TYPE || array_type == NUMBER_TYPE || value.empty?
              # stable sort
              n = 0
              value.sort_by do |v|
                value_type = get_type(v)
                if value_type != array_type
                  msg = 'function sort() expects values to be an array of only numbers, or only integers'
                  return maybe_raise Errors::InvalidTypeError, msg
                end
                n += 1
                [v, n]
              end
            else
              return maybe_raise Errors::InvalidTypeError, 'function sort() expects values to be an array of numbers or integers'
            end
          else
            return maybe_raise Errors::InvalidTypeError, 'function sort() expects values to be an array of numbers or integers'
          end
        else
          return maybe_raise Errors::InvalidArityError, 'function sort() expects one argument'
        end
      end
    end

    class SortByFunction < Function
      include TypeChecker

      FUNCTIONS['sort_by'] = self

      def call(args)
        if args.count == 2
          if get_type(args[0]) == ARRAY_TYPE && get_type(args[1]) == EXPRESSION_TYPE
            values = args[0].to_ary
            expression = args[1]
            array_type = get_type(expression.eval(values[0]))
            if array_type == STRING_TYPE || array_type == NUMBER_TYPE || values.empty?
              # stable sort the list
              n = 0
              values.sort_by do |value|
                value = expression.eval(value)
                value_type = get_type(value)
                if value_type != array_type
                  msg = 'function sort() expects values to be an array of only numbers, or only integers'
                  return maybe_raise Errors::InvalidTypeError, msg
                end
                n += 1
                [value, n]
              end
            else
              return maybe_raise Errors::InvalidTypeError, 'function sort() expects values to be an array of numbers or integers'
            end
          else
            return maybe_raise Errors::InvalidTypeError, 'function sort_by() expects an array and an expression'
          end
        else
          return maybe_raise Errors::InvalidArityError, 'function sort_by() expects two arguments'
        end
      end
    end

    module CompareBy
      include TypeChecker

      def compare_by(mode, *args)
        if args.count == 2
          values = args[0]
          expression = args[1]
          if get_type(values) == ARRAY_TYPE && get_type(expression) == EXPRESSION_TYPE
            values = values.to_ary
            type = get_type(expression.eval(values.first))
            if type != NUMBER_TYPE && type != STRING_TYPE
              msg = "function #{mode}() expects values to be strings or numbers"
              return maybe_raise Errors::InvalidTypeError, msg
            end
            values.send(mode) do |entry|
              value = expression.eval(entry)
              value_type = get_type(value)
              if value_type != type
                msg = String.new("function #{mode}() encountered a type mismatch in ")
                msg << "sequence: #{type}, #{value_type}"
                return maybe_raise Errors::InvalidTypeError, msg
              end
              value
            end
          else
            msg = "function #{mode}() expects an array and an expression"
            return maybe_raise Errors::InvalidTypeError, msg
          end
        else
          msg = "function #{mode}() expects two arguments"
          return maybe_raise Errors::InvalidArityError, msg
        end
      end
    end

    class MaxByFunction < Function
      include CompareBy

      FUNCTIONS['max_by'] = self

      def call(args)
        compare_by(:max_by, *args)
      end
    end

    class MinByFunction < Function
      include CompareBy

      FUNCTIONS['min_by'] = self

      def call(args)
        compare_by(:min_by, *args)
      end
    end

    class EndsWithFunction < Function
      include TypeChecker

      FUNCTIONS['ends_with'] = self

      def call(args)
        if args.count == 2
          search, suffix = args
          search_type = get_type(search)
          suffix_type = get_type(suffix)
          if search_type != STRING_TYPE
            msg = 'function ends_with() expects first argument to be a string'
            return maybe_raise Errors::InvalidTypeError, msg
          end
          if suffix_type != STRING_TYPE
            msg = 'function ends_with() expects second argument to be a string'
            return maybe_raise Errors::InvalidTypeError, msg
          end
          search.end_with?(suffix)
        else
          msg = 'function ends_with() expects two arguments'
          return maybe_raise Errors::InvalidArityError, msg
        end
      end
    end

    class StartsWithFunction < Function
      include TypeChecker

      FUNCTIONS['starts_with'] = self

      def call(args)
        if args.count == 2
          search, prefix = args
          search_type = get_type(search)
          prefix_type = get_type(prefix)
          if search_type != STRING_TYPE
            msg = 'function starts_with() expects first argument to be a string'
            return maybe_raise Errors::InvalidTypeError, msg
          end
          if prefix_type != STRING_TYPE
            msg = 'function starts_with() expects second argument to be a string'
            return maybe_raise Errors::InvalidTypeError, msg
          end
          search.start_with?(prefix)
        else
          msg = 'function starts_with() expects two arguments'
          return maybe_raise Errors::InvalidArityError, msg
        end
      end
    end

    class MergeFunction < Function
      FUNCTIONS['merge'] = self

      def call(args)
        if args.count == 0
          msg = 'function merge() expects 1 or more arguments'
          return maybe_raise Errors::InvalidArityError, msg
        end
        args.inject({}) do |h, v|
          h.merge(v)
        end
      end
    end

    class ReverseFunction < Function
      FUNCTIONS['reverse'] = self

      def call(args)
        if args.count == 0
          msg = 'function reverse() expects 1 or more arguments'
          return maybe_raise Errors::InvalidArityError, msg
        end
        value = args.first
        if value.respond_to?(:to_ary)
          value.to_ary.reverse
        elsif value.respond_to?(:to_str)
          value.to_str.reverse
        else
          msg = 'function reverse() expects an array or string'
          return maybe_raise Errors::InvalidTypeError, msg
        end
      end
    end

    class ToArrayFunction < Function
      FUNCTIONS['to_array'] = self

      def call(args)
        value = args.first
        value.respond_to?(:to_ary) ? value.to_ary : [value]
      end
    end
  end
end