rbjq 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rbjq
4
+ # @text, @json, @csv, @tsv, @html, @uri, @urid, @sh, @base64, @base64d
5
+ module Formats
6
+ module_function
7
+
8
+ def apply(name, v)
9
+ case name
10
+ when 'text' then Ops.tostring(v)
11
+ when 'json' then Encoder.dump(v)
12
+ when 'html' then html(Ops.tostring(v))
13
+ when 'uri' then uri(Ops.tostring(v))
14
+ when 'csv' then csv(v)
15
+ when 'tsv' then tsv(v)
16
+ when 'sh' then sh(v)
17
+ when 'base64' then [Ops.tostring(v)].pack('m0').force_encoding(Encoding::UTF_8)
18
+ when 'base64d' then base64d(Ops.tostring(v))
19
+ when 'urid' then urid(Ops.tostring(v))
20
+ else raise JQError, "#{name} is not a valid format"
21
+ end
22
+ end
23
+
24
+ HTML = { '<' => '&lt;', '>' => '&gt;', '&' => '&amp;', "'" => '&apos;', '"' => '&quot;' }.freeze
25
+
26
+ def html(s)
27
+ s.gsub(/[<>&'"]/, HTML)
28
+ end
29
+
30
+ def uri(s)
31
+ s.b.gsub(/[^A-Za-z0-9\-_.~]/n) { |c| format('%%%02X', c.ord) }.force_encoding(Encoding::UTF_8)
32
+ end
33
+
34
+ def csv(v)
35
+ raise JQError, "#{Value.desc(v)} cannot be csv-formatted, only array" unless v.is_a?(Array)
36
+
37
+ v.map do |e|
38
+ case e
39
+ when Integer, Float then Encoder.format_number(e)
40
+ when String then "\"#{e.gsub('"', '""')}\""
41
+ when true then 'true'
42
+ when false then 'false'
43
+ when nil then ''
44
+ else raise JQError, "#{Value.desc(e)} is not valid in a csv row"
45
+ end
46
+ end.join(',')
47
+ end
48
+
49
+ def tsv(v)
50
+ raise JQError, "#{Value.desc(v)} cannot be tsv-formatted, only array" unless v.is_a?(Array)
51
+
52
+ v.map do |e|
53
+ case e
54
+ when Integer, Float then Encoder.format_number(e)
55
+ when String then e.gsub(/[\\\t\n\r]/, '\\' => '\\\\', "\t" => '\\t', "\n" => '\\n', "\r" => '\\r')
56
+ when true then 'true'
57
+ when false then 'false'
58
+ when nil then ''
59
+ else raise JQError, "#{Value.desc(e)} is not valid in a csv row"
60
+ end
61
+ end.join("\t")
62
+ end
63
+
64
+ def sh(v)
65
+ items = v.is_a?(Array) ? v : [v]
66
+ items.map do |e|
67
+ case e
68
+ when String then "'#{e.gsub("'", "'\\\\''")}'"
69
+ when Array, Hash then raise JQError, "#{Value.desc(e)} can not be escaped for shell"
70
+ else Encoder.dump(e)
71
+ end
72
+ end.join(' ')
73
+ end
74
+
75
+ def base64d(s)
76
+ # like jq: decoding stops at the first '=', other characters are errors
77
+ str = s.split('=', 2).first || ''
78
+ raise JQError, "#{Value.desc(s)} is not valid base64 data" if str.match?(%r{[^A-Za-z0-9+/]})
79
+ raise JQError, "#{Value.desc(s)} trailing base64 byte found" if str.length % 4 == 1
80
+
81
+ str += '=' * ((4 - (str.length % 4)) % 4)
82
+ out = str.unpack1('m')
83
+ Value.scrub_utf8(out)
84
+ end
85
+
86
+ def urid(s)
87
+ bad = -> { raise JQError, "#{Value.desc(s)} is not a valid uri encoding" }
88
+ out = s.b.gsub(/%(..)?/n) do
89
+ hex = Regexp.last_match(1)
90
+ bad.call unless hex&.match?(/\A[0-9A-Fa-f]{2}\z/)
91
+ hex.to_i(16).chr
92
+ end
93
+ out.force_encoding(Encoding::UTF_8)
94
+ bad.call unless out.valid_encoding?
95
+ out
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rbjq
4
+ # jq functions implemented in Ruby by the user.
5
+ #
6
+ # A function receives the input and its arguments as evaluated values, like
7
+ # jq's `$x` parameters: when an argument produces several values, the
8
+ # function is called for each combination, with the first argument varying
9
+ # slowest. The values are deep-frozen copies, so a function cannot change
10
+ # jq's values in place. It returns one value. Raising Rbjq::JQError makes a
11
+ # jq error that `try` can catch.
12
+ #
13
+ # The number of jq arguments is the callable's arity minus one (the input).
14
+ # For a callable with optional or rest parameters, give it in the name:
15
+ # "name/arity".
16
+ module Functions
17
+ NAME = /\A[a-zA-Z_][a-zA-Z0-9_]*\z/
18
+ @global = {}
19
+ @lock = Mutex.new
20
+
21
+ class << self
22
+ # Hash of "name/arity" => native implementation
23
+ def global
24
+ @lock.synchronize { @global.dup }
25
+ end
26
+
27
+ def define_global(name, callable)
28
+ key, native = build(name, callable)
29
+ @lock.synchronize { @global[key] = native }
30
+ key
31
+ end
32
+
33
+ # name: "name/arity", or "name" to remove every arity
34
+ def undefine_global(name)
35
+ name = name.to_s
36
+ @lock.synchronize do
37
+ if name.include?('/')
38
+ !@global.delete(name).nil?
39
+ else
40
+ keys = @global.keys.select { |k| k.start_with?("#{name}/") }
41
+ keys.each { |k| @global.delete(k) }
42
+ !keys.empty?
43
+ end
44
+ end
45
+ end
46
+
47
+ # Hash of name => callable to Hash of "name/arity" => native implementation
48
+ def build_all(functions)
49
+ functions.to_h { |name, callable| build(name, callable) }
50
+ end
51
+
52
+ def build(name, callable)
53
+ raise ArgumentError, "function #{name.inspect} must respond to #call" unless callable.respond_to?(:call)
54
+
55
+ name, arity = parse_name(name.to_s, callable)
56
+ [key_for(name, arity), native(callable, arity)]
57
+ end
58
+
59
+ private
60
+
61
+ def key_for(name, arity)
62
+ "#{name}/#{arity}"
63
+ end
64
+
65
+ def parse_name(name, callable)
66
+ base, explicit = name.split('/', 2)
67
+ raise ArgumentError, "invalid function name: #{name.inspect}" unless base.match?(NAME)
68
+
69
+ if explicit
70
+ raise ArgumentError, "invalid arity in #{name.inspect}" unless explicit.match?(/\A\d+\z/)
71
+
72
+ return [base, explicit.to_i]
73
+ end
74
+
75
+ params = callable.respond_to?(:arity) ? callable.arity : -1
76
+ if params.negative?
77
+ raise ArgumentError, "the number of arguments of #{name.inspect} is not fixed; " \
78
+ "give it as \"#{name}/N\""
79
+ end
80
+ raise ArgumentError, "function #{name.inspect} must take the input as its first argument" if params.zero?
81
+
82
+ [base, params - 1]
83
+ end
84
+
85
+ def native(callable, _arity)
86
+ invalid = Interpreter::INVALID
87
+ lambda do |interp, input, path, args, env, &blk|
88
+ frozen_input = deep_freeze(input)
89
+ each_value_args(interp, args, 0, [], input, env) do |values|
90
+ blk.call(Program.normalize_value(callable.call(frozen_input, *values)), path && invalid)
91
+ end
92
+ end
93
+ end
94
+
95
+ # Cartesian product of the argument values; the first argument varies
96
+ # slowest, like jq's `def f($a; $b)`.
97
+ def each_value_args(interp, args, i, acc, input, env, &blk)
98
+ return yield(acc) if i == args.size
99
+
100
+ interp.eval_node(args[i], input, nil, env) do |v, _|
101
+ each_value_args(interp, args, i + 1, acc + [deep_freeze(v)], input, env, &blk)
102
+ end
103
+ end
104
+
105
+ # A frozen deep copy (strings, arrays and objects)
106
+ def deep_freeze(v)
107
+ case v
108
+ when String then v.frozen? ? v : v.dup.freeze
109
+ when Array then v.map { |e| deep_freeze(e) }.freeze
110
+ when Hash then v.to_h { |k, e| [k.frozen? ? k : k.dup.freeze, deep_freeze(e)] }.freeze
111
+ else v
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ class << self
118
+ # Define a jq function for every program compiled afterwards.
119
+ #
120
+ # Rbjq.define_function('double') { |input| input * 2 }
121
+ # Rbjq.define_function('add_n') { |input, n| input + n }
122
+ # Rbjq.query(3, 'double, add_n(10)') #=> [6, 13]
123
+ #
124
+ # Functions given to Rbjq.compile(functions:) take precedence, and jq
125
+ # definitions (`def`) in a program take precedence over both. Returns the
126
+ # "name/arity" key.
127
+ def define_function(name, callable = nil, &block)
128
+ Functions.define_global(name, callable || block)
129
+ end
130
+
131
+ # Remove a function defined with define_function ("name/arity", or
132
+ # "name" for every arity). Returns whether one was removed.
133
+ def undefine_function(name)
134
+ Functions.undefine_global(name)
135
+ end
136
+ end
137
+ end