Navigation Menu

Skip to content

Commit

Permalink
Support output by expression
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kou committed Jun 19, 2013
1 parent 539d768 commit 6de5280
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
51 changes: 28 additions & 23 deletions lib/droonga/plugin/handler_search.rb
Expand Up @@ -274,38 +274,43 @@ def simple_record(attributes, record)
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
Expand Down
35 changes: 35 additions & 0 deletions test/plugin/test_handler_search.rb
Expand Up @@ -352,6 +352,41 @@ def test_static_value
}
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
Expand Down

0 comments on commit 6de5280

Please sign in to comment.