Skip to content

Commit

Permalink
mix format pass and update latest elixir version
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardd committed Apr 19, 2021
1 parent 6891e33 commit 579969f
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 202 deletions.
5 changes: 5 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
import_deps: [],
inputs: ["*.{ex,exs}", "priv/*/", "{config,lib,test}/**/*.{ex,exs}"],
subdirectories: ["priv/*/"]
]
5 changes: 4 additions & 1 deletion .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
otp: ["23.0.4"]
elixir: ["1.7.4", "1.8.2", "1.9.4", "1.10.4", "1.11.2"]
elixir: ["1.7.4", "1.8.2", "1.9.4", "1.10.4", "1.11.3"]
steps:
- uses: actions/checkout@v2
- uses: erlef/setup-elixir@v1
Expand All @@ -17,4 +17,7 @@ jobs:
elixir-version: ${{matrix.elixir}}
- run: mix deps.get
- run: mix compile
- run: mix format --check-formatted
if: matrix.elixir == '1.11.3' # Only check formatting with the latest verison
- run: mix test

14 changes: 7 additions & 7 deletions lib/ex_aws/s3/delete_all_objects.ex
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
defmodule ExAws.Operation.S3DeleteAllObjects do
defstruct [
bucket: nil,
objects: [],
opts: [],
service: :s3
]
defstruct bucket: nil,
objects: [],
opts: [],
service: :s3

@type t :: %__MODULE__{}

defimpl ExAws.Operation do

def perform(%{bucket: bucket, objects: objects, opts: opts}, config) do
request_fun = fn objects_in_batch ->
bucket
|> ExAws.S3.delete_multiple_objects(objects_in_batch, opts)
|> ExAws.request(config)
end

delete_all_objects(request_fun, objects, opts, [])
end

defp delete_all_objects(_request_fun, [], _opts, acc) do
{:ok, Enum.reverse(acc)}
end

defp delete_all_objects(request_fun, objects, opts, acc) do
{objects, rest} = Enum.split(objects, 1000)

with {:ok, result} <- request_fun.(objects) do
delete_all_objects(request_fun, rest, opts, [result | acc])
end
Expand Down
26 changes: 14 additions & 12 deletions lib/ex_aws/s3/download.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ defmodule ExAws.S3.Download do
:path,
:dest,
opts: [],
service: :s3,
service: :s3
]

@type t :: %__MODULE__{}

def get_chunk(op, %{start_byte: start_byte, end_byte: end_byte}, config) do
%{body: body} =
op.bucket
|> ExAws.S3.get_object(op.path, [range: "bytes=#{start_byte}-#{end_byte}"])
|> ExAws.S3.get_object(op.path, range: "bytes=#{start_byte}-#{end_byte}")
|> ExAws.request!(config)

{start_byte, body}
Expand Down Expand Up @@ -53,12 +53,11 @@ defmodule ExAws.S3.Download do
headers
|> Enum.find(fn {k, _} -> String.downcase(k) == "content-length" end)
|> elem(1)
|> String.to_integer
|> String.to_integer()
end
end

defimpl ExAws.Operation, for: ExAws.S3.Download do

alias ExAws.S3.Download

def perform(op, config) do
Expand All @@ -68,18 +67,20 @@ defimpl ExAws.Operation, for: ExAws.S3.Download do
end

defp download_to({:error, e}, _op, _config), do: {:error, e}

