Skip to content

Container attributes with opts can't compile with only one contained attribute #18

Description

@bmurphy23

The Speck compiler can fail to compile a seemingly valid schema if a handful of conditions are met for an attribute:

  • The attribute is a container: attribute :foo do or attribute [:foo] do
  • The attribute has opts: optional: true, default: %{}, etc.
  • The attribute only contains one attribute inside itself

Then Speck will fail to compile the schema with an error that looks like this:

Compiling protocol/foo/bar.ex
error: undefined function attribute/2 (there is no such import)
└─ nofile:10

Example schema:

struct Foo.Bar

name "foo_bar"

attribute :top_level do
  attribute :unix_timestamp, :integer

  attribute [:data_points], optional: true do
    attribute :value, :integer
  end
end

The problem occurs because attribute [:data_points], optional: true do only contains one attribute :value. If another element were added inside, Speck would compile it correctly.

While this schema should probably be altered to read attribute :data_points, [:integer], optional: true, there's no guarantee that some external, third-party API is as optimized as it could be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions