groonga-command-parser 1.0.4 → 1.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 653da7d4f65f66a98a4fdc5300f1c40b768d99bf
4
- data.tar.gz: 3be534841fc2a4e8ee175262107ce0336d81c287
3
+ metadata.gz: d57a072e7f7777830941dca1314a03d42a4b656e
4
+ data.tar.gz: 5260af5b490df1a910e03e0d002037cb1b6bd2d8
5
5
  SHA512:
6
- metadata.gz: 4c12a30d319639ac368a1610080cfef25c47198565245453e56265db91508b58664fe6886e1cb80bc6436beef2252ec61734337274740ed26c2ebe1e87201c14
7
- data.tar.gz: 0a541136772e374816b60ceb5052b1848c9d7a0e1f05c5526a575f17e8d16ce570084a70fcc30ad6909311c9f70b6fad517605add4423c48468f1d409228ca47
6
+ metadata.gz: 78894a5a7e7913d26b12f6487760bc0c212913170526bf3c436abbfe40c86217b3a015fe8c7ad171fb96c180e0a9c174943d1c6c53094ce0e1817151def8da15
7
+ data.tar.gz: 2ac829c0a6fd339c6b10dbb8d113c646750705aca2f27aead80772b46aa0484e09a3514ddf39b631fa27f91d83d9362fe365da28985234d1a860e0dd3cdc7b71
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (C) 2015 Kouhei Sutou <kou@clear-code.com>
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ require "groonga/command/parser/command/groonga-command-logicalify"
20
+
21
+ command = Groonga::Command::Parser::Command::GroongaCommandLogicalify.new
22
+ exit(command.run(ARGV))
@@ -1,5 +1,21 @@
1
1
  # News
2
2
 
3
+ ## 1.0.5: 2015-08-08
4
+
5
+ ### Improvements
6
+
7
+ * `groonga-command-logicalify`: Added.
8
+ * Supported URI style `load` command that doesn't have `values`
9
+ parameter in path.
10
+
11
+ ### Fixes
12
+
13
+ * Fixed a bug that parameter name in URL isn't unescaped.
14
+
15
+ ### Thanks
16
+
17
+ * Hiroyuki Sato
18
+
3
19
  ## 1.0.4: 2015-05-23
4
20
 
5
21
  ### Improvements
@@ -96,7 +96,12 @@ module Groonga
96
96
 
97
97
  consume_data(parser, data)
98
98
  if parsed_command.nil?
99
- raise Error.new("not completed", data.lines.to_a.last, "")
99
+ if data.respond_to?(:each)
100
+ last_chunk = data.last
101
+ else
102
+ last_chunk = data
103
+ end
104
+ raise Error.new("not completed", last_chunk, "")
100
105
  end
101
106
 
102
107
  parsed_command
@@ -258,8 +263,13 @@ module Groonga
258
263
  @load_values_parser << @command[:values]
259
264
  reset
260
265
  else
261
- @command.original_source << "\n"
262
- @loading = true
266
+ if @command.original_format == :uri
267
+ on_load_complete(@command)
268
+ reset
269
+ else
270
+ @command.original_source << "\n"
271
+ @loading = true
272
+ end
263
273
  end
264
274
  else
265
275
  on_command(@command)
@@ -282,7 +292,7 @@ module Groonga
282
292
  arguments_string.split(/&/).each do |argument_string|
283
293
  key, value = argument_string.split(/\=/, 2)
284
294
  next if value.nil?
285
- arguments[key] = CGI.unescape(value)
295
+ arguments[CGI.unescape(key)] = CGI.unescape(value)
286
296
  end
287
297
  end
288
298
  if /\/([^\/]*)\z/=~ path
