Skip to content

Std.Shape

enum Shape

The shape of a type. The variants are named after the types they describe (Int, List), and are meant to be written with the module (Shape.Int): Bool is also a variant of Dynamic.

  • Bool

  • Int

  • Int64

  • Float

  • String

  • Bytes

  • Any

    Any value (Dynamic itself).

  • List(Shape)

  • Option(Shape)

  • Map(key: Shape, value: Shape)

  • Record(name: String, fields: List[Field])

    A type with fields, by the name of the type. The name of a type with type parameters carries its type arguments (generic_name).

  • Variants(name: String, variants: List[Variant])

    An enum, by the name of the enum, named as for Record.

  • Ref(String)

    A type that appears again inside itself, by its name (Node(left: Tree, ...)).

  • Named(name: String, shape: Shape)

    A type known by its name, with the shape of its contents: an opaque type whose constructor checks the contents (Quantity, an Int of at least 1).

  • Annotated(shape: Shape, notes: List[(String, Dynamic)])

    shape with notes on it. The notes of this module are written with description, minimum and the other functions below; a reader passes on the keys it does not know.

type Field(name: String, shape: Shape)

A field of a record or of named contents.

  • name: String
  • shape: Shape
type Variant(name: String, contents: Contents)

A variant of an enum, with the contents in the form it is declared in.

  • name: String
  • contents: Contents
enum Contents

The contents of a variant, in the form they are declared in: none, positional or named.

  • NoFields
  • PositionalFields(List[Shape])
  • NamedFields(List[Field])
trait Shaped

A type whose shape is known. proxy only carries the type: a trait function selects its impl from the types of its arguments, and there is no way to write the type argument at the call. Write p: Option[User] = None and call Shaped.shape(p), or use of_value and of_parameter.

An impl writes shape_in. visiting holds the names of the records and enums being read around this one, innermost first: a type already there is written as Ref instead of being read again, so types that refer to each other (Team with a Member, Member with a Team) end. visit does this for a named type; an impl for a type with type arguments passes visiting on to them.

  • fn shape_in(proxy: Option[Self], visiting: List[String]) -> Shape

    The shape of the type of proxy, with visiting the names of the records and enums being read around it.

  • fn shape(proxy: Option[Self]) -> Shape

    The shape of the type of proxy. Calls shape_in with an empty visiting.

fn visit(name: String, visiting: List[String], build: fn(List[String]) -> Shape fails E(build) uses { R(build) }) -> Shape fails E(build) uses { R(build) }

The shape of the record or enum name: Ref(name) if it is already being read in visiting, otherwise build with name added to visiting.

impl Team: Shaped {
fn shape_in(proxy, visiting) {
Shape.visit("Team", visiting, |visiting| {
lead: Option[Member] = None
Record(name: "Team", fields: [Field(name: "lead", shape: Shaped.shape_in(lead, visiting))])
})
}
}
fn of_value[A: Shaped](value: A) -> Shape

The shape of the type of value.

fn of_parameter[A: Shaped, B, E, R](f: fn(A) -> B fails E uses { R }) -> Shape

The shape of the parameter of f, without calling it. A router reads the type of the input of a handler this way.

fn annotate(shape: Shape, name: String, value: Dynamic) -> Shape

shape with the note name added after its notes. A note of the same name already there is replaced.

fn description(shape: Shape, text: String) -> Shape

A description for people.

fn pattern(shape: Shape, regex: String) -> Shape

A regular expression that a string matches. Only described.

fn format(shape: Shape, name: String) -> Shape

The name of a known form of string ("date-time", "email", "uuid"). Only described, as for pattern.

fn minimum(shape: Shape, n: Int) -> Shape

The least number allowed. Only described, as are the notes below: nothing in this module checks them.

fn maximum(shape: Shape, n: Int) -> Shape

The greatest number allowed.

fn min_length(shape: Shape, n: Int) -> Shape

The least length of a string, counted in code points.

fn max_length(shape: Shape, n: Int) -> Shape

The greatest length of a string, counted in code points.

fn min_items(shape: Shape, n: Int) -> Shape

The least number of items of a list.

fn max_items(shape: Shape, n: Int) -> Shape

The greatest number of items of a list.

fn strip(shape: Shape) -> Shape

shape without its notes and names: what its values are made of.

fn note(shape: Shape, name: String) -> Option[Dynamic]

The value of the note name on shape, looking through names, or None.

fn field_names(shape: Shape) -> List[String]

The names of the fields of a record, in declared order, looking through notes and names. Empty for any other shape.

fn name_of(shape: Shape) -> String

The name of shape as a type is written: Int, List[String], Page[Member]. Notes are not part of it.

fn generic_name(name: String, arguments: List[Shape]) -> String

The name of a type with type parameters, from its name and the shapes of its type arguments: generic_name("Page", [member]) is "Page[Member]". Without arguments, name as it is.