alexocode / knigge Goto Github PK
View Code? Open in Web Editor NEWAn opinionated way of dealing with behaviours
License: MIT License
An opinionated way of dealing with behaviours
License: MIT License
In a nutshell: the config from config/config.exs
is not available for packages, which makes configuring Knigge via it not possible for packages.
A potential solution might be to provide a "default" implementation, which can be overridden via the config.
Workaround is something like this:
knigge_opts =
if Mix.env() == :prod do
[implementation: MyProdImplementation]
else
[otp_app: :my_app]
end
use Knigge, knigge_opts
Currently the formatter adds parens around a one-line defdefault
; this could be disabled by adding defdefault: 2
as an exported locals_without_parents
.
Currently, there are three areas, the Knigge.Warnings
and Knigge.Options
modules using the native IO
module, and another in the Knigge.CLI.Output
module using the third party Bunt
library.
I think it would be better to utilize the Logger
module directly to ensure logs go through to whatever logging backend the users utilize, at least (especially?) in the non CLI modules.
What do you think @sascha-wolf?
There are cases where it's useful to swap an implantation out for a single test. As such an easy to use API for this might be useful to add.
Currently argument defaults in defdefault
are a bit wonky. Knigge
should either explicitly allow default values or forbid them.
When a @callback
in the behaviour is defined without brackets (as is valid if the function doesn't require any arguments), compilition fails. Example Behaviour:
defmodule MyApp.Rules.Parser do
use Knigge, otp_app: :my_app
@callback parse! :: %{String.t() => Dataset.t()}
end
This results in the following error at compiletime:
== Compilation error in file lib/my_app/rules/parser.ex ==
** (ArgumentError) argument error
:erlang.length(nil)
(knigge 1.2.0) lib/knigge/ast.ex:13: Knigge.AST.function_spec_from_callback/1
(elixir 1.11.1) lib/enum.ex:1399: Enum."-map/2-lists^map/1-0-"/2
(knigge 1.2.0) lib/knigge/behaviour.ex:33: Knigge.Behaviour.callbacks_from_attribute/1
(knigge 1.2.0) lib/knigge/code.ex:42: Knigge.Code.generate/2
(knigge 1.2.0) expanding macro: Knigge.Code.__before_compile__/1
lib/authorizer/rules/parser.ex:1: Authorizer.Rules.Parser (module)
(elixir 1.11.1) lib/kernel/parallel_compiler.ex:314: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
Changing the callback to parse!()
will fix the error.
In the following example:
defmodule MyGreatcheckBehaviour do
use Knigge, otp_app: :my_application
@callback my_great_callback(my_argument :: any()) :: any()
end
defmodule MyGreatImplementation do
@behaviour MyGreatBehaviour
@impl true
def my_great_callback(_), do: true
end
# config.exs
config :my_application, MyGreatBehaviour, MyGreatImplementation
It doesn't work in dev
or prod
because MyGreatImplementation
depends on MyGreatBehaviour
on compilation time, and Knigge
tries to Code.load?(MyGreatImplementation)
when compiling MyGreatBehaviour
.
Now, I'm using the check_if_exists?: false
option, however, it is not explained in the README and this example doesn't work without it. Another solution would be to use a third module for the behaviour, but if the main purpose is to avoid boilerplate code I don't think it is a great idea.
I don't know if it would be possible to try to check if the implementation exists in the @after_compile
hook using the Code.ensure_compile(implementation_module)
Thank you!
Just a heads up. In order to get my project to compile when using this library in combination with Mox, I had to set check_if_exists?
option to false
.
I have my mock defined in /test/support/mocks.ex
which is included in my elixirc_paths
, as recommended in the Mox documentation to provide compile-time support, but the project wouldn't compile when I use Knigge
unless I set that option.
That option isn't documented in the Knigge.Options
docs and I only found out about it looking at the tests you wrote for issue #2.
When specifying multiple "versions" for a single @callback
- for example with differing typespecs - Knigge
also generates multiple clauses which in turn generates a compiler warning.
Hello.
I'm facing some issue to integrate Knigge.
Bunt is a dependency used for the CLI, however, when you have to do some release upgrades, SASL release_handler
crash because there is no top supervisor for the bunt
application.
(See other discussion regarding this also: bitwalker/exrm#183)
An idea would be to tag the bunt
dependency only in dev or exclude it from the runtime.
This could for example with a typo happen.
There is a typo in the Options
module which prevents this library from working properly.
You are trying to read opt_app
from the options list instead of otp_app
.
Currently docs only get published on release.
It would be great if we could publish the docs for main
on each push/merge.
version
would these docs have?When configuring a module in test which only gets compiled during the tests the compiler will emit a bunch of warnings that the module can’t be found.
While there are some workarounds for this it would be nice if we could find a way to disable these warnings completely.
Elixir 1.10 will introduce a @compile {:no_warn_undefined, ...}
option but that will obviously only help for 1.10.
delegate_at: :runtime
per default in :test
This would require some changes into how we configure delegate_at
. Maybe deprecate it in favour of a delegate_at_runtime
option which can receive some environment specs like check_if_exists
can?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.