@@ -0,0 +1,146 @@
1
+ # Copyright (C) 2015 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License as published by the Free Software Foundation; either
6
+ # version 2.1 of the License, or (at your option) any later version.
7
+ #
8
+ # This library is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this library; if not, write to the Free Software
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+
17
+ require "optparse"
18
+
19
+ require "groonga/command/parser"
20
+
21
+ module Groonga
22
+ module Command
23
+ class Parser
24
+ module Command
25
+ class GroongaCommandLogicalify
26
+ def initialize
27
+ @logical_table = nil
28
+ @shard_key = nil
29
+ end
30
+
31
+ def run(argv=ARGV)
32
+ begin
33
+ parse_options!(argv)
34
+ rescue OptionParser::ParseError
35
+ puts($!.message)
36
+ return false
37
+ end
38
+
39
+ input_paths = argv
40
+ if input_paths.empty?
41
+ logicalify($stdin)
42
+ else
43
+ input_paths.each do |input_path|
44
+ File.open(input_path) do |input|
45
+ logicalify(input)
46
+ end
47
+ end
48
+ end
49
+
50
+ true
51
+ end
52
+
53
+ private
54
+ def parse_options!(argv)
55
+ option_parser = OptionParser.new
56
+ option_parser.banner += " INPUT_PATH1 INPUT_PATH2 ..."
57
+ option_parser.version = VERSION
58
+
59
+ option_parser.on("--logical-table=TABLE",
60
+ "Use TABLE as logical table name",
61
+ "[#{@logical_table}]") do |logical_table|
62
+ @logical_table = logical_table
63
+ end
64
+
65
+ option_parser.on("--shard-key=COLUMN",
66
+ "Use COLUMN as shard key",
67
+ "[#{@shard_key}]") do |shard_key|
68
+ @shard_key = shard_key
69
+ end
70
+
71
+ option_parser.parse!(argv)
72
+
73
+ if @logical_table.nil?
74
+ raise OptionParser::ParseError, "--logical-table is missing"
75
+ end
76
+
77
+ if @shard_key.nil?
78
+ raise OptionParser::ParseError, "--shard-key is missing"
79
+ end
80
+ end
81
+
82
+ def logicalify(input)
83
+ parser = Parser.new
84
+ parser.on_command do |command|
85
+ logicalify_command(command)
86
+ end
87
+ input.each_line do |line|
88
+ parser << line
89
+ end
90
+ end
91
+
92
+ def logicalify_command(command)
93
+ case command.name
94
+ when "select"
95
+ logicalify_command_select(command)
96
+ end
97
+ end
98
+
99
+ def logicalify_command_select(command)
100
+ min = nil
101
+ max = nil
102
+ case command[:table]
103
+ when /\A#{Regexp.escape(@logical_table)}_(\d{4})(\d{2})(\d{2})\z/
104
+ year = $1.to_i
105
+ month = $2.to_i
106
+ day = $3.to_i
107
+ min = Time.local(year, month, day)
108
+ max = min + (60 * 60 * 24)
109
+ else
110
+ return
111
+ end
112
+
113
+ arguments = logicalify_arguments(command.arguments)
114
+ arguments[:min] = format_time(min)
115
+ arguments[:min_border] = "include"
116
+ arguments[:max] = format_time(max)
117
+ arguments[:max_border] = "exclude"
118
+ logical_select = create_logical_select_command(arguments)
119
+ if command.uri_format?
120
+ puts(logical_select.to_uri_format)
121
+ else
122
+ puts(logical_select.to_command_format)
123
+ end
124
+ end
125
+
126
+ def create_logical_select_command(arguments)
127
+ name = "logical_select"
128
+ command_class = ::Groonga::Command.find(name)
129
+ command_class.new(name, arguments)
130
+ end
131
+
132
+ def logicalify_arguments(arguments)
133
+ arguments = arguments.merge(:logical_table => @logical_table,
134
+ :shard_key => @shard_key)
135
+ arguments.delete(:table)
136
+ arguments
137
+ end
138
+
139
+ def format_time(time)
140
+ time.strftime("%Y/%m/%d %H:%M:%S")
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
@@ -43,7 +43,6 @@ module Groonga
43
43
  location << after
44
44
  else
45
45
  before_lines = before.lines.to_a
46
- after_lines = after.lines.to_a
47
46
  last_before_line = before_lines.last
48
47
  if last_before_line
49
48
  error_offset = last_before_line.bytesize
