View Source ElixirAvro.Template.Spec (elixir_avro v0.1.0)

Module responsible for generating typespecs from Avro type.

Summary

Functions

Decides whether to enforce typedstruct types or not based on which avro type is being used.

Returns the string to be used as elixir type for any avro type.

Functions

@spec enforce?(ElixirAvro.AvroType.t()) :: boolean()

Decides whether to enforce typedstruct types or not based on which avro type is being used.

Link to this function

to_typedstruct_spec!(t, module_prefix)

View Source
@spec to_typedstruct_spec!(ElixirAvro.AvroType.t(), String.t()) ::
  String.t() | no_return()

Returns the string to be used as elixir type for any avro type.

Examples

Primitive types

iex> to_typedstruct_spec!(%Primitive{name: "boolean"}, "my_prefix") "boolean()"

iex> to_typedstruct_spec!(%Primitive{name: "bytes"}, "my_prefix") "binary()"

iex> to_typedstruct_spec!(%Primitive{name: "double"}, "my_prefix") "float()"

iex> to_typedstruct_spec!(%Primitive{name: "float"}, "my_prefix") "float()"

iex> to_typedstruct_spec!(%Primitive{name: "int"}, "my_prefix") "integer()"

iex> to_typedstruct_spec!(%Primitive{name: "long"}, "my_prefix") "integer()"

iex> to_typedstruct_spec!(%Primitive{name: "null"}, "my_prefix") "nil"

iex> to_typedstruct_spec!(%Primitive{name: "string"}, "my_prefix") "String.t()"

An unknown type will raise an ArgumentError:

iex> to_typedstruct_spec!(%Primitive{name: "non-existent-type"}, "my_prefix") ** (ArgumentError) unsupported type: "non-existent-type"

Logical types

iex> to_typedstruct_spec!(%Primitive{name: "bytes", custom_props: [ ...> %CustomProp{name: "logicalType", value: "decimal"}, %CustomProp{name: "precision", value: 4}, %CustomProp{name: "scale", value: 2} ...> ]}, ...> "my_prefix") "Decimal.t()"

iex> to_typedstruct_spec!(%Primitive{name: "string", custom_props: [%CustomProp{name: "logicalType", value: "uuid"}]}, "my_prefix") "String.t()"

iex> to_typedstruct_spec!(%Primitive{name: "int", custom_props: [%CustomProp{name: "logicalType", value: "date"}]}, "my_prefix") "Date.t()"

iex> to_typedstruct_spec!(%Primitive{name: "int", custom_props: [%CustomProp{name: "logicalType", value: "time-millis"}]}, "my_prefix") "Time.t()"

iex> to_typedstruct_spec!(%Primitive{name: "long", custom_props: [%CustomProp{name: "logicalType", value: "time-micros"}]}, "my_prefix") "Time.t()"

iex> to_typedstruct_spec!(%Primitive{name: "long", custom_props: [%CustomProp{name: "logicalType", value: "timestamp-millis"}]}, "my_prefix") "DateTime.t()"

iex> to_typedstruct_spec!(%Primitive{name: "long", custom_props: [%CustomProp{name: "logicalType", value: "timestamp-micros"}]}, "my_prefix") "DateTime.t()"

iex> to_typedstruct_spec!(%Primitive{name: "long", custom_props: [%CustomProp{name: "logicalType", value: "local-timestamp-millis"}]}, "my_prefix") "NaiveDateTime.t()"

iex> to_typedstruct_spec!(%Primitive{name: "long", custom_props: [%CustomProp{name: "logicalType", value: "local-timestamp-micros"}]}, "my_prefix") "NaiveDateTime.t()"

An unknown logical type or a non-existent {primitive, logical} type combination will raise an ArgumentError:

iex> to_typedstruct_spec!(%Primitive{name: "int", custom_props: [%CustomProp{name: "logicalType", value: "unsupported-logical-type"}]}, "my_prefix") ** (ArgumentError) unsupported type: {"int", "unsupported-logical-type"}

iex> to_typedstruct_spec!(%Primitive{name: "string", custom_props: [%CustomProp{name: "logicalType", value: "timestamp-millis"}]}, "my_prefix") ** (ArgumentError) unsupported type: {"string", "timestamp-millis"}

Fixed types

iex> totypedstruct_spec!(%Fixed{name: "md5", namespace: "test", aliases: [], size: 16, fullname: "test.md5"}, "my_prefix") "<<::128>>"

Array types

iex> to_typedstruct_spec!(%Array{type: %Primitive{name: "string"}}, "my_prefix") "[String.t()]"

iex> to_typedstruct_spec!(%Array{type: %Primitive{name: "int", custom_props: [%CustomProp{name: "logicalType", value: "date"}]}}, "my_prefix") "[Date.t()]"

Primitive types error logic still applies:

iex> to_typedstruct_spec!(%Array{type: %Primitive{name: "string", custom_props: [%CustomProp{name: "logicalType", value: "date"}]}}, "my_prefix") ** (ArgumentError) unsupported type: {"string", "date"}

Map types

iex> to_typedstruct_spec!(%Map{type: %Primitive{name: "int"}}, "my_prefix") "%{String.t() => integer()}"

iex> to_typedstruct_spec!(%Map{type: %Primitive{name: "int", custom_props: [%CustomProp{name: "logicalType", value: "time-millis"}]}}, "my_prefix") "%{String.t() => Time.t()}"

Primitive types error logic still applies:

iex> to_typedstruct_spec!(%Map{type: %Primitive{name: "string", custom_props: [%CustomProp{name: "logicalType", value: "date"}]}}, "my_prefix") ** (ArgumentError) unsupported type: {"string", "date"}

Union types

iex> to_typedstruct_spec!( ...> %Union{values: %{ ...> 0 => %Primitive{name: "null"}, ...> 1 => %Primitive{name: "string"} ...> }}, "my_prefix") "nil | String.t()"

iex> to_typedstruct_spec!( ...> %Union{values: %{ ...> 0 => %Primitive{name: "null"}, ...> 1 => %Primitive{name: "int", custom_props: [%CustomProp{name: "logicalType", value: "time-millis"}]} ...> }}, "my_prefix") "nil | Time.t()"

References

iex> to_typedstruct_spec!("test.Type", "my_prefix") "MyPrefix.Test.Type.t()"