AshTypescript.Rpc.ResultProcessor (ash_typescript v0.17.3)

Copy Markdown View Source

Extracts requested fields from RPC results using type-driven dispatch.

This module uses the same pattern as ValueFormatter and FieldSelector: type-driven recursive dispatch where each type is self-describing.

Architecture

The core insight is that both ValueFormatter and ResultProcessor need to understand type structure:

  • ValueFormatter: Formats field names (internal ↔ client)
  • ResultProcessor: Extracts requested fields (filtering)

They share the need for type-driven recursive dispatch but have different concerns.

Type-Driven Extraction

extract_value/4 (unified type-driven dispatch)
   
   > extract_resource_value/3    (Ash Resources)
   > extract_typed_struct_value/3 (TypedStruct/NewType)
   > extract_typed_map_value/3   (Map/Struct with fields)
   > extract_union_value/3       (Ash.Type.Union)
   > extract_array_value/4       (Arrays - recurse)
   > normalize_primitive/1       (Primitives)

Summary

Functions

Determines the type and constraints for a given data value.

Extracts and normalizes a value based on its type and template.

Gets the type and constraints for a field, checking all field sources.

Normalizes a value for JSON serialization.

Alias for normalize_primitive/1 for backwards compatibility. Normalizes a value for JSON serialization.

Main entry point for processing Ash results.

Functions

determine_data_type(data, resource)

Determines the type and constraints for a given data value.

This function infers type information from:

  1. The struct type of the data itself (if it's a struct)
  2. The provided resource context
  3. Falls back to nil for unknown types

extract_value(value, type, constraints, template)

@spec extract_value(term(), atom() | tuple() | nil, keyword(), list()) :: term()

Extracts and normalizes a value based on its type and template.

This is the core recursive function that dispatches to type-specific handlers based on the type's characteristics. Mirrors the pattern used in ValueFormatter.format/5.

Parameters

  • value - The value to extract from
  • type - The Ash type (or nil for unknown)
  • constraints - Type constraints
  • template - The extraction template (list of field specs)

Returns

The extracted and normalized value.

get_field_type_info(resource, field_name)

@spec get_field_type_info(module() | nil, atom()) ::
  {atom() | tuple() | nil, keyword()}

Gets the type and constraints for a field, checking all field sources.

This consolidates all the previous resource lookup functions into one.

Parameters

  • resource - The Ash resource module, TypedStruct module, or nil
  • field_name - The field name (atom)

Returns

{type, constraints} or {nil, []} if not found.

normalize_primitive(value)

Normalizes a value for JSON serialization.

Handles DateTime, Date, Time, Decimal, CiString, atoms, keyword lists, nested maps, regular lists, and Ash.Union types. Recursively normalizes nested structures.

normalize_value_for_json(value)

Alias for normalize_primitive/1 for backwards compatibility. Normalizes a value for JSON serialization.

process(result, extraction_template, resource \\ nil)

@spec process(term(), map(), module() | nil) :: term()

Main entry point for processing Ash results.