0
votes

after searching, I couldn't find how to get the type of parameters in an erlang module. For more clear, this is what I'm trying to do:

  • 1/ show all loaded erlang modules by: erlang:loaded() -> this returns a list of atoms which are modules names
  • 2/ show all exported functions for a module by this: Module:module_info(exports) -> this returns a list of tuples which are likely this: {function_name, number_of_parameters)
  • 3/ show parameter types in the above founded function -> I got stuck at this ...

An example:

  • Input: application:load("1", 2, "3", {123, "2\"{[1234", 123}, ad, <<"asa]}\">>\", 2, 300:16>>).
  • Output:

    • Module_Name: application
    • Function_Name: load
    • Number of params: 6
    • Param lists:

      • "1" ---> type: string
      • 2 ---> type: integer
      • "3" ---> type: string
      • {123, "2\"{[1234", 123} ---> type: tuple`
      • ad ---> type: atom
      • <<"asa]}\">>\", 2, 300:16>> ---> type: binary

Solved

my problem was solved at this: erlang parse string to data types using regex

1
Never looked for this before, but I think typer is the only tool I know of that does what you want, but it is not built for runtime introspection. github.com/erlang/otp/blob/master/lib/dialyzer/src/typer.erl Maybe someone else knows? Dialyzer is a permissive inspector, so unless there are typespecs for all the functions (and they are accurate) this sort of information may not be very useful in practice. - zxq9
thank you, I'm working on a server-client project on which the server works as an erlang library that receives requests from clients, likely: "erlang:length("abc")", and returns the result like this: 3 But there's a problem related to parsing the request from client, which is this question. - zoro
I just happened to mess with converting text to Erlang terms on the fly a few weeks ago, but the solution is not reliable for arbitrary code and, of course, accepting arbitrary code to execute is super dangerous. If you have the freedom to design the input language itself (and, of course, depending on the nature of the project) it might make more sense to write an S-expression interpreter for a subset of language terms rather than accepting any arbitrary Erlang term to run on the fly. Another alternative could be passing to a (very) limited shell session. - zxq9
I found a comma-regex-parser and trying to make it clear at here: stackoverflow.com/questions/47053384/… - zoro

1 Answers

0
votes

I'm not aware that this is possible, the way you describe parameters in Erlang is using spec annotations in your code, but not all the code out there is well documented and using specs like:

-spec Module:Function(ArgType1, ..., ArgTypeN) -> ReturnType.

Also, a function can take different argument types and use guards for different handling of the input. Or even use syntactic sugar on records to create custom types or use any(). Dialyzer is the only thing that can help, perhaps checking dialyzer:plt_info(Plt)