Skip to content

Commit

Permalink
Merge pull request #170 from tom-and-the-toothfairies/encode-ast
Browse files Browse the repository at this point in the history
tidies up Ast encoding and decoding
  • Loading branch information
c-brenn authored Mar 26, 2017
2 parents 6d25953 + fb202d4 commit 1d7943f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 17 deletions.
6 changes: 1 addition & 5 deletions panacea/lib/panacea/pml/analysis.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ defmodule Panacea.Pml.Analysis do
defstruct [:ast, :clashes, :drugs, :unnamed]

def run(ast) do
encoded_ast =
ast
|> :erlang.term_to_binary()
|> Base.encode64()

encoded_ast = Panacea.Pml.Ast.encode(ast)
clashes = Clashes.run(ast)
drugs = Drugs.run(ast)
unnamed = Unnamed.run(ast)
Expand Down
6 changes: 6 additions & 0 deletions panacea/lib/panacea/pml/ast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ defmodule Panacea.Pml.Ast do
do_unquote(ast, 0)
end

def encode(ast) do
ast
|> :erlang.term_to_binary()
|> Base.encode64()
end

def decode(ast) do
ast
|> decode64()
Expand Down
4 changes: 1 addition & 3 deletions panacea/test/controllers/asclepius_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ defmodule Panacea.AsclepiusControllerTest do
}
"""
{:ok, ast} = Panacea.Pml.Parser.parse(pml)
ast
|> :erlang.term_to_binary()
|> Base.encode64()
Panacea.Pml.Ast.encode(ast)
end

describe "AsclepiusController.uris_for_labels/2" do
Expand Down
6 changes: 1 addition & 5 deletions panacea/test/controllers/ast_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ defmodule Panacea.AstControllerTest do
|> String.replace_trailing("\n", "")

{:ok, ast} = Panacea.Pml.Parser.parse(pml)

encoded_ast =
ast
|> :erlang.term_to_binary()
|> Base.encode64()
encoded_ast = Panacea.Pml.Ast.encode(ast)

conn = get conn, ast_path(conn, :to_pml), %{ast: encoded_ast}

Expand Down
5 changes: 2 additions & 3 deletions panacea/test/controllers/pml_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,11 @@ defmodule Panacea.PmlControllerTest do
|> File.read!()
|> Panacea.Pml.Parser.parse()

received_ast =
{:ok, received_ast} =
conn
|> response_body()
|> Map.get("ast")
|> Base.decode64!()
|> :erlang.binary_to_term()
|> Panacea.Pml.Ast.decode()

assert received_ast == ast
end
Expand Down
3 changes: 2 additions & 1 deletion panacea/web/controllers/ast_controller.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule Panacea.AstController do
use Panacea.Web, :controller

def to_pml(conn, %{"ast" => encoded_ast}) do encoded_ast
def to_pml(conn, %{"ast" => encoded_ast}) do
encoded_ast
|> Panacea.Pml.Ast.decode()
|> to_pml()
|> respond(conn)
Expand Down

0 comments on commit 1d7943f

Please sign in to comment.