gRPCs: How do you convert Elixir strings to protobuf :bytes?

I’m trying to send what is originally a string of "pub fun main(): Int { return 1 }" to a gRPC server as :bytes. :bytes is a protobuf data type. See the struct below for the generated Elixir module for the particular protobuf I’m working with.

Problem: How do you convert a binary into this expected format?

Easy enough if it’s a hex value like "F8D6E0586B0A20C7". I’d be able to do
Base.decode16!("F8D6E0586B0A20C7"). But with a complex string that has more than just hex-accepted values?

Generated protobuf module

defmodule Flow.Access.ExecuteScriptAtLatestBlockRequest do
  @moduledoc false
  use Protobuf, syntax: :proto3

  @type t :: %__MODULE__{
          script: binary,
          arguments: [binary]
        }

  defstruct [:script, :arguments]

  field :script, 1, type: :bytes
  field :arguments, 2, repeated: true, type: :bytes
end

gRPC and protobuf libs:


Just pass it as is. Elixir strings are already binaries.

1 Like

I’m aware and have tried to pass it along. I believe the issue was that I needed to encode the string in a format that the consumer required.

Hey @dangdennis !

How did you end up solving this issue? I am experiencing the same issue :).

Cheers!

In my particular scenario, I had to do

Base.decode16!("f8d6e0586b0a20c7", case: :mixed)