[Groonga-commit] ranguba/groonga-client at 02effdd [master] Support schema command response partially

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Mar 22 00:46:55 JST 2016


Kouhei Sutou	2016-03-22 00:46:55 +0900 (Tue, 22 Mar 2016)

  New Revision: 02effddc2c731ee4ec2c917b08e77c10a8673ee8
  https://github.com/ranguba/groonga-client/commit/02effddc2c731ee4ec2c917b08e77c10a8673ee8

  Message:
    Support schema command response partially

  Added files:
    lib/groonga/client/response/schema.rb
    test/response/test-schema.rb
  Modified files:
    groonga-client.gemspec
    lib/groonga/client/response.rb
    lib/groonga/client/response/base.rb

  Modified: groonga-client.gemspec (+1 -0)
===================================================================
--- groonga-client.gemspec    2016-03-21 23:17:00 +0900 (f943b45)
+++ groonga-client.gemspec    2016-03-22 00:46:55 +0900 (c574367)
@@ -46,6 +46,7 @@ Gem::Specification.new do |spec|
 
   spec.add_runtime_dependency("gqtp", ">= 1.0.4")
   spec.add_runtime_dependency("groonga-command", ">= 1.2.0")
+  spec.add_runtime_dependency("hashie")
 
   spec.add_development_dependency("bundler")
   spec.add_development_dependency("rake")

  Modified: lib/groonga/client/response.rb (+1 -0)
===================================================================
--- lib/groonga/client/response.rb    2016-03-21 23:17:00 +0900 (df76b80)
+++ lib/groonga/client/response.rb    2016-03-22 00:46:55 +0900 (600e715)
@@ -32,6 +32,7 @@ require "groonga/client/response/log-put"
 require "groonga/client/response/log-reopen"
 require "groonga/client/response/quit"
 require "groonga/client/response/register"
+require "groonga/client/response/schema"
 require "groonga/client/response/select"
 require "groonga/client/response/status"
 require "groonga/client/response/table-create"

  Modified: lib/groonga/client/response/base.rb (+2 -1)
===================================================================
--- lib/groonga/client/response/base.rb    2016-03-21 23:17:00 +0900 (552cb0b)
+++ lib/groonga/client/response/base.rb    2016-03-22 00:46:55 +0900 (fe2da3e)
@@ -18,9 +18,10 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 require "rexml/document"
-
 require "json"
 
