[Groonga-commit] groonga/fluent-plugin-droonga at 6de5280 [master] Support output by expression

Back to archive index

Kouhei Sutou null+****@clear*****
Wed Jun 19 19:02:58 JST 2013


Kouhei Sutou	2013-06-19 19:02:58 +0900 (Wed, 19 Jun 2013)

  New Revision: 6de528051aeff9cb33b090855ea92c54391f9be1
  https://github.com/groonga/fluent-plugin-droonga/commit/6de528051aeff9cb33b090855ea92c54391f9be1

  Message:
    Support output by expression
    
    snippet_html() doesn't work yet because it depends grn_ctx internal
    data such as ctx->impl->curr_expr and $condition variable. They are
    prepared by grn_ctx_qe_exec() and grn_select(). We need API to set
    them.

  Modified files:
    lib/droonga/plugin/handler_search.rb
    test/plugin/test_handler_search.rb

  Modified: lib/droonga/plugin/handler_search.rb (+28 -23)
===================================================================
--- lib/droonga/plugin/handler_search.rb    2013-06-19 17:11:26 +0900 (02d1fbd)
+++ lib/droonga/plugin/handler_search.rb    2013-06-19 19:02:58 +0900 (e68dbcc)
@@ -274,38 +274,43 @@ module Droonga
       end
 
       def record_value(record, attribute)
-        attribute[:static_value] || record[attribute[:source]]
+        expression = attribute[:expression]
+        if expression
+          variable = attribute[:variable]
+          variable.value = record
+          expression.execute
+        else
+          record[attribute[:source]]
+        end
       end
 
-      STATIC_INTEGER_VALUE_PATTERN = /\A[-+]?\d+\z/.freeze
-      STATIC_FLOAT_VALUE_PATTERN = /\A[-+]?\d*\.\d+\z/.freeze
-      STATIC_STRING_VALUE_PATTERN = /\A("[^"]*"|'[^']*')\z/.freeze
+      def accessor_name?(source)
+        /\A[a-zA-Z\#@$_][a-zA-Z\d\#@$_\-.]*\z/ === source
+      end
 
       def normalize_target_attributes(attributes)
         attributes.collect do |attribute|
           if attribute.is_a?(String)
-            {
-              label: attribute,
-              source: attribute,
-              static_value: nil,
+            attribute = {
+              "source" => attribute,
             }
+          end
+          source = attribute["source"]
+          if accessor_name?(source)
+            expression = nil
+            variable = nil
           else
-            source = attribute["source"]
-            static_value = nil
-            case source
-            when STATIC_INTEGER_VALUE_PATTERN
-             static_value = source.to_i
-            when STATIC_FLOAT_VALUE_PATTERN
-             static_value = source.to_f
-            when STATIC_STRING_VALUE_PATTERN
-             static_value = source[1..-2]
-            end
-            {
-              label: attribute["label"] || attribute["source"],
-              source: source,
-              static_value: static_value,
-            }
+            expression = Groonga::Expression.new(context: @context)
+            variable = expression.define_variable(domain: @result)
+            expression.parse(source, syntax: :script)
+            source = nil
           end
+          {
+            label: attribute["label"] || attribute["source"],
+            source: source,
+            expression: expression,
+            variable: variable,
+          }
         end
       end
     end

  Modified: test/plugin/test_handler_search.rb (+35 -0)
===================================================================
--- test/plugin/test_handler_search.rb    2013-06-19 17:11:26 +0900 (b1cc607)
+++ test/plugin/test_handler_search.rb    2013-06-19 19:02:58 +0900 (2333d4e)
@@ -352,6 +352,41 @@ class SearchHandlerTest < Test::Unit::TestCase
         }
         assert_search(expected, request)
       end
+
+      def test_expression
+        expected = {
+          "sections-result" => {
+            "records" => [
+              {
+                "formatted title" => "<Groonga overview>",
+                "title" => "Groonga overview",
+              },
+            ],
+          },
+        }
+        request = {
+          "queries" => {
+            "sections-result" => {
+              "source" => "Sections",
+              "output" => {
+                "elements" => [
+                  "records",
+                ],
+                "format" => "complex",
+                "limit" => 1,
+                "attributes" => [
+                  "title",
+                  {
+                    "label" => "formatted title",
+                    "source" => "'<' + title + '>'",
+                  },
+                ],
+              },
+            },
+          },
+        }
+        assert_search(expected, request)
+      end
     end
 
     class FormatTest < self
-------------- next part --------------
HTML����������������������������...
Download 



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