Skip to content

feat: json encoder for incoming introspection #121

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

Merged
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
28 changes: 27 additions & 1 deletion lib/astarte_core/triggers/simple_events/encoder.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Astarte.
#
# Copyright 2018 Ispirata Srl
# Copyright 2018 - 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -87,9 +87,35 @@ defmodule Astarte.Core.Triggers.SimpleEvents.Encoder do
end
end

defimpl Jason.Encoder, for: SimpleEvents.InterfaceVersion do
alias SimpleEvents.InterfaceVersion

def encode(interface_version, opts) do
%InterfaceVersion{major: major, minor: minor} = interface_version

%{
"major" => major,
"minor" => minor
}
|> Jason.Encoder.encode(opts)
end
end

defimpl Jason.Encoder, for: SimpleEvents.IncomingIntrospectionEvent do
alias Astarte.Core.Triggers.SimpleEvents.IncomingIntrospectionEvent

def encode(%IncomingIntrospectionEvent{introspection: nil} = event, opts) do
%IncomingIntrospectionEvent{
introspection_map: introspection_map
} = event

%{
"type" => "incoming_introspection",
"introspection" => introspection_map
}
|> Jason.Encoder.encode(opts)
end

def encode(%IncomingIntrospectionEvent{} = event, opts) do
%IncomingIntrospectionEvent{
introspection: introspection
Expand Down
30 changes: 29 additions & 1 deletion test/astarte_core/triggers/simple_events_encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ defmodule Astarte.Core.SimpleEventsEncoderTest do
}
end

test "works for IncomingIntrospectionEvent" do
test "works for IncomingIntrospectionEvent with string introspection" do
alias Astarte.Core.Triggers.SimpleEvents.IncomingIntrospectionEvent

introspection = "com.example.Interface:0:2;com.example.AnotherInterface:1:1"
Expand All @@ -260,6 +260,34 @@ defmodule Astarte.Core.SimpleEventsEncoderTest do
}
end

test "works for IncomingIntrospectionEvent with map introspection" do
alias Astarte.Core.Triggers.SimpleEvents.IncomingIntrospectionEvent
alias Astarte.Core.Triggers.SimpleEvents.InterfaceVersion

introspection = %{
"com.example.Interface" => %InterfaceVersion{major: 0, minor: 2},
"com.example.AnotherInterface" => %InterfaceVersion{major: 1, minor: 1}
}

introspection_map = %{
"com.example.Interface" => %{"major" => 0, "minor" => 2},
"com.example.AnotherInterface" => %{"major" => 1, "minor" => 1}
}

event = %IncomingIntrospectionEvent{
introspection_map: introspection
}

roundtrip =
Jason.encode!(event)
|> Jason.decode!()

assert roundtrip == %{
"type" => "incoming_introspection",
"introspection" => introspection_map
}
end

test "works for InterfaceAddedEvent" do
alias Astarte.Core.Triggers.SimpleEvents.InterfaceAddedEvent

Expand Down
Loading