Utility module for exporting %FunConfig{} to JSON format.
This module provides functions to generate JSON configuration lists from PhoenixGenApi function configurations, supporting multiple output formats and customization options.
JSON Config List Format
The map format produces a structure like:
%{
"send_direct_message - Send direct message to other user" => %{
"event" => "phoenix_gen_api",
"data" => %{
"user_id" => "user_1",
"device_id" => "device_1",
"request_type" => "send_direct_message",
"request_id" => "request_1",
"service" => "chat",
"version" => "0.0.1",
"args" => %{
"to_user_id" => "",
"content" => "",
"reply_to_id" => ""
}
}
}
}Usage
# Default: returns FunConfig structs (Ash Resource type)
PhoenixGenApi.JsonConfig.generate(MyApp.Chat)
#=> [%PhoenixGenApi.Structs.FunConfig{...}, ...]
# As Elixir map (JSON config list format)
PhoenixGenApi.JsonConfig.generate(MyApp.Chat, format: :map)
#=> %{"send_direct_message - ..." => %{...}, ...}
# As JSON string
PhoenixGenApi.JsonConfig.generate(MyApp.Chat, format: :json)
#=> "{\"send_direct_message - ...\": {...}, ...}"
# Custom encoder MFA
PhoenixGenApi.JsonConfig.generate(MyApp.Chat, format: {MyEncoder, :encode, []})
# With custom descriptions
PhoenixGenApi.JsonConfig.generate(MyApp.Chat,
format: :map,
descriptions: %{"send_direct_message" => "Send direct message to other user"}
)
# With description function
PhoenixGenApi.JsonConfig.generate(MyApp.Chat,
format: :map,
descriptions: fn fun_config ->
String.replace(fun_config.request_type, "_", " ")
end
)
# With custom arg values
PhoenixGenApi.JsonConfig.generate(MyApp.Chat,
format: :map,
arg_values: %{
"send_direct_message" => %{
"to_user_id" => "user_2",
"content" => "Hello, how are you?",
"reply_to_id" => ""
}
}
)
# With arg values function
PhoenixGenApi.JsonConfig.generate(MyApp.Chat,
format: :map,
arg_values: fn fun_config ->
# Generate example values based on arg types
fun_config.arg_types
|> Enum.map(fn {name, type} -> {name, example_value(type)} end)
|> Map.new()
end
)
Summary
Functions
Exports all functions from all services.
Exports functions from a specific service.
Exports a single %FunConfig{} to JSON config map format.
Generates JSON configuration from function configurations.
Types
@type arg_values_source() :: %{required(String.t()) => map()} | (PhoenixGenApi.Structs.FunConfig.t() -> map()) | nil
@type description_source() :: %{required(String.t()) => String.t()} | (PhoenixGenApi.Structs.FunConfig.t() -> String.t()) | nil
Functions
@spec export_all(keyword()) :: [PhoenixGenApi.Structs.FunConfig.t()] | map() | String.t() | any()
Exports all functions from all services.
Parameters
opts: Options keyword list (same asgenerate/2)
Returns
Same as generate/2 but for all services.
@spec export_service( String.t() | atom(), keyword() ) :: [PhoenixGenApi.Structs.FunConfig.t()] | map() | String.t() | any()
Exports functions from a specific service.
Parameters
service: Service name (string or atom)opts: Options keyword list (same asgenerate/2)
Returns
Same as generate/2 for a single service.
@spec export_single( PhoenixGenApi.Structs.FunConfig.t(), keyword() ) :: {String.t(), map()}
Exports a single %FunConfig{} to JSON config map format.
Parameters
fun_config: A%FunConfig{}structopts: Options keyword list (same asgenerate/2)
Returns
A map in the JSON config format for a single function.
@spec generate( String.t() | atom() | [String.t() | atom()], keyword() ) :: [PhoenixGenApi.Structs.FunConfig.t()] | map() | String.t() | any()
Generates JSON configuration from function configurations.
Parameters
service_or_services: A service name (string/atom) or list of service namesopts: Options keyword list
Options
:format- Output format (:structs,:map,:json, or{module, function, args}):descriptions- Custom descriptions (map or function):arg_values- Custom arg values (map or function):user_id- User ID for the request (default: "user_1"):device_id- Device ID for the request (default: "device_1"):request_id- Request ID for the request (default: "request_1"):event- Event name (default: "phoenix_gen_api")
Returns
Depends on the :format option:
:structs- List of%FunConfig{}structs:map- Map in JSON config list format:json- JSON string{module, function, args}- Result of calling the custom encoder