Skip to content

Commit

Permalink
refinement for option fields in struct
Browse files Browse the repository at this point in the history
only affect test_data
  • Loading branch information
Ecialo committed Dec 16, 2020
1 parent 6bf05be commit 5532a0d
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 63 deletions.
22 changes: 8 additions & 14 deletions lib/mix/tasks/compile.thrift.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ defmodule Mix.Tasks.Compile.Thrift do
config = Keyword.get(Mix.Project.config(), :thrift, [])
input_files = Keyword.get(config, :files, [])
output_path = Keyword.get(config, :output_path, "lib")
output_test_data = Keyword.get(
config,
:output_test_data,
"test/test_data/"
)# |> IO.inspect(label: "output test data")

output_test_data =
Keyword.get(
config,
:output_test_data,
"test/test_data/"
)

parser_opts =
config
Expand Down Expand Up @@ -127,13 +128,11 @@ defmodule Mix.Tasks.Compile.Thrift do
@spec extract_targets([FileGroup.t()], {Path.t(), Path.t()}, boolean) :: mappings
defp extract_targets(groups, {output_path, _}, force) when is_list(groups) do
for %FileGroup{initial_file: file} = group <- groups do
# IO.puts("ololo")
targets =
group
# |> IO.inspect(label: "group for extract")
|> Thrift.Generator.targets()
|> Enum.map(&Path.join(output_path, &1))
# IO.puts("keke")

if force || Mix.Utils.stale?([file], targets) do
{:stale, group, targets}
else
Expand All @@ -149,7 +148,7 @@ defmodule Mix.Tasks.Compile.Thrift do
verbose = opts[:verbose]

# Load the list of previously-generated files.
previous = read_manifest(manifest |> IO.inspect(label: "manifest")) |> IO.inspect(label: "previous")
previous = read_manifest(manifest)

# Determine which of our current targets are in need of (re)generation.
stale = for {:stale, group, targets} <- mappings, do: {group, targets}
Expand All @@ -173,15 +172,10 @@ defmodule Mix.Tasks.Compile.Thrift do
unless Enum.empty?(stale) do
Mix.Utils.compiling_n(length(stale), :thrift)

# IO.inspect(stale, lable: "stale")

Enum.each(stale, fn {group, _targets} ->
# IO.inspect(output, label: "output")
Thrift.Generator.generate!(group, output)
IO.puts("after gen")
verbose && Mix.shell().info("Compiled #{group.initial_file}")
end)

end

# Update and rewrite the manifest.
Expand Down
11 changes: 9 additions & 2 deletions lib/mix/tasks/thrift.generate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ defmodule Mix.Tasks.Thrift.Generate do
{opts, files} =
OptionParser.parse!(
args,
switches: [include: :keep, namespace: :string, out: :string, out_test: :string, verbose: :boolean],
switches: [
include: :keep,
namespace: :string,
out: :string,
out_test: :string,
verbose: :boolean
],
aliases: [I: :include, o: :out, v: :verbose]
)

config = Keyword.get(Mix.Project.config(), :thrift, [])
output_path = opts[:out] || Keyword.get(config, :output_path, "lib")
output_test_data = opts[:out_test] || Keyword.get(config, :output_test_data, "lib") #|> IO.inspect(label: "test_data")
output_test_data = opts[:out_test] || Keyword.get(config, :output_test_data, "lib")

namespace = opts[:namespace] || Keyword.get(config, :namespace)

Expand All @@ -74,6 +80,7 @@ defmodule Mix.Tasks.Thrift.Generate do
Keyword.new()
|> Keyword.put(:include_paths, include_paths)
|> Keyword.put(:namespace, namespace)

unless Enum.empty?(files) do
File.mkdir_p!(output_path)
Enum.each(files, &generate!(&1, output_path, parser_opts, opts))
Expand Down
19 changes: 0 additions & 19 deletions lib/thrift/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ defmodule Thrift.Generator do
|> generate_schema
|> hd()
|> Enum.map(fn {name, _} -> target_path(name) end)

# |> IO.inspect(label: "target paths")
end)
end

Expand All @@ -43,8 +41,6 @@ defmodule Thrift.Generator do
end

def generate!(%FileGroup{} = file_group, [_output_dir, _output_test_data] = output) do
IO.inspect(file_group.schemas, label: "fg")

