Enum and union behaviours (#130)

* Added Enums and unions to behvaiour
This commit is contained in:
Steve Cohen 2017-01-02 19:44:32 -08:00 committed by GitHub
parent 67a85df415
commit f2cb210282
2 changed files with 45 additions and 0 deletions

View File

@ -9,9 +9,12 @@ defmodule Thrift.Generator.Behaviour do
alias Thrift.Generator.Utils
alias Thrift.Parser.FileGroup
alias Thrift.Parser.Models.{
Exception,
Field,
Struct,
StructRef,
TEnum,
Union,
}
def generate(schema, service) do
@ -65,6 +68,23 @@ defmodule Thrift.Generator.Behaviour do
|> FileGroup.resolve(ref)
|> typespec(file_group)
end
defp typespec(%TEnum{}, _) do
quote do
non_neg_integer
end
end
defp typespec(%Union{name: name}, file_group) do
dest_module = FileGroup.dest_module(file_group, name)
quote do
%unquote(dest_module){}
end
end
defp typespec(%Exception{name: name}, file_group) do
dest_module = FileGroup.dest_module(file_group, name)
quote do
%unquote(dest_module){}
end
end
defp typespec(%Struct{name: name}, file_group) do
dest_module = FileGroup.dest_module(file_group, name)
quote do

View File

@ -6,6 +6,25 @@ defmodule BehaviourTest do
1: string username
}
struct T {
1: i64 id
}
union SorT {
1: S s_value,
2: T t_value
}
enum WaitStates {
WAITING
ACCEPTING
BLOCKED
}
exception MyEx {
1: string message
}
service BehaviourService {
void ping(1: i64 my_int),
void my_bool(1: bool my_bool),
@ -16,6 +35,9 @@ defmodule BehaviourTest do
map<string, bool> my_map2(1: map<string, map<string, string>> my_map)
void struct_param(1: S my_struct)
void myCamelCasedFunction(1: string camelParam);
WaitStates get_state();
SorT get_s_or_t();
MyEx dont_do_this();
}
"""
@ -31,5 +53,8 @@ defmodule BehaviourTest do
assert {:my_map2, 1} in behaviour_specs
assert {:struct_param, 1} in behaviour_specs
assert {:my_camel_cased_function, 1} in behaviour_specs
assert {:get_state, 0} in behaviour_specs
assert {:get_s_or_t, 0} in behaviour_specs
assert {:dont_do_this, 0} in behaviour_specs
end
end