diff --git a/lib/mix/tasks/compile.thrift.ex b/lib/mix/tasks/compile.thrift.ex index c2416845..016223ee 100644 --- a/lib/mix/tasks/compile.thrift.ex +++ b/lib/mix/tasks/compile.thrift.ex @@ -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 @@ -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 @@ -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} @@ -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. diff --git a/lib/mix/tasks/thrift.generate.ex b/lib/mix/tasks/thrift.generate.ex index 60e2e76a..c4d1c78d 100644 --- a/lib/mix/tasks/thrift.generate.ex +++ b/lib/mix/tasks/thrift.generate.ex @@ -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) @@ -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)) diff --git a/lib/thrift/generator.ex b/lib/thrift/generator.ex index d56e3375..10f65dc6 100644 --- a/lib/thrift/generator.ex +++ b/lib/thrift/generator.ex @@ -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 @@ -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) @@ -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([ @@ -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)} diff --git a/lib/thrift/generator/binary/framed/http/server.ex b/lib/thrift/generator/binary/framed/http/server.ex index f816c204..c160d595 100644 --- a/lib/thrift/generator/binary/framed/http/server.ex +++ b/lib/thrift/generator/binary/framed/http/server.ex @@ -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") diff --git a/lib/thrift/generator/service.ex b/lib/thrift/generator/service.ex index 1e4989c1..83feb294 100644 --- a/lib/thrift/generator/service.ex +++ b/lib/thrift/generator/service.ex @@ -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) diff --git a/lib/thrift/generator/test_data_generator.ex b/lib/thrift/generator/test_data_generator.ex index f1a31399..81e9248c 100644 --- a/lib/thrift/generator/test_data_generator.ex +++ b/lib/thrift/generator/test_data_generator.ex @@ -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() @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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__ @@ -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 @@ -268,6 +278,7 @@ defmodule Thrift.Generator.TestDataGenerator do anno |> String.to_atom() |> Macro.var(nil) + quote do ^unquote(var_name) end @@ -280,6 +291,7 @@ defmodule Thrift.Generator.TestDataGenerator do "^" <> v -> annotation_to_pin(v) v -> v end + {k, v} end @@ -291,5 +303,4 @@ defmodule Thrift.Generator.TestDataGenerator do [unquote_splicing(props_from_annotations)] end end - end diff --git a/lib/thrift/generator/test_data_generator/struct.ex b/lib/thrift/generator/test_data_generator/struct.ex index f1c4c8fb..6609c970 100644 --- a/lib/thrift/generator/test_data_generator/struct.ex +++ b/lib/thrift/generator/test_data_generator/struct.ex @@ -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 @@ -149,5 +158,4 @@ defmodule Thrift.Generator.TestDataGenerator.Struct do end end end - end diff --git a/lib/thrift/parser/file_group.ex b/lib/thrift/parser/file_group.ex index a577fbc2..0a249b9a 100644 --- a/lib/thrift/parser/file_group.ex +++ b/lib/thrift/parser/file_group.ex @@ -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`. @@ -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 ->