Enum.flat_map(file_group.schemas, fn {_, schema} ->
schema
|> Map.put(:file_group, file_group)
Expand All @@ -67,7 +63,6 @@ defmodule Thrift.Generator do
def generate_schema(schema) do
current_module_file_group = FileGroup.set_current_module(schema.file_group, schema.module)
schema = %Schema{schema | file_group: current_module_file_group}
IO.inspect(schema, label: "schema")

modules =
List.flatten([
Expand Down Expand Up @@ -185,26 +180,12 @@ defmodule Thrift.Generator do
typedef = key |> Atom.to_string() |> String.capitalize()
name = "#{thrift_module}.#{typedef}" |> String.to_atom()

# typedef_name =
# Atom.to_string(schema.module) <>
# "." <> (key |> Atom.to_string() |> String.capitalize())

# IO.inspect(schema, limit: 10, label: "schema")

# IO.inspect(ss)
FileGroup.dest_module(schema.file_group, name)

# |> IO.inspect(label: "full_name")

_otherwise ->
FileGroup.dest_module(schema.file_group, struct)
end

# if label == :typedef do
# IO.inspect(ss)
# end
# full_name = FileGroup.dest_module(schema.file_group, struct)
# |> IO.inspect()
full_test_data_name = TestDataGenerator.test_data_module_from_data_module(full_name)

{full_test_data_name, TestDataGenerator.generate(label, schema, full_name, struct)}
Expand Down
1 change: 0 additions & 1 deletion lib/thrift/generator/binary/framed/http/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ defmodule Thrift.Generator.Binary.Framed.HTTP.Server do
{:ok, payload, conn} <- Plug.Conn.read_body(conn),
{:ok, parsed} <- Protocol.Binary.deserialize(:message_begin, payload)
) do
IO.inspect(parsed)
handle_thrift_message(conn, parsed, opts)
else
{:error, _} -> send_resp(conn, 500, "bad thrift")
Expand Down
2 changes: 0 additions & 2 deletions lib/thrift/generator/service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ defmodule Thrift.Generator.Service do
@dialyzer [{:nowarn_function, generate: 2}, {:nowarn_function, generate_response_struct: 2}]

def generate(schema, service) do
# IO.inspect(schema, label: "SCHEMA")
# IO.inspect(service, label: "SERVICE")
file_group = schema.file_group
dest_module = FileGroup.dest_module(file_group, service)

Expand Down
51 changes: 31 additions & 20 deletions lib/thrift/generator/test_data_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule Thrift.Generator.TestDataGenerator do
end

def get_generator(type, file_group, annotations \\ %{})

def get_generator(:bool, _, _) do
quote do
bool()
Expand Down Expand Up @@ -56,6 +57,7 @@ defmodule Thrift.Generator.TestDataGenerator do
def get_generator(:i16, _, annotations) do
min = refinement_from_annotations(annotations, :min, &parse_integer/1, -32_768)
max = refinement_from_annotations(annotations, :max, &parse_integer/1, 32_767)

quote do
integer(unquote(min), unquote(max))
end
Expand All @@ -64,14 +66,19 @@ defmodule Thrift.Generator.TestDataGenerator do
def get_generator(:i32, _, annotations) do
min = refinement_from_annotations(annotations, :min, &parse_integer/1, -2_147_483_648)
max = refinement_from_annotations(annotations, :max, &parse_integer/1, 2_147_483_647)

quote do
integer(unquote(min), unquote(max))
end
end

def get_generator(:i64, _, annotations) do
min = refinement_from_annotations(annotations, :min, &parse_integer/1, -9_223_372_036_854_775_808)
max = refinement_from_annotations(annotations, :max, &parse_integer/1, 9_223_372_036_854_775_807)
min =
refinement_from_annotations(annotations, :min, &parse_integer/1, -9_223_372_036_854_775_808)

max =
refinement_from_annotations(annotations, :max, &parse_integer/1, 9_223_372_036_854_775_807)

quote do
integer(unquote(min), unquote(max))
end
Expand All @@ -80,6 +87,7 @@ defmodule Thrift.Generator.TestDataGenerator do
def get_generator(:double, _, annotations) do
min = refinement_from_annotations(annotations, :min, &parse_float/1, :inf)
max = refinement_from_annotations(annotations, :max, &parse_float/1, :inf)

quote do
float(unquote(min), unquote(max))
end
Expand All @@ -104,15 +112,14 @@ defmodule Thrift.Generator.TestDataGenerator do
end

def get_generator({:map, {k, v}}, file_group, annotations) do
separate = fn {k, v} ->
[type, key] =
k
|> Atom.to_string()
|> String.split("_", parts: 2)

separate =
fn {k, v} ->
[type, key] =
k
|> Atom.to_string()
|> String.split("_", parts: 2)
{type, {String.to_atom(key), v}}
end
{type, {String.to_atom(key), v}}
end

separated_annotations =
annotations
Expand Down Expand Up @@ -162,6 +169,7 @@ defmodule Thrift.Generator.TestDataGenerator do
|> test_data_module_from_data_module

props = gen_props(annotations)

quote do
unquote(dest_module).get_generator(context, unquote(props))
end
Expand All @@ -182,19 +190,20 @@ defmodule Thrift.Generator.TestDataGenerator do
FileGroup.dest_module(file_group, name)
|> test_data_module_from_data_module


props = gen_props(annotations)

quote do
unquote(dest_module).get_generator(context, unquote(props))
end
end

def get_generator(%Typedef{name: name}, file_group, annotations) do
def get_generator(%Typedef{} = typedef, file_group, annotations) do
dest_module =
FileGroup.dest_module(file_group, name)
FileGroup.dest_module(file_group, typedef)
|> test_data_module_from_data_module

props = gen_props(annotations)

quote do
unquote(dest_module).get_generator(context, unquote(props))
end
Expand All @@ -207,6 +216,7 @@ defmodule Thrift.Generator.TestDataGenerator do
def apply_defaults(struct_) do
apply_defaults(struct_, nil)
end

def apply_defaults(struct_, context) when is_struct(struct_) do
module_name =
struct_.__struct__
Expand Down Expand Up @@ -247,12 +257,12 @@ defmodule Thrift.Generator.TestDataGenerator do

defp refinement_from_annotations(annotations, field, parse_fun, default) do
annotations
|> Map.get(field)
|> case do
nil -> default
"^" <> v -> annotation_to_pin(v)
v -> parse_fun.(v)
end
|> Map.get(field)
|> case do
nil -> default
"^" <> v -> annotation_to_pin(v)
v -> parse_fun.(v)
end
end

defp parse_integer(str) do
Expand All @@ -268,6 +278,7 @@ defmodule Thrift.Generator.TestDataGenerator do
anno
|> String.to_atom()
|> Macro.var(nil)

quote do
^unquote(var_name)
end
Expand All @@ -280,6 +291,7 @@ defmodule Thrift.Generator.TestDataGenerator do
"^" <> v -> annotation_to_pin(v)
v -> v
end

{k, v}
end

Expand All @@ -291,5 +303,4 @@ defmodule Thrift.Generator.TestDataGenerator do
[unquote_splicing(props_from_annotations)]
end
end

end
16 changes: 12 additions & 4 deletions lib/thrift/generator/test_data_generator/struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,20 @@ defmodule Thrift.Generator.TestDataGenerator.Struct do
end

def gen_draw(field_ast, file_group) do
# IO.inspect(field_ast.annotations, label: "anno")
generator = TestDataGenerator.get_generator(field_ast.type, file_group, field_ast.annotations)
annotations = field_ast.annotations
draw_anno = Map.drop(annotations, [:act])
generator = TestDataGenerator.get_generator(field_ast.type, file_group, draw_anno)

declared_required =
case field_ast.required do
:default -> true
otherwise -> otherwise
end

actually_required = Map.get(field_ast.annotations, :act) == "required"

generator =
if field_ast.required do
if declared_required or actually_required do
generator
else
quote do
Expand Down Expand Up @@ -149,5 +158,4 @@ defmodule Thrift.Generator.TestDataGenerator.Struct do
end
end
end

end
6 changes: 5 additions & 1 deletion lib/thrift/parser/file_group.ex
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ defmodule Thrift.Parser.FileGroup do
dest_module(file_group, name)
end

def dest_module(file_group, %Typedef{name: name}) do
dest_module(file_group, name)
end

def dest_module(file_group, Constant) do
# Default to naming the constants module after the namespaced, camelized
# basename of its file. For foo.thrift, this would be `foo.Foo`.
Expand Down Expand Up @@ -223,7 +227,7 @@ defmodule Thrift.Parser.FileGroup do
struct_name =
name_parts
|> Enum.at(1)
|> initialcase()
|> Macro.camelize()

case file_group.namespaces[module_name] do
nil ->
Expand Down

0 comments on commit 5532a0d

Please sign in to comment.