Skip to content

JSON Module Support for Elixir 1.18 and CI Environment Updates #210

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ on:

jobs:
test:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
env:
MIX_ENV: test
strategy:
fail-fast: false
matrix:
include:
- pair:
elixir: "1.13"
otp: "22"
elixir: "1.16"
otp: "26"
lint: lint
- pair:
elixir: "1.17"
otp: "27"
lint: lint
- pair:
elixir: "1.18"
otp: "27"
lint: lint
steps:
- uses: actions/checkout@v4

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Add `kubernetes_use_cached_resources` option to Kubernetes strategy
* Use Elixir 1.18's built-in JSON module when available

## 3.4.1

Expand Down
18 changes: 18 additions & 0 deletions lib/json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Cluster.JSON do
@moduledoc false

# Delegates to JSON in Elixir v1.18+ or Jason for earlier versions

cond do
Code.ensure_loaded?(JSON) ->
defdelegate decode!(data), to: JSON

Code.ensure_loaded?(Jason) ->
defdelegate decode!(data), to: Jason

true ->
message = "Missing a compatible JSON library, add `:jason` to your deps."

IO.warn(message, Macro.Env.stacktrace(__ENV__))
end
end
4 changes: 2 additions & 2 deletions lib/strategy/kubernetes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ defmodule Cluster.Strategy.Kubernetes do

case :httpc.request(:get, {~c"https://#{master}/#{path}", headers}, http_options, []) do
{:ok, {{_version, 200, _status}, _headers, body}} ->
parse_response(ip_lookup_mode, Jason.decode!(body))
parse_response(ip_lookup_mode, Cluster.JSON.decode!(body))
|> Enum.map(fn node_info ->
format_node(
Keyword.get(config, :mode, :ip),
Expand All @@ -421,7 +421,7 @@ defmodule Cluster.Strategy.Kubernetes do
end)

{:ok, {{_version, 403, _status}, _headers, body}} ->
%{"message" => msg} = Jason.decode!(body)
%{"message" => msg} = Cluster.JSON.decode!(body)
warn(topology, "cannot query kubernetes (unauthorized): #{msg}")
[]

Expand Down
2 changes: 1 addition & 1 deletion lib/strategy/rancher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ defmodule Cluster.Strategy.Rancher do
[]
) do
{:ok, {{_version, 200, _status}, _headers, body}} ->
parse_response(app_name, Jason.decode!(body))
parse_response(app_name, Cluster.JSON.decode!(body))

{:ok, {{_version, code, status}, _headers, body}} ->
warn(
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Cluster.Mixfile do
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:dialyxir, "~> 1.0", only: :dev, runtime: false},
{:exvcr, "~> 0.11", only: :test, runtime: false},
{:jason, "~> 1.1"},
{:jason, "~> 1.1", optional: true},
{:telemetry, "~> 1.3"}
]
end
Expand Down