|
| 1 | +defmodule JS2E.Printers.TuplePrinter do |
| 2 | + @behaviour JS2E.Printers.PrinterBehaviour |
| 3 | + @moduledoc """ |
| 4 | + A printer for printing a 'tuple' type decoder. |
| 5 | + """ |
| 6 | + |
| 7 | + @templates_location Application.get_env(:js2e, :templates_location) |
| 8 | + @type_location Path.join(@templates_location, "tuple/type.elm.eex") |
| 9 | + @decoder_location Path.join(@templates_location, "tuple/decoder.elm.eex") |
| 10 | + @encoder_location Path.join(@templates_location, "tuple/encoder.elm.eex") |
| 11 | + |
| 12 | + require Elixir.{EEx, Logger} |
| 13 | + import JS2E.Printers.Util |
| 14 | + alias JS2E.{Printer, TypePath, Types} |
| 15 | + alias JS2E.Types.{TupleType, SchemaDefinition} |
| 16 | + |
| 17 | + EEx.function_from_file(:defp, :type_template, @type_location, |
| 18 | + [:type_name, :type_fields]) |
| 19 | + |
| 20 | + EEx.function_from_file(:defp, :decoder_template, @decoder_location, |
| 21 | + [:decoder_name, :type_name, :clauses]) |
| 22 | + |
| 23 | + EEx.function_from_file(:defp, :encoder_template, @encoder_location, |
| 24 | + [:encoder_name, :type_name, :properties]) |
| 25 | + |
| 26 | + @impl JS2E.Printers.PrinterBehaviour |
| 27 | + @spec print_type(Types.typeDefinition, SchemaDefinition.t, |
| 28 | + Types.schemaDictionary) :: String.t |
| 29 | + def print_type(%TupleType{name: name, |
| 30 | + path: _path, |
| 31 | + items: types}, schema_def, schema_dict) do |
| 32 | + |
| 33 | + type_name = upcase_first name |
| 34 | + type_fields = create_type_fields(types, schema_def, schema_dict) |
| 35 | + |
| 36 | + type_template(type_name, type_fields) |
| 37 | + end |
| 38 | + |
| 39 | + @spec create_type_fields([TypePath.t], SchemaDefinition.t, |
| 40 | + Types.schemaDictionary) :: [String.t] |
| 41 | + defp create_type_fields(types, schema_def, schema_dict) do |
| 42 | + types |> Enum.map(&(create_type_field(&1, schema_def, schema_dict))) |
| 43 | + end |
| 44 | + |
| 45 | + @spec create_type_field(TypePath.t, SchemaDefinition.t, |
| 46 | + Types.schemaDictionary) :: String.t |
| 47 | + defp create_type_field(type_path, schema_def, schema_dict) do |
| 48 | + type_path |
| 49 | + |> Printer.resolve_type!(schema_def, schema_dict) |
| 50 | + |> create_type_name(schema_def) |
| 51 | + end |
| 52 | + |
| 53 | + @impl JS2E.Printers.PrinterBehaviour |
| 54 | + @spec print_decoder(Types.typeDefinition, SchemaDefinition.t, |
| 55 | + Types.schemaDictionary) :: String.t |
| 56 | + def print_decoder(%TupleType{name: name, |
| 57 | + path: _path, |
| 58 | + items: type_paths}, |
| 59 | + schema_def, schema_dict) do |
| 60 | + |
| 61 | + decoder_name = "#{name}Decoder" |
| 62 | + type_name = upcase_first name |
| 63 | + clauses = create_decoder_clauses(type_paths, schema_def, schema_dict) |
| 64 | + |
| 65 | + decoder_template(decoder_name, type_name, clauses) |
| 66 | + end |
| 67 | + |
| 68 | + @spec create_decoder_clauses( |
| 69 | + [TypePath.t], |
| 70 | + SchemaDefinition.t, |
| 71 | + Types.schemaDictionary |
| 72 | + ) :: [map] |
| 73 | + defp create_decoder_clauses(type_paths, schema_def, schema_dict) do |
| 74 | + |
| 75 | + type_paths |
| 76 | + |> Enum.map(fn type_path -> |
| 77 | + create_decoder_clause(type_path, schema_def, schema_dict) |
| 78 | + end) |
| 79 | + end |
| 80 | + |
| 81 | + @spec create_decoder_clause( |
| 82 | + TypePath.t, |
| 83 | + SchemaDefinition.t, |
| 84 | + Types.schemaDictionary |
| 85 | + ) :: map |
| 86 | + defp create_decoder_clause(type_path, schema_def, schema_dict) do |
| 87 | + |
| 88 | + {property_type, resolved_schema_def} = |
| 89 | + type_path |
| 90 | + |> Printer.resolve_type!(schema_def, schema_dict) |
| 91 | + |
| 92 | + decoder_name = create_decoder_name( |
| 93 | + {property_type, resolved_schema_def}, schema_def) |
| 94 | + |
| 95 | + cond do |
| 96 | + union_type?(property_type) || one_of_type?(property_type) -> |
| 97 | + create_decoder_union_clause(decoder_name) |
| 98 | + |
| 99 | + enum_type?(property_type) -> |
| 100 | + property_type_decoder = |
| 101 | + property_type.type |
| 102 | + |> determine_primitive_type_decoder!() |
| 103 | + |
| 104 | + create_decoder_enum_clause(property_type_decoder, decoder_name) |
| 105 | + |
| 106 | + true -> |
| 107 | + create_decoder_normal_clause(decoder_name) |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + defp create_decoder_union_clause(decoder_name) do |
| 112 | + %{decoder_name: decoder_name} |
| 113 | + end |
| 114 | + |
| 115 | + defp create_decoder_enum_clause(property_type_decoder, decoder_name) do |
| 116 | + %{property_decoder: property_type_decoder, |
| 117 | + decoder_name: decoder_name} |
| 118 | + end |
| 119 | + |
| 120 | + defp create_decoder_normal_clause(decoder_name) do |
| 121 | + %{decoder_name: decoder_name} |
| 122 | + end |
| 123 | + |
| 124 | + @impl JS2E.Printers.PrinterBehaviour |
| 125 | + @spec print_encoder(Types.typeDefinition, SchemaDefinition.t, |
| 126 | + Types.schemaDictionary) :: String.t |
| 127 | + def print_encoder(%TupleType{name: name, |
| 128 | + path: _path, |
| 129 | + items: type_paths}, |
| 130 | + schema_def, schema_dict) do |
| 131 | + |
| 132 | + type_name = upcase_first name |
| 133 | + encoder_name = "encode#{type_name}" |
| 134 | + |
| 135 | + properties = create_encoder_properties(type_paths, schema_def, schema_dict) |
| 136 | + |
| 137 | + template = encoder_template(encoder_name, type_name, properties) |
| 138 | + trim_newlines(template) |
| 139 | + end |
| 140 | + |
| 141 | + defp create_encoder_properties(type_paths, schema_def, schema_dict) do |
| 142 | + |
| 143 | + type_paths |
| 144 | + |> Enum.map(fn type_path -> |
| 145 | + Printer.resolve_type!(type_path, schema_def, schema_dict) |
| 146 | + end) |
| 147 | + |> Enum.reduce([], fn ({resolved_property, resolved_schema}, properties) -> |
| 148 | + encoder_name = create_encoder_name( |
| 149 | + {resolved_property, resolved_schema}, schema_def) |
| 150 | + updated_property = Map.put(resolved_property, :encoder_name, encoder_name) |
| 151 | + properties ++ [updated_property] |
| 152 | + end) |
| 153 | + end |
| 154 | + |
| 155 | +end |
0 commit comments