   defp encode_fields(fields, offset) do
     fields =
       for {name, value} <- fields do
-        field = named_field(name: name, data_type: :string)
-        value = encode_type(value, :string)
+        type = infer_type_from_term(value)
+        field = named_field(name: name, data_type: type)
+        value = encode_type(value, type)
         {field, value}
       end
 

        end
    end

    {fields_to_map(fields_and_values), rest}
  end

  defp fields_to_map(fields_and_values) do
    for {field, value} <- fields_and_values, into: %{} do
      {field_name(field), value}
    end
  end



  defp field_name(field) when is_record(field, :named_field), do: named_field(field, :name)

  defp infer_type_from_term(term) when is_binary(term),  do: :string
  defp infer_type_from_term(term) when is_integer(term), do: :sint32
  defp infer_type_from_term(term) when is_float(term),   do: :double
  defp infer_type_from_term(term) when is_boolean(term), do: :boolean
  defp infer_type_from_term(term) when is_list(term),    do: :embedded_list
  defp infer_type_from_term(term) when is_map(term),     do: :embedded_map

  # http://orientdb.com/docs/last/Types.html
  @types [
    boolean: 0,
    sint32: 1,
    sint16: 2,
    sint64: 3,
    float: 4,
    double: 5,
    datetime: 6,
    string: 7,
    binary: 8,
    embedded: 9,
    embedded_list: 10,
    embedded_set: 11,
    embedded_map: 12,
    link: 13,
    link_list: 14,
    link_set: 15,
    link_map: 16,
    byte: 17,
    transient: 18,
    date: 19,
    custom: 20,
    decimal: 21,
    link_bag: 22,
    any: 23,
  ]

  for {type_name, type_id} <- @types do
    defp int_to_type(unquote(type_id)), do: unquote(type_name)
    defp type_to_int(unquote(type_name)), do: unquote(type_id)
  end
end
