Class: TypedData::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/typed_data/schema.rb,
lib/typed_data/schema/type.rb,
lib/typed_data/schema/errors.rb,
lib/typed_data/schema/int_type.rb,
lib/typed_data/schema/map_type.rb,
lib/typed_data/schema/enum_type.rb,
lib/typed_data/schema/long_type.rb,
lib/typed_data/schema/null_type.rb,
lib/typed_data/schema/array_type.rb,
lib/typed_data/schema/bytes_type.rb,
lib/typed_data/schema/float_type.rb,
lib/typed_data/schema/union_type.rb,
lib/typed_data/schema/record_type.rb,
lib/typed_data/schema/string_type.rb,
lib/typed_data/schema/boolean_type.rb

Defined Under Namespace

Classes: ArrayType, BooleanType, BytesType, EnumType, FloatType, IntType, InvalidValue, LongType, MapType, NullType, RecordType, StringType, Type, UnionType, UnknownField, UnsupportedType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • schema (Hash)

    an Avro schema



72
73
74
75
# File 'lib/typed_data/schema.rb', line 72

def initialize(schema)
  @schema = deep_symbolize_keys(schema)
  @root_type = Schema.build_type(@schema)
end

Instance Attribute Details

#root_typeObject (readonly)

Returns the value of attribute root_type.



69
70
71
# File 'lib/typed_data/schema.rb', line 69

def root_type
  @root_type
end

Class Method Details

.build_type(type, logical_type = nil) ⇒ Object

Parameters:

  • type (String, Hash{Symbol => Object}, Array<Hash{Symbol => Object}>)
  • logical_type (String, nil) (defaults to: nil)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/typed_data/schema.rb', line 21

def build_type(type, logical_type = nil)
  type = type.first if type.is_a?(Array) && type.size == 1

  case type
  when Array
    UnionType.new(type)
  when Hash
    actual_type = type[:type]
    if type[:logicalType]
      return build_type(actual_type, type[:logicalType])
    end

    case actual_type
    when "enum"
      EnumType.new(type[:name], type[:symbols])
    when "fixed"
      BytesType.new(type[:name] || "bytes")
    when "array"
      items = type[:items]
      ArrayType.new(items.is_a?(Array) ? items : [items])
    when "map"
      values = type[:values]
      MapType.new(values.is_a?(Array) ? values : [values])
    when "record"
      RecordType.new(type[:name], type[:fields])
    else
      raise UnsupportedType, "Unknown type: #{actual_type}"
    end
  when "boolean"
    BooleanType.new(type, logical_type)
  when "int"
    IntType.new(type, logical_type)
  when "long"
    LongType.new(type, logical_type)
  when "float", "double"
    FloatType.new(type, logical_type)
  when "bytes"
    BytesType.new(type, logical_type)
  when "string"
    StringType.new(type, logical_type)
  when "null"
    NullType.new(type, logical_type)
  else
    raise UnsupportedType, "Unknown type: #{type}"
  end
end