Skip to content

Commit

Permalink
be a bit stricter
Browse files Browse the repository at this point in the history
  • Loading branch information
oxg558 committed Feb 7, 2025
1 parent 0cc0d4a commit ec801da
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/avrogen/avro/types/record/field.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ defmodule Avrogen.Avro.Types.Record.Field do
# The only way to differentiate between "default is nil because there isn't one" and "default is actually null" is to
# check if the type is also a union with null.
def has_default?(%__MODULE__{default: nil, type: %Types.Union{} = union}),
do: Types.Union.has_member?(union, Types.Primitive.null())
do: Types.Union.optional_by_convention?(union)

def has_default?(%__MODULE__{default: nil}), do: false
def has_default?(%__MODULE__{}), do: true
Expand Down
5 changes: 5 additions & 0 deletions lib/avrogen/avro/types/union.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Avrogen.Avro.Types.Union do
"""

alias Avrogen.Avro.Schema
alias Avrogen.Avro.Types.Primitive
use TypedStruct

typedstruct do
Expand All @@ -25,6 +26,10 @@ defmodule Avrogen.Avro.Types.Union do
def parse(value) when is_list(value), do: %__MODULE__{types: Enum.map(value, &Schema.parse/1)}

def has_member?(%__MODULE__{types: types}, type), do: Enum.member?(types, type)

# A union with null listed first is optional by convention (see moduledoc).
def optional_by_convention?(%__MODULE__{types: [first_type | _]}),
do: first_type == Primitive.null()
end

alias Avrogen.Avro.Schema.CodeGenerator
Expand Down

0 comments on commit ec801da

Please sign in to comment.