Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move work to compiler #6

Merged
merged 4 commits into from
Jun 7, 2023
Merged

Conversation

tverlaan
Copy link
Contributor

@tverlaan tverlaan commented Jun 5, 2023

Previously dicts were parsed and stored in ETS on application startup. This commit introduces modules and functions to get vendors, attributes and values. This saves time during startup of the application. Secondly this opens the possibility to offer a set of macros that will replace the call with the correct value at compile time instead of the encoding phase at runtime. This should also decrease the amount of times you run into EntryNotFoundError by for example misspelling.

nimble_parsec is compiled into a module so that users of the library don't have to add nimble_parsec as a dep themselves.

The dictionaries are moved to 'dicts' so they will also not be added automatically to a release (I updated mix.exs)

I can see this is a relatively big change. Please don't hesitate to leave any feedback or ask any questions.

Previously dicts were parsed and stored in ETS on application startup.
This commit introduces modules to get vendors, attributes and values.
This saves time during startup of the application. Secondly this opens
the possibility to offer a set of macros that will replace the macro
with the correct value at compile time instead of the encoding phase at
runtime. This should also decrease the amount of times you run into
EntryNotFoundError.

nimble_parsec is compiled into a module so that users of the library
don't have to add nimble_parsec as a dep themselves.
@bearice
Copy link
Owner

bearice commented Jun 6, 2023

This is really a big change, thanks for the work.
I have two questions:

  1. will this patch compilable with the old API ? or say, will the old version example.exs runs without changing?
  2. could the new parser work at runtime so that users can update their dicts without recompiling?

@tverlaan
Copy link
Contributor Author

tverlaan commented Jun 6, 2023

  1. Yes it is compatible with the old API. You can confirm this in the tests. I updated example.exs to guide people to the macro's as it should perform better at runtime.
  2. No this won't work. I wouldn't know a usecase where that would be necessary?

Come to think of it, with the current default dictionary list we don't parse as many dictionaries as before. Perhaps the default should be all the dictionaries that are included in the main dictionary file?

@bearice
Copy link
Owner

bearice commented Jun 6, 2023

I'm asking because I had some private dicts, I can just load them at runtime without touching this library, but I don't see how can I do this after this patch .

@tverlaan
Copy link
Contributor Author

tverlaan commented Jun 6, 2023

Would it help if I add an option where you can provide the path to your own dicts? They would need to be available at compile time but they will be compiled into the same module as the other dicts.

@bearice
Copy link
Owner

bearice commented Jun 6, 2023

this might work, also we should ensure the same dictionaries is loaded by default as before so that it wont break users that assumes this.

@tverlaan
Copy link
Contributor Author

tverlaan commented Jun 6, 2023

Done! :)

The only thing that concerns me about this default behaviour is that there are quite a few overlapping values which generates the following warning which can't be avoided:
warning: this clause cannot match because a previous clause at line 76 always matches
This was also happening with the previous version, but silently..

It isn't nice to see this on first compilation. Perhaps this can be reconsidered with a v2?

lookup!(name: {vendor, attr, name})
end
for attribute <- attributes do
attr_fun_name = "attr_#{attribute.name}" |> String.replace("-", "_") |> String.to_atom()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: String.replace("-", "_") should be extract as a mangle_name function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, in general I think this module needs to be cleaned up and a bit more DRY. However you would have to create the function before calling it at compile time.

If it's okay with you I'd rather merge and then polish in smaller chunks.

lib/radius/dict.ex Outdated Show resolved Hide resolved
example.exs Show resolved Hide resolved
Copy link
Owner

@bearice bearice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bearice bearice merged commit 057d46f into bearice:master Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants