TypeSafeAPI.SystemOne.Prepared (TypeSafe AI v0.1.0-alpha.3)

Copy Markdown View Source

A question set that has been validated, encoded, and had its caller keys recorded, ready to send with any number of states.

Doing this work once is what makes TypeSafeAPI.evaluate_many/4 cheap: the validation, the wire encoding and the TypeSafeAPI.Keys registry are identical for every state, so they are built once and shared across tasks. encoded holds JSON bytes, not a term to be serialized again, so a request body is a splice of the cached iodata rather than another walk of the question set. It is a struct rather than a bare map so the functions that take one can match on it and fail loudly when handed something else.

Do not modify one by hand

count, encoded and keys are all derived from questions when the struct is built, and nothing revalidates them afterwards. %{prepared | questions: other_questions} compiles and still matches every %Prepared{} guard, but it sends the old questions on the wire and decodes the answers under a stale key registry. Build a new one with TypeSafeAPI.prepare/1 instead.

Summary

Functions

Builds a prepared question set from an already normalized [{id, question}] list, deriving the count, the wire encoding and the key registry from it.

Types

t()

@type t() :: %TypeSafeAPI.SystemOne.Prepared{
  count: pos_integer(),
  encoded: TypeSafeAPI.JSON.Encoded.t(),
  keys: TypeSafeAPI.Keys.t(),
  questions: [{TypeSafeAPI.Keys.key(), TypeSafeAPI.Question.t()}]
}

Functions

new(questions)

@spec new([{TypeSafeAPI.Keys.key(), TypeSafeAPI.Question.t()}]) :: t()

Builds a prepared question set from an already normalized [{id, question}] list, deriving the count, the wire encoding and the key registry from it.

TypeSafeAPI.prepare/1 is the public entry point; it normalizes and validates first.