defp download_to({:ok, file}, op, config) do
try do
op
|> Download.build_chunk_stream(config)
|> Task.async_stream(fn boundaries ->
chunk = Download.get_chunk(op, boundaries, config)
:ok = :file.pwrite(file, [chunk])
end,
|> Task.async_stream(
fn boundaries ->
chunk = Download.get_chunk(op, boundaries, config)
:ok = :file.pwrite(file, [chunk])
end,
max_concurrency: Keyword.get(op.opts, :max_concurrency, 8),
timeout: Keyword.get(op.opts, :timeout, 60_000)
)
|> Stream.run
|> Stream.run()

File.close(file)

Expand All @@ -95,9 +96,10 @@ defimpl ExAws.Operation, for: ExAws.S3.Download do
def stream!(op, config) do
op
|> Download.build_chunk_stream(config)
|> Task.async_stream(fn boundaries ->
Download.get_chunk(op, boundaries, config)
end,
|> Task.async_stream(
fn boundaries ->
Download.get_chunk(op, boundaries, config)
end,
max_concurrency: Keyword.get(op.opts, :max_concurrency, 8),
timeout: Keyword.get(op.opts, :timeout, 60_000)
)
Expand Down
28 changes: 17 additions & 11 deletions lib/ex_aws/s3/lazy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ defmodule ExAws.S3.Lazy do
|> Map.get(:body)
end

Stream.resource(fn -> {request_fun, []} end, fn
:quit -> {:halt, nil}
Stream.resource(
fn -> {request_fun, []} end,
fn
:quit ->
{:halt, nil}

{fun, args} -> case fun.(args) do
{fun, args} ->
case fun.(args) do
results = %{is_truncated: "true"} ->
{add_results(results, opts), {fun, [marker: next_marker(results)]}}

results = %{is_truncated: "true"} ->
{add_results(results, opts), {fun, [marker: next_marker(results)]}}

results ->
{add_results(results, opts), :quit}
end
end, &(&1))
results ->
{add_results(results, opts), :quit}
end
end,
& &1
)
end

def add_results(results, opts) do
Expand All @@ -31,8 +36,9 @@ defmodule ExAws.S3.Lazy do

def next_marker(%{next_marker: "", contents: contents}) do
contents
|> List.last
|> List.last()
|> Map.fetch!(:key)
end

def next_marker(%{next_marker: marker}), do: marker
end
144 changes: 74 additions & 70 deletions lib/ex_aws/s3/parsers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,108 +3,113 @@ if Code.ensure_loaded?(SweetXml) do
import SweetXml, only: [sigil_x: 2]

def parse_upload({:ok, resp = %{body: xml}}) do
parsed_body = xml
|> SweetXml.xpath(~x"//CompleteMultipartUploadResult",
location: ~x"./Location/text()"s,
bucket: ~x"./Bucket/text()"s,
key: ~x"./Key/text()"s,
eTag: ~x"./ETag/text()"s
)
parsed_body =
xml
|> SweetXml.xpath(~x"//CompleteMultipartUploadResult",
location: ~x"./Location/text()"s,
bucket: ~x"./Bucket/text()"s,
key: ~x"./Key/text()"s,
eTag: ~x"./ETag/text()"s
)

{:ok, %{resp | body: parsed_body}}
end

def parse_list_objects({:ok, resp = %{body: xml}}) do
parsed_body = xml
|> SweetXml.xpath(~x"//ListBucketResult",
name: ~x"./Name/text()"s,
is_truncated: ~x"./IsTruncated/text()"s,
prefix: ~x"./Prefix/text()"s,
marker: ~x"./Marker/text()"s,
next_continuation_token: ~x"./NextContinuationToken/text()"s,
key_count: ~x"./KeyCount/text()"s,
max_keys: ~x"./MaxKeys/text()"s,
next_marker: ~x"./NextMarker/text()"s,
contents: [
~x"./Contents"l,
key: ~x"./Key/text()"s,
last_modified: ~x"./LastModified/text()"s,
e_tag: ~x"./ETag/text()"s,
size: ~x"./Size/text()"s,
storage_class: ~x"./StorageClass/text()"s,
owner: [
~x"./Owner"o,
id: ~x"./ID/text()"s,
display_name: ~x"./DisplayName/text()"s
parsed_body =
xml
|> SweetXml.xpath(~x"//ListBucketResult",
name: ~x"./Name/text()"s,
is_truncated: ~x"./IsTruncated/text()"s,
prefix: ~x"./Prefix/text()"s,
marker: ~x"./Marker/text()"s,
next_continuation_token: ~x"./NextContinuationToken/text()"s,
key_count: ~x"./KeyCount/text()"s,
max_keys: ~x"./MaxKeys/text()"s,
next_marker: ~x"./NextMarker/text()"s,
contents: [
~x"./Contents"l,
key: ~x"./Key/text()"s,
last_modified: ~x"./LastModified/text()"s,
e_tag: ~x"./ETag/text()"s,
size: ~x"./Size/text()"s,
storage_class: ~x"./StorageClass/text()"s,
owner: [
~x"./Owner"o,
id: ~x"./ID/text()"s,
display_name: ~x"./DisplayName/text()"s
]
],
common_prefixes: [
~x"./CommonPrefixes"l,
prefix: ~x"./Prefix/text()"s
]
],
common_prefixes: [
~x"./CommonPrefixes"l,
prefix: ~x"./Prefix/text()"s
]
)
)

{:ok, %{resp | body: parsed_body}}
end

def parse_list_objects(val), do: val

def parse_all_my_buckets_result({:ok, resp = %{body: xml}}) do
parsed_body = xml
|> SweetXml.xpath(~x"//ListAllMyBucketsResult",
owner: [
~x"./Owner",
id: ~x"./ID/text()"s,
display_name: ~x"./DisplayName/text()"s
],
buckets: [
~x".//Bucket"l,
name: ~x"./Name/text()"s,
creation_date: ~x"./CreationDate/text()"s
]
)
parsed_body =
xml
|> SweetXml.xpath(~x"//ListAllMyBucketsResult",
owner: [
~x"./Owner",
id: ~x"./ID/text()"s,
display_name: ~x"./DisplayName/text()"s
],
buckets: [
~x".//Bucket"l,
name: ~x"./Name/text()"s,
creation_date: ~x"./CreationDate/text()"s
]
)

{:ok, %{resp | body: parsed_body}}
end

def parse_all_my_buckets_result(val), do: val

def parse_initiate_multipart_upload({:ok, resp = %{body: xml}}) do
parsed_body = xml
|> SweetXml.xpath(~x"//InitiateMultipartUploadResult",
bucket: ~x"./Bucket/text()"s,
key: ~x"./Key/text()"s,
upload_id: ~x"./UploadId/text()"s
)
parsed_body =
xml
|> SweetXml.xpath(~x"//InitiateMultipartUploadResult",
bucket: ~x"./Bucket/text()"s,
key: ~x"./Key/text()"s,
upload_id: ~x"./UploadId/text()"s
)

{:ok, %{resp | body: parsed_body}}
end

def parse_initiate_multipart_upload(val), do: val

def parse_upload_part_copy(val), do: val
def parse_complete_multipart_upload(val), do: val

def parse_list_multipart_uploads({:ok, %{body: xml} = resp}) do
parsed_body = SweetXml.xpath(xml, ~x"//ListMultipartUploadsResult",
bucket: ~x"./Bucket/text()"s,
key_marker: ~x"./KeyMarker/text()"s,
upload_id_marker: ~x"./UploadIdMarker/text()"s,
uploads: [~x"./Upload"l,
key: ~x"./Key/text()"s,
upload_id: ~x"./UploadId/text()"s,
]
)
parsed_body =
SweetXml.xpath(xml, ~x"//ListMultipartUploadsResult",
bucket: ~x"./Bucket/text()"s,
key_marker: ~x"./KeyMarker/text()"s,
upload_id_marker: ~x"./UploadIdMarker/text()"s,
uploads: [~x"./Upload"l, key: ~x"./Key/text()"s, upload_id: ~x"./UploadId/text()"s]
)

{:ok, %{resp | body: parsed_body}}
end

def parse_list_multipart_uploads(val), do: val

def parse_list_parts(val), do: val

def parse_object_tagging({:ok, resp = %{body: xml}}) do
parsed_body = SweetXml.xpath(xml, ~x"//Tagging",
tags: [~x"./TagSet/Tag"l,
key: ~x"./Key/text()"s,
value: ~x"./Value/text()"s,
]
)
parsed_body =
SweetXml.xpath(xml, ~x"//Tagging",
tags: [~x"./TagSet/Tag"l, key: ~x"./Key/text()"s, value: ~x"./Value/text()"s]
)

{:ok, %{resp | body: parsed_body}}
end
Expand Down Expand Up @@ -155,7 +160,7 @@ if Code.ensure_loaded?(SweetXml) do
end
else
defmodule ExAws.S3.Parsers do
defp missing_xml_parser(), do: raise ExAws.Error, "Missing XML parser. Please see docs"
defp missing_xml_parser(), do: raise(ExAws.Error, "Missing XML parser. Please see docs")
def upload(_val), do: missing_xml_parser()
def parse_list_objects(_val), do: missing_xml_parser()
def parse_all_my_buckets_result(_val), do: missing_xml_parser()
Expand All @@ -166,5 +171,4 @@ else
def parse_list_parts(_val), do: missing_xml_parser()
def parse_object_tagging(_val), do: missing_xml_parser()
end

end
Loading

0 comments on commit 579969f

Please sign in to comment.