Class: Avro::Schema::BytesSchema

Inherits:
PrimitiveSchema show all
Defined in:
lib/avro/schema.rb

Constant Summary collapse

ERROR_INVALID_SCALE =
'Scale must be greater than or equal to 0'
ERROR_INVALID_PRECISION =
'Precision must be positive'
ERROR_PRECISION_TOO_SMALL =
'Precision must be greater than scale'

Constants inherited from Avro::Schema

CRC_EMPTY, DECIMAL_LOGICAL_TYPE, DEFAULT_VALIDATE_OPTIONS, INT_MAX_VALUE, INT_MIN_VALUE, LONG_MAX_VALUE, LONG_MIN_VALUE, NAMED_TYPES, NAMED_TYPES_SYM, NAME_REGEX, PRIMITIVE_TYPES, PRIMITIVE_TYPES_SYM, SINGLE_OBJECT_MAGIC_NUMBER, VALID_TYPES, VALID_TYPES_SYM

Instance Attribute Summary collapse

Attributes inherited from Avro::Schema

#logical_type, #type_sym

Instance Method Summary collapse

Methods inherited from Avro::Schema

#==, #be_read?, #crc_64_avro_fingerprint, #hash, #initFPTable, #md5_fingerprint, #mutual_read?, parse, #read?, real_parse, #sha256_fingerprint, #single_object_encoding_header, #single_object_schema_fingerprint, #subparse, #to_s, #type, #type_adapter, validate

Constructor Details

#initialize(type, logical_type = nil, precision = nil, scale = nil) ⇒ BytesSchema

Returns a new instance of BytesSchema.



494
495
496
497
498
499
500
501
# File 'lib/avro/schema.rb', line 494

def initialize(type, logical_type=nil, precision=nil, scale=nil)
  super(type.to_sym, logical_type)

  @precision = precision.to_i if precision
  @scale = scale.to_i if scale

  validate_decimal! if logical_type == DECIMAL_LOGICAL_TYPE
end

Instance Attribute Details

#precisionObject (readonly)

Returns the value of attribute precision.



492
493
494
# File 'lib/avro/schema.rb', line 492

def precision
  @precision
end

#scaleObject (readonly)

Returns the value of attribute scale.



492
493
494
# File 'lib/avro/schema.rb', line 492

def scale
  @scale
end

Instance Method Details

#match_schema?(schema) ⇒ Boolean

Returns:

  • (Boolean)


512
513
514
515
516
517
518
519
520
# File 'lib/avro/schema.rb', line 512

def match_schema?(schema)
  return true if super

  if logical_type == DECIMAL_LOGICAL_TYPE && schema.logical_type == DECIMAL_LOGICAL_TYPE
    return precision == schema.precision && (scale || 0) == (schema.scale || 0)
  end

  false
end

#to_avro(names = nil) ⇒ Object



503
504
505
506
507
508
509
510
# File 'lib/avro/schema.rb', line 503

def to_avro(names=nil)
  avro = super
  return avro if avro.is_a?(String)

  avro['precision'] = precision if precision
  avro['scale'] = scale if scale
  avro
end