Lather.Types.Mapper (lather v1.0.42)

View Source

Dynamic type mapping system for SOAP services.

This module provides utilities to convert between WSDL XML Schema types and Elixir data structures dynamically, allowing the library to work with any SOAP service's data types without hardcoded implementations.

Summary

Functions

Creates a type mapping context from WSDL analysis.

Converts Elixir data structures to XML data based on type context.

Gets the definition of a specific type.

Infers the type of XML data based on the type context.

Validates data against a type definition.

Converts XML data to Elixir data structures based on type context.

Functions

create_context(service_info, options \\ [])

@spec create_context(
  map(),
  keyword()
) :: map()

Creates a type mapping context from WSDL analysis.

Parameters

  • service_info - Service information from WSDL analysis
  • options - Mapping configuration options

Options

  • :generate_structs - Whether to generate Elixir structs (default: false)
  • :struct_module - Module namespace for generated structs
  • :type_prefix - Prefix for generated type names
  • :namespace_mapping - Custom namespace to module mapping

Examples

type_context = Lather.Types.Mapper.create_context(service_info)

type_context = Lather.Types.Mapper.create_context(
  service_info,
  generate_structs: true,
  struct_module: MyApp.SoapTypes
)

elixir_to_xml(elixir_data, type_name, type_context)

@spec elixir_to_xml(any(), String.t(), map()) :: {:ok, map()} | {:error, term()}

Converts Elixir data structures to XML data based on type context.

Parameters

  • elixir_data - Elixir data structure
  • type_name - The target type name for XML conversion
  • type_context - Type mapping context from create_context/2

Examples

elixir_data = %{name: "John", age: 30, active: true}
{:ok, xml_data} = Lather.Types.Mapper.elixir_to_xml(
  elixir_data,
  "User",
  type_context
)
# %{"name" => "John", "age" => "30", "active" => "true"}

get_type_definition(type_name, type_context)

@spec get_type_definition(String.t(), map()) ::
  {:ok, map()} | {:error, :type_not_found}

Gets the definition of a specific type.

Examples

{:ok, type_def} = Lather.Types.Mapper.get_type_definition("User", type_context)

infer_type(xml_data, type_context)

@spec infer_type(map(), map()) :: {:ok, String.t()} | {:error, :type_not_found}

Infers the type of XML data based on the type context.

Parameters

  • xml_data - Parsed XML data
  • type_context - Type mapping context

Examples

{:ok, type_name} = Lather.Types.Mapper.infer_type(xml_data, type_context)
# "User"

validate_type(data, type_name, type_context)

@spec validate_type(any(), String.t(), map()) :: :ok | {:error, term()}

Validates data against a type definition.

Parameters

  • data - Data to validate
  • type_name - Type name to validate against
  • type_context - Type mapping context

Examples

:ok = Lather.Types.Mapper.validate_type(data, "User", type_context)
{:error, {:missing_required_field, "name"}} =
  Lather.Types.Mapper.validate_type(%{}, "User", type_context)

xml_to_elixir(xml_data, type_name, type_context)

@spec xml_to_elixir(map(), String.t(), map()) :: {:ok, any()} | {:error, term()}

Converts XML data to Elixir data structures based on type context.

Parameters

  • xml_data - Parsed XML data (maps with string keys)
  • type_name - The expected type name for the data
  • type_context - Type mapping context from create_context/2

Examples

xml_data = %{"name" => "John", "age" => "30", "active" => "true"}
{:ok, elixir_data} = Lather.Types.Mapper.xml_to_elixir(
  xml_data,
  "User",
  type_context
)
# %{name: "John", age: 30, active: true}