lib/fluent/plugin/bigquery/schema.rb in fluent-plugin-bigquery-0.3.4 vs lib/fluent/plugin/bigquery/schema.rb in fluent-plugin-bigquery-0.4.0

- old
+ new

@@ -1,5 +1,7 @@ +require 'multi_json' + module Fluent module BigQuery class FieldSchema def initialize(name, mode = :nullable) unless [:nullable, :required, :repeated].include?(mode) @@ -54,11 +56,15 @@ def type :string end def format_one(value) - value.to_s + if value.is_a?(Hash) || value.is_a?(Array) + MultiJson.dump(value) + else + value.to_s + end end end class IntegerFieldSchema < FieldSchema def type @@ -114,16 +120,61 @@ value end end end + class DateFieldSchema < FieldSchema + def type + :date + end + + def format_one(value) + if value.respond_to?(:strftime) + value.strftime("%Y-%m-%d") + else + value + end + end + end + + class DateTimeFieldSchema < FieldSchema + def type + :datetime + end + + def format_one(value) + if value.respond_to?(:strftime) + value.strftime("%Y-%m-%dT%H:%M:%S.%6L") + else + value + end + end + end + + class TimeFieldSchema < FieldSchema + def type + :time + end + + def format_one(value) + if value.respond_to?(:strftime) + value.strftime("%H:%M:%S.%6L") + else + value + end + end + end + class RecordSchema < FieldSchema FIELD_TYPES = { string: StringFieldSchema, integer: IntegerFieldSchema, float: FloatFieldSchema, boolean: BooleanFieldSchema, timestamp: TimestampFieldSchema, + date: DateFieldSchema, + datetime: DateTimeFieldSchema, + time: TimeFieldSchema, record: RecordSchema }.freeze def initialize(name, mode = :nullable) super(name, mode)