-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
398 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import Config | ||
|
||
config :resource_kit, ResourceKit.Deref, adapter: ResourceKit.Deref.Local | ||
|
||
config :resource_kit, ResourceKitCLI.Endpoint, server: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
defmodule ResourceKit do | ||
@moduledoc false | ||
|
||
defdelegate insert(action, params), to: ResourceKit.Action.Insert, as: :run | ||
defdelegate insert(action, params, opts), to: ResourceKit.Action.Insert, as: :run | ||
|
||
defdelegate list(action, params), to: ResourceKit.Action.List, as: :run | ||
defdelegate list(action, params, opts), to: ResourceKit.Action.List, as: :run | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule ResourceKit.Deref do | ||
@moduledoc """ | ||
A behavior definition that developers can use to implement their own dereferencing logic. | ||
## Options | ||
* `adapter` - A module that implemented the deref behaviour. Or `{adapter, opts}` if the adapter has options. | ||
""" | ||
|
||
alias ResourceKit.Types | ||
|
||
alias ResourceKit.Deref.Context | ||
alias ResourceKit.Schema.Ref | ||
|
||
@conf Application.compile_env(:resource_kit, [__MODULE__, :adapter], ResourceKit.Deref.Local) | ||
@adapter if is_tuple(@conf), do: elem(@conf, 0), else: @conf | ||
@opts if is_tuple(@conf), do: elem(@conf, 1), else: [] | ||
|
||
@callback resolve(ref :: Ref.t(), ctx :: Context.t()) :: | ||
{:ok, Ref.t()} | {:error, Types.error()} | ||
|
||
@callback fetch(ref :: Ref.t(), ctx :: Context.t()) :: | ||
{:ok, Types.json_value()} | {:error, Types.error()} | ||
|
||
defmacro __using__(_args) do | ||
quote location: :keep do | ||
@behaviour unquote(__MODULE__) | ||
|
||
import unquote(__MODULE__) | ||
|
||
@impl unquote(__MODULE__) | ||
def resolve(ref, ctx) do | ||
unquote(__MODULE__).absolute(ref, ctx) | ||
end | ||
|
||
defoverridable resolve: 2 | ||
end | ||
end | ||
|
||
defguard is_absolute(term) when is_struct(term, Ref) and is_binary(term.uri.scheme) | ||
|
||
@spec absolute(ref :: Ref.t(), ctx :: Context.t()) :: {:ok, Ref.t()} | {:error, Types.error()} | ||
def absolute(ref, ctx) | ||
|
||
def absolute(%Ref{} = ref, %Context{}) when is_absolute(ref) do | ||
{:ok, ref} | ||
end | ||
|
||
def absolute(%Ref{uri: uri}, %Context{current: %Ref{uri: current}}) do | ||
{:ok, %Ref{uri: %{current | path: Path.expand(uri.path, current.path)}}} | ||
end | ||
|
||
@spec resolve(ref :: Ref.t(), ctx :: Context.t()) :: {:ok, Ref.t()} | {:error, Types.error()} | ||
def resolve(ref, ctx) do | ||
@adapter.resolve(ref, put_options(ctx)) | ||
end | ||
|
||
@spec fetch(ref :: Ref.t(), ctx :: Context.t()) :: | ||
{:ok, Types.json_value()} | {:error, Types.error()} | ||
def fetch(ref, ctx) do | ||
@adapter.fetch(ref, put_options(ctx)) | ||
end | ||
|
||
defp put_options(ctx) do | ||
%{ctx | opts: @opts} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule ResourceKit.Deref.Context do | ||
@moduledoc false | ||
|
||
use TypedStruct | ||
|
||
alias ResourceKit.Schema.Ref | ||
|
||
typedstruct do | ||
field :current, Ref.t(), enforce: true | ||
field :opts, keyword(), default: [] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule ResourceKit.Deref.Local do | ||
@moduledoc """ | ||
A deref implementation that loads a JSON document from a local directory. | ||
## Options | ||
* `directory` - The root directory from which to load JSON documents. Defaults to `File.cwd/0`. | ||
""" | ||
|
||
use ResourceKit.Deref | ||
|
||
alias ResourceKit.Deref.Context | ||
alias ResourceKit.Schema.Ref | ||
|
||
@impl ResourceKit.Deref | ||
def fetch(%Ref{uri: %URI{} = uri}, %Context{opts: opts}) do | ||
directory = Keyword.get_lazy(opts, :directory, &File.cwd!/0) | ||
file = uri.path |> Path.relative() |> Path.expand(directory) | ||
|
||
with {:ok, content} <- fetch_file(file), | ||
{:ok, value} <- decode_json(content) do | ||
{:ok, value} | ||
else | ||
{:error, message} -> {:error, {message, path: uri.path}} | ||
end | ||
end | ||
|
||
defp fetch_file(path) do | ||
case File.read(path) do | ||
{:ok, content} -> {:ok, content} | ||
{:error, reason} -> {:error, "#{reason}"} | ||
end | ||
end | ||
|
||
defp decode_json(content) do | ||
case Jason.decode(content) do | ||
{:ok, value} -> {:ok, value} | ||
{:error, reason} -> {:error, Exception.message(reason)} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
defmodule ResourceKit.Pipeline.Compile.Token do | ||
@moduledoc false | ||
|
||
use TypedStruct | ||
use ResourceKit.Pipeline.Token | ||
|
||
typedstruct module: Context do | ||
field :root, URI.t(), enforce: true | ||
field :current, URI.t(), enforce: true | ||
end | ||
|
||
token do | ||
field :action, map(), enforce: true | ||
field :context, Context.t(), enforce: true | ||
end | ||
end |
Oops, something went wrong.