Module: Aliyun::Log::Record::TypeCasting

Defined in:
lib/aliyun/log/record/type_casting.rb

Defined Under Namespace

Modules: Registry Classes: BigDecimalType, DateTimeType, DateType, FloatType, IntegerType, JsonType, StringType, Value

Constant Summary collapse

TYPE_MAPPING =
{
  text: :string,
  long: :integer,
  double: :float,
  json: :json
}.freeze

Class Method Summary collapse

Class Method Details

.cast_field(value, options) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/aliyun/log/record/type_casting.rb', line 16

def self.cast_field(value, options)
  options ||= {}
  type = options[:cast_type]
  type ||= TYPE_MAPPING[options[:type]]

  return value if options.nil?
  return nil if value.nil?

  caster = Registry.lookup(type)
  raise ArgumentError, "Unknown type #{options[:type]}" if caster.nil?

  caster.new(options).cast(value)
end

.dump_field(value, options) ⇒ Object

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aliyun/log/record/type_casting.rb', line 30

def self.dump_field(value, options)
  options ||= {}
  type = options[:cast_type]
  type ||= TYPE_MAPPING[options[:type]]

  return value if options.nil?
  return nil if value.nil?

  dumper = Registry.lookup(type)
  raise ArgumentError, "Unknown type #{options[:type]}" if dumper.nil?

  dumper.new(options).dump(value)
end