Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional value where value is an enum generates warning that breaks compilation in case of --warnings-as-errors #24

Open
fusillicode opened this issue Feb 18, 2025 · 0 comments

Comments

@fusillicode
Copy link

fusillicode commented Feb 18, 2025

The following schema

   {
      "name": "foo",
      "type": [
        "null",
        {
          "type": "record",
          "name": "Foo",
          "fields": [
            {
              "name": "value",
              "type": {
                "type": "enum",
                "name": "FooValue",
                "symbols": [
                  "Bar",
                  "Unknown"
                ]
              },
              "default": "Unknown"
            }
          ]
        }
      ],
      "default": "null"
    }

generates the following warning

Compiling 207 files (.ex)
    warning: this clause cannot match because a previous clause at line 29 always matches
    │
 35 │   def from_avro_map(%{} = invalid) do
    │   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ lib/generated/foo.ex:35

The warning raises from the following generated code:

 @impl true
  def from_avro_map(%{} = avro_map) do
    value = avro_map["value"] || "Unknown"
    {:ok, %__MODULE__{value: decode_value(value)}}
  end

  @required_keys MapSet.new([])
  def from_avro_map(%{} = invalid) do
    actual = Map.keys(invalid) |> MapSet.new()
    missing = MapSet.difference(@required_keys, actual) |> Enum.join(", ")
    {:error, "Missing keys: " <> missing}
  end

  def from_avro_map(_) do
    {:error, "Expected a map."}
  end

The second definition of from_avro_map should be probably omitted in this scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant