Skip to content

Use Elixir JSON Module #114

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion lib/mix_dependency_submission/cli/submit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ defmodule MixDependencySubmission.CLI.Submit do
ignore: ignore
)

Logger.info("Calculated Submission: #{Jason.encode!(submission, pretty: true)}")
Logger.info("Calculated Submission: #{JSON.encode!(submission, pretty: true)}")

submission
|> ApiClient.submit(github_api_url, github_repository, github_token)
Expand Down
2 changes: 1 addition & 1 deletion lib/mix_dependency_submission/submission.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule MixDependencySubmission.Submission do
manifests: manifests() | nil
}

@derive Jason.Encoder
@derive JSON.Encoder
@enforce_keys [:version, :job, :sha, :ref, :detector, :scanned]
defstruct [:version, :job, :sha, :ref, :detector, :scanned, metadata: nil, manifests: nil]

Expand Down
8 changes: 4 additions & 4 deletions lib/mix_dependency_submission/submission/detector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ defmodule MixDependencySubmission.Submission.Detector do
@enforce_keys [:name, :version, :url]
defstruct [:name, :version, :url]

defimpl Jason.Encoder do
@impl Jason.Encoder
def encode(value, opts) do
defimpl JSON.Encoder do
@impl JSON.Encoder
def encode(value, encoder) do
value
|> Map.from_struct()
|> Map.update!(:version, &Version.to_string/1)
|> Map.update!(:url, &URI.to_string/1)
|> Jason.Encode.map(opts)
|> encoder.(encoder)
end
end
end
8 changes: 4 additions & 4 deletions lib/mix_dependency_submission/submission/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ defmodule MixDependencySubmission.Submission.Job do
@enforce_keys [:id, :correlator]
defstruct [:id, :correlator, html_url: nil]

defimpl Jason.Encoder do
@impl Jason.Encoder
def encode(value, opts) do
defimpl JSON.Encoder do
@impl JSON.Encoder
def encode(value, encoder) do
value
|> Map.from_struct()
|> Map.update!(:html_url, &uri_to_string/1)
|> Enum.reject(&match?({_key, nil}, &1))
|> Map.new()
|> Jason.Encode.map(opts)
|> encoder.(encoder)
end

@spec uri_to_string(uri :: URI.t()) :: String.t()
Expand Down
8 changes: 4 additions & 4 deletions lib/mix_dependency_submission/submission/manifest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ defmodule MixDependencySubmission.Submission.Manifest do
@enforce_keys [:name]
defstruct [:name, file: nil, metadata: nil, resolved: nil]

defimpl Jason.Encoder do
@impl Jason.Encoder
def encode(value, opts) do
defimpl JSON.Encoder do
@impl JSON.Encoder
def encode(value, encoder) do
value
|> Map.from_struct()
|> Enum.reject(&match?({_key, nil}, &1))
|> Map.new()
|> Jason.Encode.map(opts)
|> encoder.(encoder)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ defmodule MixDependencySubmission.Submission.Manifest.Dependency do
@enforce_keys []
defstruct package_url: nil, metadata: nil, relationship: nil, scope: nil, dependencies: nil

defimpl Jason.Encoder do
@impl Jason.Encoder
def encode(value, opts) do
defimpl JSON.Encoder do
@impl JSON.Encoder
def encode(value, encoder) do
value
|> Map.from_struct()
|> update_in([:package_url], &purl_to_string/1)
|> update_in([:dependencies], &List.wrap/1)
|> update_in([:dependencies, Access.all()], &purl_to_string/1)
|> Enum.reject(fn {_key, value} -> value in [nil, []] end)
|> Map.new()
|> Jason.Encode.map(opts)
|> encoder.(encoder)
end

@spec purl_to_string(purl :: Purl.t()) :: String.t()
Expand Down
8 changes: 4 additions & 4 deletions lib/mix_dependency_submission/submission/manifest/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ defmodule MixDependencySubmission.Submission.Manifest.File do
@enforce_keys []
defstruct source_location: nil

defimpl Jason.Encoder do
@impl Jason.Encoder
def encode(value, opts) do
defimpl JSON.Encoder do
@impl JSON.Encoder
def encode(value, encoder) do
value
|> Map.from_struct()
|> Enum.reject(&match?({_key, nil}, &1))
|> Map.new()
|> Jason.Encode.map(opts)
|> encoder.(encoder)
end
end
end
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ defmodule MixDependencySubmission.MixProject do
{:ex_doc, ">= 0.0.0", only: [:dev], runtime: false},
{:excoveralls, "~> 0.5", only: [:test], runtime: false},
{:hex, github: "hexpm/hex", tag: "v2.1.1", runtime: false},
{:jason, "~> 1.4"},
{:optimus, "~> 0.2"},
{:plug, "~> 1.0", only: [:test]},
{:purl, "~> 0.2.0"},
Expand Down
4 changes: 2 additions & 2 deletions test/mix_dependency_submission/submission/detector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule MixDependencySubmission.Submission.DetectorTest do

doctest Detector

describe "Jason.Encoder" do
describe "JSON.Encoder" do
test "encodes filled struct" do
detector = %Detector{
name: "test",
Expand All @@ -14,7 +14,7 @@ defmodule MixDependencySubmission.Submission.DetectorTest do
}

assert %{"name" => "test", "url" => "http://example.com", "version" => "1.0.0"} =
detector |> Jason.encode!() |> Jason.decode!()
detector |> JSON.encode!() |> JSON.decode!()
end
end
end
6 changes: 3 additions & 3 deletions test/mix_dependency_submission/submission/job_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule MixDependencySubmission.Submission.JobTest do

doctest Job

describe "Jason.Encoder" do
describe "JSON.Encoder" do
test "encodes filled struct" do
job = %Job{
id: "test",
Expand All @@ -14,7 +14,7 @@ defmodule MixDependencySubmission.Submission.JobTest do
}

assert %{"correlator" => "test", "html_url" => "http://example.com", "id" => "test"} =
job |> Jason.encode!() |> Jason.decode!()
job |> JSON.encode!() |> JSON.decode!()
end

test "encodes partial struct" do
Expand All @@ -24,7 +24,7 @@ defmodule MixDependencySubmission.Submission.JobTest do
}

assert %{"correlator" => "test", "id" => "test"} =
job |> Jason.encode!() |> Jason.decode!()
job |> JSON.encode!() |> JSON.decode!()
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule MixDependencySubmission.Submission.Manifest.DependencyTest do

doctest Dependency

describe "Jason.Encoder" do
describe "JSON.Encoder" do
test "encodes filled struct" do
dependency = %Dependency{
package_url: %Purl{type: "hex", name: "gettext"},
Expand All @@ -23,13 +23,13 @@ defmodule MixDependencySubmission.Submission.Manifest.DependencyTest do
"package_url" => "pkg:hex/gettext",
"relationship" => "direct",
"scope" => "runtime"
} = dependency |> Jason.encode!() |> Jason.decode!()
} = dependency |> JSON.encode!() |> JSON.decode!()
end

test "encodes empty struct" do
dependency = %Dependency{}

assert %{} == dependency |> Jason.encode!() |> Jason.decode!()
assert %{} == dependency |> JSON.encode!() |> JSON.decode!()
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ defmodule MixDependencySubmission.Submission.Manifest.FileTest do

doctest File

describe "Jason.Encoder" do
describe "JSON.Encoder" do
test "encodes filled struct" do
file = %File{
source_location: "mix.exs"
}

assert %{"source_location" => "mix.exs"} = file |> Jason.encode!() |> Jason.decode!()
assert %{"source_location" => "mix.exs"} = file |> JSON.encode!() |> JSON.decode!()
end

test "encodes empty struct" do
file = %File{}

assert %{} == file |> Jason.encode!() |> Jason.decode!()
assert %{} == file |> JSON.encode!() |> JSON.decode!()
end
end
end
6 changes: 3 additions & 3 deletions test/mix_dependency_submission/submission/manifest_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule MixDependencySubmission.Submission.ManifestTest do

doctest Manifest

describe "Jason.Encoder" do
describe "JSON.Encoder" do
test "encodes filled struct" do
manifest = %Manifest{
name: "test",
Expand All @@ -21,15 +21,15 @@ defmodule MixDependencySubmission.Submission.ManifestTest do
"name" => "test",
"resolved" => %{}
} =
manifest |> Jason.encode!() |> Jason.decode!()
manifest |> JSON.encode!() |> JSON.decode!()
end

test "encodes partial struct" do
manifest = %Manifest{
name: "test"
}

assert %{"name" => "test"} = manifest |> Jason.encode!() |> Jason.decode!()
assert %{"name" => "test"} = manifest |> JSON.encode!() |> JSON.decode!()
end
end
end
4 changes: 2 additions & 2 deletions test/mix_dependency_submission/submission_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule MixDependencySubmission.SubmissionTest do

doctest Submission

describe "Jason.Encoder" do
describe "JSON.Encoder" do
test "encodes filled struct" do
datetime = DateTime.utc_now()

Expand Down Expand Up @@ -40,7 +40,7 @@ defmodule MixDependencySubmission.SubmissionTest do
"sha" => "sha",
"version" => 0
} =
submission |> Jason.encode!() |> Jason.decode!()
submission |> JSON.encode!() |> JSON.decode!()
end
end
end
Loading