Class: Thrift::Types::Known::Any::Any

Inherits:
Object
  • Object
show all
Includes:
Struct, Struct_Union
Defined in:
lib/thrift/types/known/any.rb,
lib/thrift/types/known/any_types.rb,
lib/thrift/types/known/any_types.rb

Defined Under Namespace

Classes: CodecNotHandled, TypeNotHandled

Constant Summary collapse

NAME =
'Any'.freeze
NAMESPACE =
'types.known.any'.freeze
THRIFT_FIELD_INDEX_TYPE =
1
THRIFT_FIELD_INDEX_VALUE =
2
FIELDS =
{
  THRIFT_FIELD_INDEX_TYPE => {type: ::Thrift::Types::STRING, name: 'type'},
  THRIFT_FIELD_INDEX_VALUE => {type: ::Thrift::Types::STRING, name: 'value', binary: true}
}

Constants included from Struct_Union

Struct_Union::CONTAINER_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Struct

#<=>, #==, #differences, #eql?, field_accessor, #fields_with_default_values, generate_accessors, #hash, #initialize, #inspect, qmark_isset_method, #read, #write

Methods included from Struct_Union

#each_field, #field_info, #inspect_collection, #inspect_field, #is_container?, #name_to_id, #read_field, #sorted_field_ids, #write_container, #write_data

Class Method Details

.from_object(obj, codec_key = '') ⇒ Object

Raises:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/thrift/types/known/any.rb', line 132

def from_object(obj, codec_key = '')
  struct_def = Thrift::STRUCT_DEFINITIONS.values.find do |v|
    obj.is_a? v.klass
  end

  raise TypeNotHandled unless struct_def
  raise CodecNotHandled unless CODECS[codec_key]

  key = codec_key.eql?('') ? '' : "-#{codec_key}"

  Any.new(
    type:  "thrift#{key}/#{struct_def.struct_type}",
    value: CODECS[codec_key].encode(obj)
  )
end

Instance Method Details

#struct_fieldsObject



29
# File 'lib/thrift/types/known/any_types.rb', line 29

def struct_fields; FIELDS; end

#to_objectObject

Raises:



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/thrift/types/known/any.rb', line 149

def to_object
  codec_key, struct_type = parse_type

  raise TypeNotHandled unless Thrift::STRUCT_DEFINITIONS[struct_type]
  raise CodecNotHandled unless CODECS[codec_key]

  res = Thrift::STRUCT_DEFINITIONS[struct_type].klass.new
  CODECS[codec_key].decode(value, res)

  res
end

#validateObject



31
32
33
34
# File 'lib/thrift/types/known/any_types.rb', line 31

def validate
  raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Required field type is unset!') unless @type
  raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Required field value is unset!') unless @value
end