improvement: handle optional arguments

This commit is contained in:
Zach Daniel 2023-01-15 01:48:24 -05:00
parent 39e7dede1a
commit 683a34d121
6 changed files with 387 additions and 3 deletions

View file

@ -33,6 +33,10 @@ defmodule AshHq.Docs.Dsl do
attribute :examples, {:array, :string}
attribute :args, {:array, :string}
attribute :optional_args, {:array, :string} do
default []
end
attribute :arg_defaults, :map
attribute :path, {:array, :string}
attribute :recursive_as, :string
attribute :order, :integer, allow_nil?: false
@ -86,6 +90,8 @@ defmodule AshHq.Docs.Dsl do
references do
reference :library_version, on_delete: :delete
end
migration_defaults optional_args: "[]"
end
actions do

View file

@ -0,0 +1,17 @@
defmodule AshHq.Docs.Dsl.Types.Arg do
use Ash.Resource,
data_layer: :embedded
attributes do
attribute :optional, :boolean do
default false
allow_nil? false
end
attribute :name, :string do
allow_nil? false
end
attribute :default, :string
end
end

View file

@ -222,6 +222,9 @@ defmodule AshHqWeb.Pages.Docs do
<tr>
<th>Name</th>
<th>Type</th>
{#if @dsl.arg_defaults not in [%{}, nil]}
<th>Default</th>
{/if}
<th>Doc</th>
<th>Links</th>
</tr>
@ -234,13 +237,16 @@ defmodule AshHqWeb.Pages.Docs do
</LivePatch>
<div class="flex flex-row space-x-2">
<CalloutText text={option.name} />
{render_tags(assigns, option)}
{render_tags(assigns, %{option | required: option.required && !Map.has_key?(@dsl.arg_defaults, option.name)})}
</div>
</div>
</td>
<td>
{option.type}
</td>
{#if @dsl.arg_defaults not in [%{}, nil]}
<th>{Map.get(@dsl.arg_defaults, option.name)}</th>
{/if}
<td>
{raw(render_replacements(@libraries, @selected_versions, option.html_for))}
</td>

View file

@ -0,0 +1,23 @@
defmodule AshHq.Repo.Migrations.MigrateResources40 do
@moduledoc """
Updates resources based on their most recent snapshots.
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""
use Ecto.Migration
def up do
alter table(:dsls) do
add :optional_args, {:array, :text}, default: []
add :arg_defaults, :map
end
end
def down do
alter table(:dsls) do
remove :arg_defaults
remove :optional_args
end
end
end

View file

@ -0,0 +1,295 @@
{
"attributes": [
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "sanitized_path",
"type": "text"
},
{
"allow_nil?": false,
"default": "fragment(\"uuid_generate_v4()\")",
"generated?": false,
"primary_key?": true,
"references": null,
"size": null,
"source": "id",
"type": "uuid"
},
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "name",
"type": "text"
},
{
"allow_nil?": false,
"default": "\"\"",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "doc",
"type": "text"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "doc_html",
"type": "text"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "imports",
"type": [
"array",
"text"
]
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "links",
"type": "map"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "examples",
"type": [
"array",
"text"
]
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "args",
"type": [
"array",
"text"
]
},
{
"allow_nil?": true,
"default": "[]",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "optional_args",
"type": [
"array",
"text"
]
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "arg_defaults",
"type": "map"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "path",
"type": [
"array",
"text"
]
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "recursive_as",
"type": "text"
},
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "order",
"type": "bigint"
},
{
"allow_nil?": false,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "type",
"type": "text"
},
{
"allow_nil?": false,
"default": "fragment(\"now()\")",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "inserted_at",
"type": "utc_datetime_usec"
},
{
"allow_nil?": false,
"default": "fragment(\"now()\")",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "updated_at",
"type": "utc_datetime_usec"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": {
"destination_attribute": "id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"name": "dsls_library_version_id_fkey",
"on_delete": "delete",
"on_update": null,
"schema": "public",
"table": "library_versions"
},
"size": null,
"source": "library_version_id",
"type": "uuid"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": {
"destination_attribute": "id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"name": "dsls_extension_id_fkey",
"on_delete": null,
"on_update": null,
"schema": "public",
"table": "extensions"
},
"size": null,
"source": "extension_id",
"type": "uuid"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": {
"destination_attribute": "id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"name": "dsls_dsl_id_fkey",
"on_delete": null,
"on_update": null,
"schema": "public",
"table": "dsls"
},
"size": null,
"source": "dsl_id",
"type": "uuid"
}
],
"base_filter": null,
"check_constraints": [],
"custom_indexes": [],
"custom_statements": [
{
"code?": false,
"down": "DROP INDEX dsls_name_lower_index;",
"name": "name_index",
"up": "CREATE INDEX dsls_name_lower_index ON dsls(lower(name));\n"
},
{
"code?": false,
"down": "DROP INDEX dsls_name_trigram_index;",
"name": "trigram_index",
"up": "CREATE INDEX dsls_name_trigram_index ON dsls USING GIST (name gist_trgm_ops);\n"
},
{
"code?": false,
"down": "DROP INDEX dsls_search_index;",
"name": "search_index",
"up": "CREATE INDEX dsls_search_index ON dsls USING GIN((\n setweight(to_tsvector('english', name), 'A') ||\n setweight(to_tsvector('english', doc), 'D')\n));\n"
}
],
"has_create_action": true,
"hash": "14B6D64A6B4DA6AACB6CD273F31980B22F8FF0D9FFE1BFFA590498D1A09A8BA4",
"identities": [],
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"repo": "Elixir.AshHq.Repo",
"schema": null,
"table": "dsls"
}

View file

@ -154,7 +154,36 @@ defmodule Utils do
doc: docs_with_examples(entity.describe || "", examples(entity.examples)),
imports: [],
links: Map.new(entity.links || []),
args: entity.args,
args:
Enum.map(entity.args, fn
{:optional, name, _} ->
name
{:optional, name} ->
name
name ->
name
end),
optional_args:
Enum.flat_map(entity.args, fn
{:optional, name, _} ->
[name]
{:optional, name} ->
[name]
_ ->
[]
end),
arg_defaults:
Enum.reduce(entity.args, %{},
fn {:optional, name, default}, acc ->
Map.put(acc, name, inspect(default))
_, acc ->
acc
end),
type: :entity,
path: path,
options: add_argument_indices(schema(option_schema, path ++ [entity.name]), entity.args)
@ -169,7 +198,15 @@ defmodule Utils do
defp add_argument_indices(values, arguments) do
Enum.map(values, fn value ->
case Enum.find_index(arguments, &(&1 == value.name)) do
case Enum.find_index(arguments, fn {:optional, name, _} ->
name == value.name
{:optional, name} ->
name == value.name
name ->
name == value.name
end) do
nil ->
value