lib/scrimp/json_thrift.rb
# Converts +value+ to a Thrift type that can be passed to a Thrift setter for the given +field+.
#
# @param value [Object] the parsed json type to be converted
# @param field [Hash] the Thrift field that will accept +value+
# @return [Object] +value+ converted to a type that the setter for +field+ will expect
# @raise [Thrift::TypeError] if +field+ has an invalid type
Converts +value+ to a Thrift type that can be passed to a Thrift setter for the given +field+.
Converts + value + to a Thrift type that can be passed to a Thrift setter for the given + field + .
def json_type_to_thrift_type(value, field)
      type = Thrift.type_name(field[:type])
      raise Thrift::TypeError.new("Type for #{field.inspect} not found.") unless type
      case type.sub('Types::', '')
      when 'STRUCT'
        field[:class].from_hash(value)
      when 'MAP'
        value.each_with_object({}) do |(k, v), h|
          thrift_k = json_type_to_thrift_type(k, field[:key])
          thrift_v = json_type_to_thrift_type(v, field[:value])
          h[thrift_k] = thrift_v
        end
      when 'LIST'
        value.map { |e| json_type_to_thrift_type(e, field[:element]) }
      when 'SET'
        value.map { |e| json_type_to_thrift_type(e, field[:element]) }.to_set
      when 'DOUBLE'
        value.to_f
      when 'VOID'
        nil
      else 
        (field[:enum_class] && field[:enum_class]::VALUE_MAP.key(value)) || value
      end
    end
def json_type_to_thrift_type ( value , field ) type = Thrift . type_name ( field [ :type ] ) raise Thrift :: TypeError . new ( "Type for #{field.inspect} not found." ) unless type case type . sub ( 'Types::' , '' ) when 'STRUCT' field [ :class ] . from_hash ( value ) when 'MAP' value . each_with_object ( { } ) do | ( k , v ) , h | thrift_k = json_type_to_thrift_type ( k , field [ :key ] ) thrift_v = json_type_to_thrift_type ( v , field [ :value ] ) h [ thrift_k ] = thrift_v end when 'LIST' value . map { | e | json_type_to_thrift_type ( e , field [ :element ] ) } when 'SET' value . map { | e | json_type_to_thrift_type ( e , field [ :element ] ) } . to_set when 'DOUBLE' value . to_f when 'VOID' nil else ( field [ :enum_class ] && field [ :enum_class ] :: VALUE_MAP . key ( value ) ) || value end end
Converts +value+ to a Thrift type that can be passed to a Thrift setter for the given +field+.
Converts + value + to a Thrift type that can be passed to a Thrift setter for the given + field + .
# JSON doesn't allow arbitrary keys, so maps are sent as [k, v] pairs # TODO: STOP
+value+ converted to a type that the setter for +field+ will expect
+ value + converted to a type that the setter for + field + will expect
Object
[Thrift::TypeError] if +field+ has an invalid type
[ Thrift :: TypeError ] if + field + has an invalid type
value
the parsed json type to be converted
the parsed json type to be converted
field
the Thrift field that will accept +value+
the Thrift field that will accept + value +