Skip to content
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
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ otp_release:
- 17.4
- 18.2.1
elixir:
- 1.1.1
- 1.2.3
- 1.3.1
sudo: required
Expand All @@ -15,5 +14,4 @@ before_script:
script:
- cd $TRAVIS_BUILD_DIR
- mix compile
- THRIFT='docker run -v "$PWD:/thrash" -w /thrash thrift:0.9.3 thrift' THRIFT_INPUT_DIR=test/ mix compile.thrift
- mix test
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,6 @@ usage details. Note, both of these mixins accept a `source` argument
to allow you to manually define the source structure in your Thrift
IDL.

## Mix.Tasks.Compile.Thrift

Thrash provides the `compile.thrift` mix task to help with compiling
Elixir projects that use Thrift and Thrash.

See [lib/mix/tasks/compile/thrift.ex](lib/mix/tasks/compile/thrift.ex)
for detailed documentation of the compile task, including how to
modify your mix.exs file so that this task runs automatically.

## Data Types

Thrift data types are mapped to Elixir as follows.
Expand Down Expand Up @@ -238,11 +229,7 @@ source from the test thrift file.
```
# fetch deps
mix deps.fetch
# make sure the compile.thrift task is available
mix compile
# compile thrift
THRIFT_INPUT_DIR=test/ mix compile.thrift
# now tests should work
mix test
```

Expand Down
32 changes: 7 additions & 25 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.
# If this ever gets more complex, switch to separate .exs files
defmodule Config do
def idl_files(:test), do: Path.wildcard("test/thrift/**/*.thrift")
def idl_files(_), do: []
end

# You can configure for your application as:
#
# config :thrash, key: :value
#
# And access this configuration in your application as:
#
# Application.get_env(:thrash, :key)
#
# Or configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
config :thrash,
idl_files: Config.idl_files(Mix.env)
128 changes: 0 additions & 128 deletions lib/mix/tasks/compile/thrift.ex

This file was deleted.

18 changes: 7 additions & 11 deletions lib/thrash/constants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,18 @@ defmodule Thrash.Constants do
`MyApp.Constants.magic_number/0`.
"""

alias Thrash.IDL
alias Thrash.ThriftMeta

defmacro __using__(_opts \\ []) do
constants = ThriftMeta.erl_gen_path
|> ThriftMeta.constants_headers
|> Enum.map(fn(header) ->
namespace = ThriftMeta.constants_namespace(header)

header
|> ThriftMeta.read_constants_exclusive
|> ThriftMeta.thrashify_constants(namespace)
end)
|> List.flatten
caller = __CALLER__.module
caller_namespace = Thrash.MacroHelpers.find_namespace(caller)

constants = IDL.parse
|> ThriftMeta.read_constants(caller_namespace)

Enum.map(constants, fn({k, v}) ->
defconst(k, v)
defconst(k, Macro.escape(v))
end)
end

Expand Down
17 changes: 5 additions & 12 deletions lib/thrash/enumerated.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ defmodule Thrash.Enumerated do
"""

alias Thrash.ThriftMeta
alias Thrash.IDL
alias Thrash.MacroHelpers

# there is probably a more idiomatic way to do a lot of this..
Expand All @@ -60,7 +61,10 @@ defmodule Thrash.Enumerated do
# if you get ":enum_not_found" here, it indicates that the enum
# you were looking for does not exist in the thrift-generated
# erlang code
quoted_map = module |> find_in_thrift |> ensure_quoted
quoted_map = IDL.parse
|> ThriftMeta.read_enum(module)
|> Macro.escape

map_keys = get_keys(quoted_map)
map_values = get_values(quoted_map)
reversed = build_reverse(quoted_map)
Expand Down Expand Up @@ -112,21 +116,10 @@ defmodule Thrash.Enumerated do
end
end

defp find_in_thrift(modname) do
ThriftMeta.find_in_thrift(fn(h) ->
ThriftMeta.read_enum(h, modname)
end, :enum_not_found)
end

defp reverse_kv(kv) do
Enum.map(kv, fn({k, v}) -> {v, k} end)
end

defp ensure_quoted(m) when is_map(m) do
{:%{}, [line: __ENV__.line], Enum.into(m, [])}
end
defp ensure_quoted(m), do: m

defp build_reverse({:%{}, line, kv}) do
{:%{}, line, reverse_kv(kv)}
end
Expand Down
57 changes: 57 additions & 0 deletions lib/thrash/idl.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule Thrash.IDL do
alias Thrift.Parser
alias Thrift.Parser.Models

def parse do
parse(Application.get_env(:thrash, :idl_files))
end

def parse(junk) when junk == [] or is_nil(junk) do
raise ArgumentError, message: "No IDL files found."
end
def parse(idl_files) do
idl_files
|> Enum.reduce(%Models.Schema{}, fn(path, full_schema) ->
file_idl = Parser.parse(File.read!(path))
merge(full_schema, file_idl)
end)
end

def merge(
accum = %Models.Schema{},
el = %Models.Schema{}) do
%{accum |
constants: merge_maps(accum.constants, el.constants),
enums: merge_maps(accum.enums, el.enums),
exceptions: merge_maps(accum.exceptions, el.exceptions),
includes: merge_includes(accum.includes, el.includes),
namespaces: merge_maps(accum.namespaces, el.namespaces),
services: merge_maps(accum.services, el.services),
structs: merge_maps(accum.structs, el.structs),
thrift_namespace: merge_namespaces(
accum.thrift_namespace,
el.thrift_namespace
),
typedefs: merge_maps(accum.typedefs, el.typedefs),
unions: merge_maps(accum.unions, el.unions)
}
end

def constants(idl = %Models.Schema{}) do
Map.get(idl, :constants)
end

defp merge_maps(m1, m2) do
Map.merge(m1, m2)
end

defp merge_includes(i1, i2) do
i1 ++ i2
end

defp merge_namespaces(nil, nil), do: nil
defp merge_namespaces(s1, s2) do
IO.puts("NAMESPACES: #{inspect s1} #{inspect s2}")
s2
end
end
19 changes: 19 additions & 0 deletions lib/thrash/macro_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ defmodule Thrash.MacroHelpers do
quoted_chained_or(rest, {:|, [], [b, a]})
end

@doc """
Convert an elixir module to an atom (without the Elixir prefix)
"""
@spec unelixir_atom(atom) :: atom
def unelixir_atom(module_name) do
parts = module_name
|> Atom.to_string
|> String.split(".")

unelixired_parts = case parts do
["Elixir" | rest] -> rest
other -> other
end

unelixired_parts
|> Enum.join(".")
|> String.to_atom
end

defp quoted_chained_or([], ast) do
ast
end
Expand Down
Loading