+require "hashie"
+
 module Groonga
   class Client
     module Response

  Added: lib/groonga/client/response/schema.rb (+151 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga/client/response/schema.rb    2016-03-22 00:46:55 +0900 (cbe2a5d)
@@ -0,0 +1,151 @@
+# Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "groonga/client/response/base"
+
+module Groonga
+  class Client
+    module Response
+      # The response class for `schema` command.
+      #
+      # @since 0.2.2
+      class Schema < Base
+        Response.register("schema", self)
+
+        # @return [Hash<String, Type>] Key is type name and
+        #   value is the definition of the type.
+        #
+        # @since 0.2.2
+        def types
+          @types ||= HashValueConverter.convert(@body["types"]) do |raw_type|
+            Type[raw_type]
+          end
+        end
+
+        # @return [Hash<String, Tokenizer>] Key is tokenizer name and
+        #   value is the definition of the tokenizer.
+        #
+        # @since 0.2.2
+        def tokenizers
+          @tokenizers ||= HashValueConverter.convert(@body["tokenizers"]) do |tokenizer|
+            Tokenizer[tokenizer]
+          end
+        end
+
+        # @return [Hash<String, Table>] Key is table name and value is the
+        #   definition of the table.
+        #
+        # @since 0.2.2
+        def tables
+          @tables ||= HashValueConverter.convert(@body["tables"]) do |raw_table|
+            table = Table.new(self)
+            raw_table.each do |key, value|
+              table[key] = value
+            end
+            table
+          end
+        end
+
+        module HashValueConverter
+          class << self
+            def convert(hash)
+              converted = {}
+              hash.each do |key, value|
+                converted[key] = yield(value)
+              end
+              converted
+            end
+          end
+        end
+
+        class Type < Hash
+          include Hashie::Extensions::MethodAccess
+        end
+
+        class Tokenizer < Hash
+          include Hashie::Extensions::MethodAccess
+        end
+
+        class KeyType < Hash
+          include Hashie::Extensions::MethodAccess
+        end
+
+        class ValueType < Hash
+          include Hashie::Extensions::MethodAccess
+        end
+
+        class Column < Hash
+          include Hashie::Extensions::MethodAccess
+
+          def initialize(schema, properties)
+            @schema = schema
+            super()
+            properties.each do |key, value|
+              self[key] = value
+            end
+          end
+        end
+
+        class Table < Hash
+          include Hashie::Extensions::MethodAccess
+
+          def initialize(schema)
+            @schema = schema
+            super()
+          end
+
+          def []=(key, value)
+            case key.to_sym
+            when :key_type
+              super(key, coerce_key_type(value))
+            when :tokenizer
+              super(key, coerce_tokenzer(value))
+            when :columns
+              super(key, coerce_columns(value))
+            else
+              super
+            end
+          end
+
+          private
+          def coerce_key_type(raw_key_type)
+            if raw_key_type.nil?
+              nil
+            elsif raw_key_type["type"] == "type"
+              @schema.types[raw_key_type["name"]]
+            else
+              @schema.tables[raw_key_type["name"]]
+            end
+          end
+
+          def coerce_tokenizer(raw_tokenizer)
+            if raw_tokenizer.nil?
+              nil
+            else
+              @schema.tokenizers[raw_tokenizer["name"]]
+            end
+          end
+
+          def coerce_columns(raw_columns)
+            HashValueConverter.convert(raw_columns) do |raw_column|
+              Column.new(@schema, raw_column)
+            end
+          end
+        end
+      end
+    end
+  end
+end

  Added: test/response/test-schema.rb (+83 -0) 100644
===================================================================
--- /dev/null
+++ test/response/test-schema.rb    2016-03-22 00:46:55 +0900 (2b92c29)
@@ -0,0 +1,83 @@
+# Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "response/helper"
+
+class TestResponseSchema < Test::Unit::TestCase
+  class TestParseRawResponse < self
+    include TestResponseHelper
+
+    def test_select
+      header = [0, 1372430096.70991, 0.000522851943969727]
+      body = {}
+      raw_response = [header, body].to_json
+
+      response = parse_raw_response("schema", raw_response)
+      assert_equal(Groonga::Client::Response::Schema, response.class)
+    end
+  end
+
+  class TestBody < self
+    def setup
+      @command = Groonga::Command::Base.new("schema", {})
+    end
+
+    def create_response(body)
+      header = [0, 1372430096.70991, 0.000522851943969727]
+      Groonga::Client::Response::Schema.new(@command, header, body)
+    end
+
+    class TestTables < self
+      def test_key_type
+        body = {
+          "types" => {
+            "ShortText" => {
+              "name" => "ShortText",
+            },
+          },
+          "tables" => {
+            "Users" => {
+              "key_type" => {
+                "name" => "ShortText",
+                "type" => "type",
+              },
+            }
+          }
+        }
+        response = create_response(body)
+        assert_equal("ShortText",
+                     response.tables["Users"].key_type.name)
+      end
+
+      def test_columns
+        body = {
+          "tables" => {
+            "Users" => {
+              "columns" => {
+                "age" => {
+                  "name" => "age",
+                }
+              }
+            }
+          }
+        }
+        response = create_response(body)
+        assert_equal("age",
+                     response.tables["Users"].columns["age"].name)
+      end
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
다운로드 



More information about the Groonga-commit mailing list
Back to archive index