@@ -53,10 +52,14 @@ module Groonga
53
52
  before_lines.each do |before_line|
54
53
  location << before_line
55
54
  end
56
- location << after_lines.first
57
- location << " " * error_offset + "^\n"
58
- after_lines[1..-1].each do |after_line|
59
- location << after_line
55
+
56
+ unless after.empty?
57
+ after_lines = after.lines.to_a
58
+ location << after_lines.first
59
+ location << " " * error_offset + "^\n"
60
+ after_lines[1..-1].each do |after_line|
61
+ location << after_line
62
+ end
60
63
  end
61
64
  end
62
65
  location
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  #
3
- # Copyright (C) 2013-2014 Kouhei Sutou <kou@clear-code.com>
3
+ # Copyright (C) 2013-2015 Kouhei Sutou <kou@clear-code.com>
4
4
  #
5
5
  # This library is free software; you can redistribute it and/or
6
6
  # modify it under the terms of the GNU Lesser General Public
@@ -19,7 +19,7 @@
19
19
  module Groonga
20
20
  module Command
21
21
  class Parser
22
- VERSION = "1.0.4"
22
+ VERSION = "1.0.5"
23
23
  end
24
24
  end
25
25
  end
@@ -49,6 +49,26 @@ class ParserTest < Test::Unit::TestCase
49
49
  assert_equal({:table => "Users"}, command.arguments)
50
50
  end
51
51
 
52
+ def test_bracket
53
+ path = "/d/select?table=Users&drilldown[name].keys=name"
54
+ command = Groonga::Command::Parser.parse(path)
55
+ assert_equal({
56
+ :table => "Users",
57
+ :"drilldown[name].keys" => "name",
58
+ },
59
+ command.arguments)
60
+ end
61
+
62
+ def test_escaped_bracket
63
+ path = "/d/select?table=Users&drilldown%5Bname%5D.keys=name"
64
+ command = Groonga::Command::Parser.parse(path)
65
+ assert_equal({
66
+ :table => "Users",
67
+ :"drilldown[name].keys" => "name",
68
+ },
69
+ command.arguments)
70
+ end
71
+
52
72
  def test_custom_prefix
53
73
  path = "/db1/select?table=Users"
54
74
  command = Groonga::Command::Parser.parse(path)
@@ -163,6 +183,19 @@ status
163
183
  end
164
184
  end
165
185
 
186
+ class URITest < self
187
+ def test_no_values
188
+ uri = "/d/load?table=Users"
189
+ @parser << uri
190
+ @parser << "\n"
191
+ assert_equal([
192
+ [:load_start, uri],
193
+ [:load_complete, uri],
194
+ ],
195
+ @events)
196
+ end
197
+ end
198
+
166
199
  class InlineTest < self
167
200
  class BracketTest < self
168
201
  def test_have_columns
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: groonga-command-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kouhei Sutou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-23 00:00:00.000000000 Z
11
+ date: 2015-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: groonga-command
@@ -155,6 +155,7 @@ email:
155
155
  - kou@clear-code.com
156
156
  executables:
157
157
  - groonga-command-convert-format
158
+ - groonga-command-logicalify
158
159
  extensions: []
159
160
  extra_rdoc_files: []
160
161
  files:
@@ -163,12 +164,14 @@ files:
163
164
  - README.md
164
165
  - Rakefile
165
166
  - bin/groonga-command-convert-format
167
+ - bin/groonga-command-logicalify
166
168
  - doc/text/lgpl-2.1.txt
167
169
  - doc/text/news.md
168
170
  - groonga-command-parser.gemspec
169
171
  - lib/groonga/command/parser.rb
170
172
  - lib/groonga/command/parser/command-line-splitter.rb
171
173
  - lib/groonga/command/parser/command/groonga-command-convert-format.rb
174
+ - lib/groonga/command/parser/command/groonga-command-logicalify.rb
172
175
  - lib/groonga/command/parser/error.rb
173
176
  - lib/groonga/command/parser/load-values-parser.rb
174
177
  - lib/groonga/command/parser/version.rb