<commit_before>defmodule Thrift.Generator.StructGenerator do
  alias Thrift.Generator.{StructBinaryProtocol, Utils}
  alias Thrift.Parser.FileGroup
  alias Thrift.Parser.Models.{
    Exception,
    Field,
    Struct,
    TypeRef,
    TEnum,
    Union,
  }

  def generate(label, schema, name, struct) when label in [:struct, :union, :exception] do
    struct_parts = Enum.map(struct.fields, fn
      %Field{name: name, type: type, default: default} ->
        {name, Utils.quote_value(default, type, schema)}
    end)

    binary_protocol_defs = [
      StructBinaryProtocol.struct_serializer(struct, name, schema.file_group),
      StructBinaryProtocol.struct_deserializer(struct, name, schema.file_group),
    ]
    |> Utils.merge_blocks
    |> Utils.sort_defs

    define_block = case label do
      :exception ->
        quote do: defexception unquote(struct_parts)
      _ ->
        quote do: defstruct unquote(struct_parts)
    end

    extra_defs = if label == :exception and not Keyword.has_key?(struct_parts, :message) do
      quote do
        @spec message(Exception.t) :: String.t
        def message(exception), do: inspect(exception)
      end
    end

    quote do
      defmodule unquote(name) do
        _ = unquote "Auto-generated Thrift #{label} #{struct.name}"
        unquote_splicing(for field <- struct.fields do
          quote do
            _ = unquote "#{field.id}: #{to_thrift(field.type, schema.file_group)} #{field.name}"
          end
        end)
        unquote(define_block)
        @type t :: %__MODULE__{}
        def new, do: %__MODULE__{}
        unquote_splicing(List.wrap(extra_defs))
        defmodule BinaryProtocol do
          unquote_splicing(binary_protocol_defs)
        end
        def serialize(struct) do
          BinaryProtocol.serialize(struct)
        end
        def serialize(struct, :binary) do
          BinaryProtocol.serialize(struct)
        end
        def deserialize(binary) do
          BinaryProtocol.deserialize(binary)
        end
      end
    end
  end

  def to_thrift(base_type, _file_group) when is_atom(base_type) do
    Atom.to_string(base_type)
  end
  def to_thrift({:map, {key_type, val_type}}, file_group) do
    "map<#{to_thrift key_type, file_group},#{to_thrift val_type, file_group}>"
  end
  def to_thrift({:set, element_type}, file_group) do
    "set<#{to_thrift element_type, file_group}>"
  end
  def to_thrift({:list, element_type}, file_group) do
    "list<#{to_thrift element_type, file_group}>"
  end
  def to_thrift(%TEnum{name: name}, _file_group) do
    "#{name}"
  end
  def to_thrift(%Struct{name: name}, _file_group) do
    "#{name}"
  end
  def to_thrift(%Exception{name: name}, _file_group) do
    "#{name}"
  end
  def to_thrift(%Union{name: name}, _file_group) do
    "#{name}"
  end
  def to_thrift(%TypeRef{referenced_type: type}, file_group) do
    FileGroup.resolve(file_group, type) |> to_thrift(file_group)
  end
end
<commit_msg>Mark local to_thrift/2 functions as private (#267)<commit_after>defmodule Thrift.Generator.StructGenerator do
  alias Thrift.Generator.{StructBinaryProtocol, Utils}
  alias Thrift.Parser.FileGroup
  alias Thrift.Parser.Models.{
    Exception,
    Field,
    Struct,
    TypeRef,
    TEnum,
    Union,
  }

  def generate(label, schema, name, struct) when label in [:struct, :union, :exception] do
    struct_parts = Enum.map(struct.fields, fn
      %Field{name: name, type: type, default: default} ->
        {name, Utils.quote_value(default, type, schema)}
    end)

    binary_protocol_defs = [
      StructBinaryProtocol.struct_serializer(struct, name, schema.file_group),
      StructBinaryProtocol.struct_deserializer(struct, name, schema.file_group),
    ]
    |> Utils.merge_blocks
    |> Utils.sort_defs

    define_block = case label do
      :exception ->
        quote do: defexception unquote(struct_parts)
      _ ->
        quote do: defstruct unquote(struct_parts)
    end

    extra_defs = if label == :exception and not Keyword.has_key?(struct_parts, :message) do
      quote do
        @spec message(Exception.t) :: String.t
        def message(exception), do: inspect(exception)
      end
    end

    quote do
      defmodule unquote(name) do
        _ = unquote "Auto-generated Thrift #{label} #{struct.name}"
        unquote_splicing(for field <- struct.fields do
          quote do
            _ = unquote "#{field.id}: #{to_thrift(field.type, schema.file_group)} #{field.name}"
          end
        end)
        unquote(define_block)
        @type t :: %__MODULE__{}
        def new, do: %__MODULE__{}
        unquote_splicing(List.wrap(extra_defs))
        defmodule BinaryProtocol do
          @moduledoc false
          unquote_splicing(binary_protocol_defs)
        end
        def serialize(struct) do
          BinaryProtocol.serialize(struct)
        end
        def serialize(struct, :binary) do
          BinaryProtocol.serialize(struct)
        end
        def deserialize(binary) do
          BinaryProtocol.deserialize(binary)
        end
      end
    end
  end

  defp to_thrift(base_type, _file_group) when is_atom(base_type) do
    Atom.to_string(base_type)
  end
  defp to_thrift({:map, {key_type, val_type}}, file_group) do
    "map<#{to_thrift key_type, file_group},#{to_thrift val_type, file_group}>"
  end
  defp to_thrift({:set, element_type}, file_group) do
    "set<#{to_thrift element_type, file_group}>"
  end
  defp to_thrift({:list, element_type}, file_group) do
    "list<#{to_thrift element_type, file_group}>"
  end
  defp to_thrift(%TEnum{name: name}, _file_group) do
    "#{name}"
  end
  defp to_thrift(%Struct{name: name}, _file_group) do
    "#{name}"
  end
  defp to_thrift(%Exception{name: name}, _file_group) do
    "#{name}"
  end
  defp to_thrift(%Union{name: name}, _file_group) do
    "#{name}"
  end
  defp to_thrift(%TypeRef{referenced_type: type}, file_group) do
    FileGroup.resolve(file_group, type) |> to_thrift(file_group)
  end
end

{'fraction_non_alphanumeric': 0.1071488645920942, 'fraction_numerical': 0.000672834314550042, 'mean_word_length': 3.0448979591836736, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4502967', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1970, 'n_words': 442}