From 58e9d2bceac7664da8acda5ae9b844ed9642c7bb Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 7 Feb 2017 11:45:51 +0800 Subject: [PATCH 1/3] add header desc support --- lib/maru/builder/description.ex | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/maru/builder/description.ex b/lib/maru/builder/description.ex index 51d751b..aafcfe8 100644 --- a/lib/maru/builder/description.ex +++ b/lib/maru/builder/description.ex @@ -33,4 +33,12 @@ defmodule Maru.Builder.Description do end end + defmacro headers([do: block]) do + desc = Keyword.get(options, :desc) + quote do + @desc put_in(@desc, [:headers], []) + unquote(block) + end + end + end From 89ee146731abe91a61dcc1e9f9d80b1debc16aa8 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 8 Feb 2017 14:38:55 +0800 Subject: [PATCH 2/3] add headers in desc --- lib/maru/builder/description.ex | 25 ++++++++++++++++++++++--- lib/maru/router.ex | 2 +- test/builder/dsls_test.exs | 11 +++++++++++ test/builder/exceptions_test.exs | 1 + test/tasks/routes_test.exs | 9 +++++---- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/maru/builder/description.ex b/lib/maru/builder/description.ex index aafcfe8..fd6a891 100644 --- a/lib/maru/builder/description.ex +++ b/lib/maru/builder/description.ex @@ -3,6 +3,17 @@ defmodule Maru.Builder.Description do Parse and build description block. """ + @doc """ + Define headers of description. + """ + defmacro headers([do: block]) do + + quote do + @desc put_in(@desc, [:headers], []) + unquote(block) + end + end + @doc """ Define detail of description. """ @@ -33,12 +44,20 @@ defmodule Maru.Builder.Description do end end - defmacro headers([do: block]) do + defmacro requires(name, options) do desc = Keyword.get(options, :desc) + type = Keyword.get(options, :type) + header = %{attr_name: case is_atom(name) do + true -> Atom.to_string(name) + _ -> name + end, type: case is_atom(type) do + true -> Atom.to_string(type) + _ -> type + end, description: desc} |> Macro.escape quote do - @desc put_in(@desc, [:headers], []) - unquote(block) + @desc update_in(@desc, [:headers], &(&1 ++[unquote(header)])) end end + end diff --git a/lib/maru/router.ex b/lib/maru/router.ex index f992c6a..d834d01 100644 --- a/lib/maru/router.ex +++ b/lib/maru/router.ex @@ -1,6 +1,6 @@ defmodule Maru.Router do @moduledoc false - + defmacro __using__(opts) do quote do use Maru.Builder, unquote(opts) diff --git a/test/builder/dsls_test.exs b/test/builder/dsls_test.exs index d085d4c..ceb0ce8 100644 --- a/test/builder/dsls_test.exs +++ b/test/builder/dsls_test.exs @@ -101,6 +101,9 @@ defmodule Maru.Builder.DSLsTest do end test "description with block" do + alias Maru.Struct.Parameter, as: P + alias Maru.Struct.Parameter.Information, as: PI + defmodule DescTestWithBlock do use Maru.Router @@ -108,6 +111,10 @@ defmodule Maru.Builder.DSLsTest do detail """ this is detail """ + headers do + requires :token, type: :string, desc: "ok" + requires "access-token", type: :string, desc: "ok" + end responses do status 200, desc: "ok" @@ -120,6 +127,10 @@ defmodule Maru.Builder.DSLsTest do assert %{ summary: "desc test", detail: "this is detail\n", + headers: [ + %{attr_name: "token", type: "string", description: "ok"}, + %{attr_name: "access-token", type: "string", description: "ok"} + ], responses: [ %{code: 200, description: "ok"}, %{code: 500, description: "error"}, diff --git a/test/builder/exceptions_test.exs b/test/builder/exceptions_test.exs index 6ce2af5..c01ffd5 100644 --- a/test/builder/exceptions_test.exs +++ b/test/builder/exceptions_test.exs @@ -93,6 +93,7 @@ defmodule Maru.Builder.ExceptionsTest do params do requires :a end + get "/a" do unwarn(params) text(conn, "a") diff --git a/test/tasks/routes_test.exs b/test/tasks/routes_test.exs index ae7160f..b55fc0b 100644 --- a/test/tasks/routes_test.exs +++ b/test/tasks/routes_test.exs @@ -5,7 +5,6 @@ defmodule Maru.Tasks.RoutesTest do defmodule Router do use Maru.Router - get do text(conn, "ok") end @@ -28,10 +27,12 @@ defmodule Maru.Tasks.RoutesTest do defmodule Router2 do use Maru.Router - desc "test endpoint" - get do - text(conn, "ok") + desc "test endpoint" do + get do + text(conn, "ok") + end end + end defmodule API2 do From ec511094ac9a7b23a56e7c36cb52e175f5d05d4b Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 8 Feb 2017 15:37:48 +0800 Subject: [PATCH 3/3] fix both import error change marco requires to need in headers des --- lib/maru/builder/description.ex | 2 +- test/builder/dsls_test.exs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/maru/builder/description.ex b/lib/maru/builder/description.ex index fd6a891..e87411b 100644 --- a/lib/maru/builder/description.ex +++ b/lib/maru/builder/description.ex @@ -44,7 +44,7 @@ defmodule Maru.Builder.Description do end end - defmacro requires(name, options) do + defmacro need(name, options) do desc = Keyword.get(options, :desc) type = Keyword.get(options, :type) header = %{attr_name: case is_atom(name) do diff --git a/test/builder/dsls_test.exs b/test/builder/dsls_test.exs index ceb0ce8..e2ef148 100644 --- a/test/builder/dsls_test.exs +++ b/test/builder/dsls_test.exs @@ -112,8 +112,12 @@ defmodule Maru.Builder.DSLsTest do this is detail """ headers do - requires :token, type: :string, desc: "ok" - requires "access-token", type: :string, desc: "ok" + need :token, type: :string, desc: "ok" + need "access-token", type: :string, desc: "ok" + end + + params do + requires :test, type: :string, desc: "ok" end responses do