View Source OpenAPI.Renderer.Util (OpenAPI Generator v0.1.0-rc.0)
Default implementation and helpers related to formatting and writing files
This module contains the default implementations for:
It also contains several helpers for working with ASTs, including the addition of formatting metadata necessary to create consistent code.
Link to this section Summary
Default Implementations
Convert an AST into formatted code as a string
Write a rendered file to the filesystem
Functions
Flatten and remove nil
elements from a list of AST nodes
Walks the given AST and replaces @doc
and @moduledoc
strings with """
blocks if the
contents have newlines
Enforce the existence of whitespace after an expression
Collapse nested unions and replace references with {module, type} identifiers
Render an internal type as a typespec
Replace enum
types with the equivalent list of const
types
Flatten nested union types
Link to this section Default Implementations
@spec format(OpenAPI.Renderer.State.t(), OpenAPI.Renderer.File.t()) :: iodata()
Convert an AST into formatted code as a string
Default implementation of OpenAPI.Renderer.format/2
.
The AST may include optional formatting metadata (ex. delimiter
, indentation
, or
end_of_expression
). It will be formatted to a line width of 98 to match the default Mix
formatter. In addition, any @moduledoc
or @doc
statements that contain a newline character
will be modified to use """
as the delimiter.
@spec write(OpenAPI.Renderer.State.t(), OpenAPI.Renderer.File.t()) :: :ok
Write a rendered file to the filesystem
Default implementation of OpenAPI.Renderer.write/2
.
This implementation writes the file contents
to the location
and ensures an additional
newline is included at the end of the file. It also ensures that any subdirectories are created
prior to writing. Any failure will result in a raised error.
Link to this section Functions
Flatten and remove nil
elements from a list of AST nodes
This helper deals with cases in which certain sections of code are rendered conditionally, or
with variable lengths of statements. nil
values will be removed, and nested lists of nodes
will be flattened.
example
Example
header = if condition, do: quote(do: IO.puts("List"))
statements = for item <- items, do: quote(do: IO.puts(" * #{item}"))
clean_list([header, statements])
Walks the given AST and replaces @doc
and @moduledoc
strings with """
blocks if the
contents have newlines
Enforce the existence of whitespace after an expression
This helper is useful for cases in which single-line expressions should be separated from the following line by whitespace, but the formatter would not naturally insert that whitespace. For example:
@my_attribute "Hello"
@spec my_function :: String.t()
def my_function, do: @my_attribute
It may be desirable to insert whitespace following the module attribute @my_attribute
. By
calling this function on that node, the following will be output:
@my_attribute "Hello"
@spec my_function :: String.t()
def my_function, do: @my_attribute
If a list of nodes is given, the last node will receive the additional whitespace metadata.
@spec to_readable_type(OpenAPI.Renderer.State.t(), OpenAPI.Processor.Type.t()) :: term()
Collapse nested unions and replace references with {module, type} identifiers
This function renders most types exactly as they are expressed internally
(ex. {:string, :generic}
), however it transforms certain union types to be more human-readable
and it replaces schema references with the equivalent {Module, :type}
.
@spec to_type( OpenAPI.Renderer.State.t(), OpenAPI.Processor.Type.t() | {module(), atom()} ) :: Macro.t()
Render an internal type as a typespec
To the best of its ability, this function constructs an accurate typespec for the internal
type given. Note that this is somewhat lossy; for example, many distinct types of strings will
map to the String.t()
type.
@spec unwrap_enums([OpenAPI.Processor.Type.t()]) :: [OpenAPI.Processor.Type.t()]
Replace enum
types with the equivalent list of const
types
This low-level helper is used by to_type/2
when simplifying union types.
@spec unwrap_unions([OpenAPI.Processor.Type.t()]) :: [OpenAPI.Processor.Type.t()]
Flatten nested union types
This low-level helper is used by to_readable_type/2
and to_type/2
when simplifying union
types.