YUKI Hiroshi
null+****@clear*****
Thu Dec 26 17:24:50 JST 2013
YUKI Hiroshi 2013-12-26 17:24:50 +0900 (Thu, 26 Dec 2013) New Revision: 89b8db76fc7e8116214542ddce784a9a4f10a49a https://github.com/droonga/fluent-plugin-droonga/commit/89b8db76fc7e8116214542ddce784a9a4f10a49a Message: Validate column values Added files: test/command/suite/add/invalid-integer.expected test/command/suite/add/invalid-time.expected Copied files: test/command/suite/add/invalid-integer.test (from test/command/suite/add/mismatched-value-type.test) Removed files: test/command/suite/add/mismatched-value-type.expected Modified files: lib/droonga/plugin/handler/add.rb test/command/suite/add/unknown-column.expected Renamed files: test/command/suite/add/invalid-time.test (from test/command/suite/add/mismatched-value-type.test) Modified: lib/droonga/plugin/handler/add.rb (+33 -5) =================================================================== --- lib/droonga/plugin/handler/add.rb 2013-12-26 17:03:51 +0900 (7691249) +++ lib/droonga/plugin/handler/add.rb 2013-12-26 17:24:50 +0900 (872a629) @@ -37,13 +37,23 @@ module Droonga end end - class UnknownTable < BadRequest + class UnknownTable < NotFound def initialize(table_name) super("The table #{table_name.inspect} does not exist in the dataset.") end + end - def status_code - 404 + class InvalidValue < BadRequest + def initialize(column, value, request) + super("The column #{column.inspect} cannot store the value #{value.inspect}.", + request) + end + end + + class UnknownColumn < BadRequest + def initialize(column, table, request) + super("The column #{column.inspect} does not exist in the table #{table.inspect}.", + request) end end @@ -64,11 +74,29 @@ module Droonga unless request.include?("key") raise MissingPrimaryKeyParameter.new(request["table"]) end - table.add(request["key"], request["values"]) + add_record(table, request) else - table.add(request["values"]) + add_record(table, nil, request["values"], request) end [true] end + + def add_record(table, request) + record = nil + if request["key"] + record = table.add(request["key"]) + else + record = table.add + end + request["values"].each do |column, value| + begin + record[column] = value + rescue Groonga::InvalidArgument => error + raise InvalidValue.new(column, value, request) + rescue ArgumentError => error + raise InvalidValue.new(column, value, request) + end + end + end end end Added: test/command/suite/add/invalid-integer.expected (+20 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/add/invalid-integer.expected 2013-12-26 17:24:50 +0900 (22668ef) @@ -0,0 +1,20 @@ +[ + "droonga.message", + 0, + { + "inReplyTo": null, + "statusCode": 400, + "type": "add.result", + "body": { + "name": "InvalidValue", + "message": "The column \"age\" cannot store the value \"secret\".", + "detail": { + "table": "User", + "key": "key", + "values": { + "age": "secret" + } + } + } + } +] Copied: test/command/suite/add/invalid-integer.test (+0 -0) 100% =================================================================== Added: test/command/suite/add/invalid-time.expected (+20 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/add/invalid-time.expected 2013-12-26 17:24:50 +0900 (3d7bc78) @@ -0,0 +1,20 @@ +[ + "droonga.message", + 0, + { + "inReplyTo": null, + "statusCode": 400, + "type": "add.result", + "body": { + "name": "InvalidValue", + "message": "The column \"birthday\" cannot store the value \"today\".", + "detail": { + "table": "User", + "key": "key", + "values": { + "birthday": "today" + } + } + } + } +] Renamed: test/command/suite/add/invalid-time.test (+0 -0) 100% =================================================================== Deleted: test/command/suite/add/mismatched-value-type.expected (+0 -13) 100644 =================================================================== --- test/command/suite/add/mismatched-value-type.expected 2013-12-26 17:03:51 +0900 (deb5f1b) +++ /dev/null @@ -1,13 +0,0 @@ -[ - "droonga.message", - 0, - { - "inReplyTo": null, - "statusCode": 400, - "type": "add.result", - "body": { - "name": "MismatchedValueType", - "message": "" - } - } -] Modified: test/command/suite/add/unknown-column.expected (+8 -1) =================================================================== --- test/command/suite/add/unknown-column.expected 2013-12-26 17:03:51 +0900 (fa4e923) +++ test/command/suite/add/unknown-column.expected 2013-12-26 17:24:50 +0900 (81c5a38) @@ -7,7 +7,14 @@ "type": "add.result", "body": { "name": "UnknownColumn", - "message": "The column \"unknown\" does not exist in the table \"User\"" + "message": "The column \"unknown\" does not exist in the table \"User\"", + "detail": { + "table": "User", + "key": "key", + "values": { + "unknown": "unknown" + } + } } } ] -------------- next part -------------- HTML����������������������������...Download