Skip to content

Commit

Permalink
fix: validate schema at compile time
Browse files Browse the repository at this point in the history
chore: more benchmarks
  • Loading branch information
zachdaniel committed Aug 14, 2024
1 parent 63dd620 commit aa90f7b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
41 changes: 39 additions & 2 deletions benchmarks/options.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,57 @@ schema = [
]
]

new_schema = Spark.Options.new!(schema)

defmodule MySchema do
use Spark.Options.Validator, schema: schema
end

# prime
Spark.Options.validate!([foo: "foo", bar: "bar", baz: 20], schema)
Spark.Options.validate!([foo: "foo", bar: "bar", baz: 20], new_schema)
MySchema.validate!([foo: "foo", bar: "bar", baz: 20])

Benchee.run(
%{
"existing" => fn ->
Spark.Options.validate!([foo: "foo", bar: "bar", baz: 20], schema)
options = Spark.Options.validate!([foo: "foo", bar: "bar", baz: 20], schema)
_foo = options[:foo]
_bar = options[:bar]
_foo = options[:foo]
_bar = options[:bar]
_foo = options[:foo]
_bar = options[:bar]
end,
"existing with built schema" => fn ->
options = Spark.Options.validate!([foo: "foo", bar: "bar", baz: 20], new_schema)

_foo = options[:foo]
_bar = options[:bar]
_foo = options[:foo]
_bar = options[:bar]
_foo = options[:foo]
_bar = options[:bar]
end,
"validator" => fn ->
MySchema.validate!([foo: "foo", bar: "bar", baz: 20]) |> MySchema.to_options()
%MySchema{} = options = MySchema.validate!([foo: "foo", bar: "bar", baz: 20])
_foo = options.foo
_bar = options.bar
_foo = options.foo
_bar = options.bar
_foo = options.foo
_bar = options.bar
end,
"validator to options" => fn ->
options = MySchema.validate!([foo: "foo", bar: "bar", baz: 20]) |> MySchema.to_options()

_foo = options[:foo]
_bar = options[:bar]
_foo = options[:foo]
_bar = options[:bar]
_foo = options[:foo]
_bar = options[:bar]
_foo = options[:foo]
_bar = options[:bar]
end
})
1 change: 1 addition & 0 deletions lib/spark/options/validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ defmodule Spark.Options.Validator do

[
quote bind_quoted: [schema: schema, define_deprecated_access?: define_deprecated_access?] do
schema = Spark.Options.new!(schema).schema
@schema Keyword.new(schema)

struct_fields =
Expand Down

0 comments on commit aa90f7b

Please sign in to comment.