Skip to content

Storing binary data in database is causing a proto to be changed #177

@jaronoff97

Description

@jaronoff97

i have a custom type that i'm attempting to serialize a protobuf to a binary and store in the DB. I tried using the binary type, but moved to the string type as that seemed more fitting some of the examples I saw. I'm getting a really odd error though when i try to load from the DB. It appears as though the bytes stored in the database have been modified.


  use Ecto.Type
  @impl true
  ch_string = Ecto.ParameterizedType.init(Ch, type: "String")
  def type, do: unquote(Macro.escape(ch_string))

  @doc """
  Convert the map from the database back to
  the desired term.
  """
  @impl true
  def load(term) when is_binary(term) do
    IO.inspect(term, label: "Loaded binary term")
    IO.inspect(byte_size(term), label: "Loaded term byte size")

    case MyModule.MyObject.decode(term) do
      {:ok, decoded_term} ->
        IO.inspect(decoded_term, label: "Decoded term")
        IO.inspect(byte_size(decoded_term), label: "Decoded term byte size")
        {:ok, decoded_term}

      {:error, reason} ->
        IO.puts("Decoding error: #{reason}")
        {:error, reason}
    end
  end

  @doc """
  Converting the data structure to map for storage.
  """
  @impl true
  def dump(term) do
    encoded_term = MyModule.MyObject.encode(term)
    IO.inspect(term, label: "Term to be encoded")
    IO.inspect(encoded_term, label: "Encoded term")
    IO.inspect(byte_size(encoded_term), label: "Encoded term byte size")
    {:ok, encoded_term}
  end

And i get this when trying to load:

Encoded term: <<10, 136, 11, 10, 133, 11, 18, 130, 11, 10, 255, 10, 101, 120, 112, 111, 114,
  116, 101, 114, 115, 58, 10, 32, 32, 32, 32, 99, 108, 105, 99, 107, 104, 111,
  117, 115, 101, 58, 10, 32, 32, 32, 32, 32, 32, 32, 32, 99, 114, 101, ...>>
Encoded term byte size: 1419
Loaded binary term: <<10, 239, 191, 189, 11, 10, 239, 191, 189, 11, 18, 239, 191, 189, 11, 10, 239,
  191, 189, 10, 101, 120, 112, 111, 114, 116, 101, 114, 115, 58, 10, 32, 32, 32,
  32, 99, 108, 105, 99, 107, 104, 111, 117, 115, 101, 58, 10, 32, 32, 32, ...>>
Loaded term byte size: 1427
** (Protobuf.DecodeError) insufficient data decoding field config_map, expected <<10, 239, 191, 189, 11, 18, 239, 191, 189, 11, 10, 239, 191, 189, 10, 101, 120, 112, 111, 114, 116, 101, 114, 115, 58, 10, 32, 32, 32, 32, 99, 108, 105, 99, 107, 104, 111, 117, 115, 101, 58, 10, 32, 32, 32, 32, 32, 32, 32, 32, ...>> to be at least 24076271 bytes

I'd love to know what I'm doing wrong here, or if the library is doing something specific to encode strings?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions