class Avro::Schema
Constants
- INT_MAX_VALUE
- INT_MIN_VALUE
- LONG_MAX_VALUE
- LONG_MIN_VALUE
- NAMED_TYPES
- NAMED_TYPES_SYM
- PRIMITIVE_TYPES
Sets of strings, for backwards compatibility. See below for sets of symbols, for better performance.
- PRIMITIVE_TYPES_SYM
- VALID_TYPES
- VALID_TYPES_SYM
Attributes
logical_type[R]
type_sym[R]
Public Class Methods
new(type, logical_type=nil)
click to toggle source
# File lib/avro/schema.rb 104 def initialize(type, logical_type=nil) 105 @type_sym = type.is_a?(Symbol) ? type : type.to_sym 106 @logical_type = logical_type 107 end
parse(json_string)
click to toggle source
# File lib/avro/schema.rb 37 def self.parse(json_string) 38 real_parse(MultiJson.load(json_string), {}) 39 end
real_parse(json_obj, names=nil, default_namespace=nil)
click to toggle source
Build Avro
Schema
from data parsed out of JSON string.
# File lib/avro/schema.rb 42 def self.real_parse(json_obj, names=nil, default_namespace=nil) 43 if json_obj.is_a? Hash 44 type = json_obj['type'] 45 logical_type = json_obj['logicalType'] 46 raise SchemaParseError, %Q(No "type" property: #{json_obj}) if type.nil? 47 48 # Check that the type is valid before calling #to_sym, since symbols are never garbage 49 # collected (important to avoid DoS if we're accepting schemas from untrusted clients) 50 unless VALID_TYPES.include?(type) 51 raise SchemaParseError, "Unknown type: #{type}" 52 end 53 54 type_sym = type.to_sym 55 if PRIMITIVE_TYPES_SYM.include?(type_sym) 56 return PrimitiveSchema.new(type_sym, logical_type) 57 58 elsif NAMED_TYPES_SYM.include? type_sym 59 name = json_obj['name'] 60 namespace = json_obj.include?('namespace') ? json_obj['namespace'] : default_namespace 61 case type_sym 62 when :fixed 63 size = json_obj['size'] 64 return FixedSchema.new(name, namespace, size, names, logical_type) 65 when :enum 66 symbols = json_obj['symbols'] 67 return EnumSchema.new(name, namespace, symbols, names) 68 when :record, :error 69 fields = json_obj['fields'] 70 return RecordSchema.new(name, namespace, fields, names, type_sym) 71 else 72 raise SchemaParseError.new("Unknown named type: #{type}") 73 end 74 75 else 76 case type_sym 77 when :array 78 return ArraySchema.new(json_obj['items'], names, default_namespace) 79 when :map 80 return MapSchema.new(json_obj['values'], names, default_namespace) 81 else 82 raise SchemaParseError.new("Unknown Valid Type: #{type}") 83 end 84 end 85 86 elsif json_obj.is_a? Array 87 # JSON array (union) 88 return UnionSchema.new(json_obj, names, default_namespace) 89 elsif PRIMITIVE_TYPES.include? json_obj 90 return PrimitiveSchema.new(json_obj) 91 else 92 raise UnknownSchemaError.new(json_obj) 93 end 94 end
validate(expected_schema, logical_datum, encoded = false)
click to toggle source
Determine if a ruby datum is an instance of a schema
# File lib/avro/schema.rb 97 def self.validate(expected_schema, logical_datum, encoded = false) 98 SchemaValidator.validate!(expected_schema, logical_datum, encoded) 99 true 100 rescue SchemaValidator::ValidationError 101 false 102 end
Public Instance Methods
==(other, seen=nil)
click to toggle source
# File lib/avro/schema.rb 144 def ==(other, seen=nil) 145 other.is_a?(Schema) && type_sym == other.type_sym 146 end
be_read?(other_schema)
click to toggle source
# File lib/avro/schema.rb 136 def be_read?(other_schema) 137 other_schema.read?(self) 138 end
hash(seen=nil)
click to toggle source
# File lib/avro/schema.rb 148 def hash(seen=nil) 149 type_sym.hash 150 end
md5_fingerprint()
click to toggle source
Returns the MD5 fingerprint of the schema as an Integer.
# File lib/avro/schema.rb 121 def md5_fingerprint 122 parsing_form = SchemaNormalization.to_parsing_form(self) 123 Digest::MD5.hexdigest(parsing_form).to_i(16) 124 end
mutual_read?(other_schema)
click to toggle source
# File lib/avro/schema.rb 140 def mutual_read?(other_schema) 141 SchemaCompatibility.mutual_read?(other_schema, self) 142 end
read?(writers_schema)
click to toggle source
# File lib/avro/schema.rb 132 def read?(writers_schema) 133 SchemaCompatibility.can_read?(writers_schema, self) 134 end
sha256_fingerprint()
click to toggle source
Returns the SHA-256 fingerprint of the schema as an Integer.
# File lib/avro/schema.rb 127 def sha256_fingerprint 128 parsing_form = SchemaNormalization.to_parsing_form(self) 129 Digest::SHA256.hexdigest(parsing_form).to_i(16) 130 end
subparse(json_obj, names=nil, namespace=nil)
click to toggle source
# File lib/avro/schema.rb 152 def subparse(json_obj, names=nil, namespace=nil) 153 if json_obj.is_a?(String) && names 154 fullname = Name.make_fullname(json_obj, namespace) 155 return names[fullname] if names.include?(fullname) 156 end 157 158 begin 159 Schema.real_parse(json_obj, names, namespace) 160 rescue => e 161 raise e if e.is_a? SchemaParseError 162 raise SchemaParseError, "Sub-schema for #{self.class.name} not a valid Avro schema. Bad schema: #{json_obj}" 163 end 164 end
to_avro(names=nil)
click to toggle source
# File lib/avro/schema.rb 166 def to_avro(names=nil) 167 props = {'type' => type} 168 props['logicalType'] = logical_type if logical_type 169 props 170 end
to_s()
click to toggle source
# File lib/avro/schema.rb 172 def to_s 173 MultiJson.dump to_avro 174 end
type()
click to toggle source
Returns the type as a string (rather than a symbol), for backwards compatibility. Deprecated in favor of {#type_sym}.
# File lib/avro/schema.rb 114 def type; @type_sym.to_s; end
type_adapter()
click to toggle source
# File lib/avro/schema.rb 116 def type_adapter 117 @type_adapter ||= LogicalTypes.type_adapter(type, logical_type) || LogicalTypes::Identity 118 end