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

[DRAFT] Elixir Optional parameters #113

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
75cdac4
Fix link to aws-sdk-go-v2.
Doerge May 30, 2024
fcf9e75
Fix SPEC_PATH in README.
Doerge May 30, 2024
58a5ed2
Move all optional action parameters into Keyword lists, and add
Doerge May 31, 2024
4a9292f
Add the first part of the parameter documentation. E.g. the inner par…
Doerge May 31, 2024
27f1093
Add text wrapping of parameter docs with Excribe.
Doerge May 31, 2024
ee5d7d1
Move params docs near the top of the action docstring.
Doerge Jun 1, 2024
d1515f5
Add link to docs (search).
Doerge Jun 1, 2024
545f5f8
Sort parameters into required and optional in the REST Action
Doerge Jul 16, 2024
dc18ee4
Dirty fix for types named `identifier`. See latest quicksight.
Doerge Jul 16, 2024
3acf04b
Document the `input` params for PostServices.
Doerge Jul 17, 2024
bc51b1a
Fix Rest.
Doerge Jul 17, 2024
ef78fd8
Handle parameters that are sent as HTTP bodies better:
Doerge Jul 19, 2024
45409d6
Remove junk.
Doerge Jul 19, 2024
5490817
Document and and add guards for optional bodies.
Doerge Jul 19, 2024
e732f63
Improve types, guards, specs for both params and body.
Doerge Jul 19, 2024
9e4bb8a
Refactor to use render_def.
Doerge Jul 19, 2024
5836fea
Fix optional body.
Doerge Aug 3, 2024
eeb672d
Add "required" to docstring param docs, and cleanup whitespaces.
Doerge Aug 4, 2024
13925f6
Improve whitespace rendering.
Doerge Aug 4, 2024
6c77d69
Fix body-parameters location name.
Doerge Aug 4, 2024
b79af58
Remove repeated query param usage.
Doerge Aug 4, 2024
7a8317d
Formatting of body param required.
Doerge Aug 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Code is generated by running the `generate.exs` script. It requires Elixir 1.8.4
### Elixir

```bash
export SPEC_PATH=../../aws/aws-sdk-go/models/apis
export SPEC_PATH=../aws-sdk-go-v2/codegen/sdk-codegen/aws-models
export TEMPLATE_PATH=priv
export ELIXIR_OUTPUT_PATH=../aws-elixir/lib/aws/generated
mix run generate.exs elixir $SPEC_PATH $TEMPLATE_PATH $ELIXIR_OUTPUT_PATH
Expand All @@ -21,7 +21,7 @@ mix run generate.exs elixir $SPEC_PATH $TEMPLATE_PATH $ELIXIR_OUTPUT_PATH
### Erlang

```bash
export SPEC_PATH=../../aws/aws-sdk-go/models/apis
export SPEC_PATH=../aws-sdk-go-v2/codegen/sdk-codegen/aws-models
export TEMPLATE_PATH=priv
export ERLANG_OUTPUT_PATH=../aws-erlang/src
mix run generate.exs erlang $SPEC_PATH $TEMPLATE_PATH $ERLANG_OUTPUT_PATH
Expand Down Expand Up @@ -75,5 +75,5 @@ Alternatively you can install XCode's Command Line Developer Tools package:

$ xcode-select --install

[aws-sdk-go]: https://github.com/aws/aws-sdk-go
[aws-sdk-go]: https://github.com/aws/aws-sdk-go-v2
[brew]: https://brew.sh/
19 changes: 19 additions & 0 deletions lib/aws_codegen.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule AWS.CodeGen do
alias AWS.CodeGen.RestService.Parameter
alias AWS.CodeGen.Spec

@moduledoc """
Expand Down Expand Up @@ -132,6 +133,24 @@ defmodule AWS.CodeGen do
format_string!(context.language, rendered)
end

@param_quoted_elixir EEx.compile_string(
~s|* `:<%= parameter.code_name %>` (`t:<%= parameter.type %>`<%= if parameter.required do %> required<% end %>) <%= parameter.docs %>|
)
def render_parameter(:elixir, %Parameter{} = parameter) do
Code.eval_quoted(@param_quoted_elixir, parameter: parameter)
|> then(&elem(&1, 0))
|> Excribe.format(width: 80, hanging: 4, pretty: true)
end

@body_param_quoted EEx.compile_string(
~s|* `"<%= parameter.location_name %>" => t:<%= parameter.type %>`<%= if parameter.required do %> (required)<% end %> <%= parameter.docs %>|
)
def render_body_parameter(%Parameter{} = parameter) do
Code.eval_quoted(@body_param_quoted, parameter: parameter)
|> then(&elem(&1, 0))
|> Excribe.format(width: 80, hanging: 4, pretty: true)
end

defp format_string!(:elixir, rendered) do
[Code.format_string!(rendered), ?\n]
end
Expand Down
59 changes: 46 additions & 13 deletions lib/aws_codegen/docstring.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,33 @@ defmodule AWS.CodeGen.Docstring do
heredoc in generated Elixir or Erlang code.
"""
def format(:elixir, text) do
text
|> html_to_markdown()
|> split_first_sentence_in_one_line()
|> split_paragraphs()
|> Enum.map(&justify_line(&1, @max_elixir_line_length))
|> Enum.join("\n")
|> fix_broken_markdown_links()
|> fix_elixir_lookalike_format_strings()
|> fix_html_spaces()
|> fix_long_break_lines()
|> transform_subtitles()
|> String.trim_trailing()
docstring =
text
|> html_to_markdown()
|> fix_broken_markdown_links()
|> fix_elixir_lookalike_format_strings()
|> fix_html_spaces()
|> fix_long_break_lines()
|> transform_subtitles()
|> String.trim_trailing()

# Split off the beginning of the docs.
[docstring_header, _docstring_rest] =
case String.split(docstring, "\n", parts: 3, trim: true) do
[a, b, rest] ->
[a <> "\n" <> b, rest]

[a, b] ->
[a, b]

[a] ->
[a, ""]

[] ->
["", ""]
end

docstring_header |> Excribe.format(width: 80, hanging: 2)
end

def format(:erlang, nil), do: ""
Expand Down Expand Up @@ -149,7 +164,7 @@ defmodule AWS.CodeGen.Docstring do
defp update_nodes({tag, _, children}) when tag in ~w(i em), do: "*#{Floki.text(children)}*"

defp update_nodes({tag, _, children}) when tag in ~w(p fullname note important),
do: Floki.text(children) <> @two_break_lines
do: (Floki.text(children) |> String.replace("\n", " ")) <> @two_break_lines

defp update_nodes({"a", attrs, children}) do
case Enum.find(attrs, fn {attr, _} -> attr == "href" end) do
Expand Down Expand Up @@ -367,4 +382,22 @@ defmodule AWS.CodeGen.Docstring do

List.flatten(lines ++ [current])
end

def docs_url(shapes, operation) do
service_name =
shapes
|> Map.keys()
|> List.first()
|> then(fn name ->
name
|> String.split("#")
|> hd()
|> String.split(".")
|> List.last()
end)

op_name = operation |> String.split("#") |> List.last()

"https://docs.aws.amazon.com/search/doc-search.html?searchPath=documentation&searchQuery=#{service_name}%20#{op_name}&this_doc_guide=API%2520Reference"
end
end
Loading