Options
All
  • Public
  • Public/Protected
  • All
Menu

Represent any number of span-compatible layouts.

Factory: {@link module:Layout.union|union}

If the union has a {@link Union#defaultLayout|default layout} that layout must have a non-negative {@link Layout#span|span}. The span of a fixed-span union includes its {@link Union#discriminator|discriminator} if the variant is a {@link Union#usesPrefixDiscriminator|prefix of the union}, plus the span of its {@link Union#defaultLayout|default layout}.

If the union does not have a default layout then the encoded span of the union depends on the encoded span of its variant (which may be fixed or variable).

{@link VariantLayout#layout|Variant layout}s are added through {@link Union#addVariant|addVariant}. If the union has a default layout, the span of the {@link VariantLayout#layout|layout contained by the variant} must not exceed the span of the {@link Union#defaultLayout|default layout} (minus the span of a {@link Union#usesPrefixDiscriminator|prefix disriminator}, if used). The span of the variant will equal the span of the union itself.

The variant for a buffer can only be identified from the {@link Union#discriminator|discriminator} {@link UnionDiscriminator#property|property} (in the case of the {@link Union#defaultLayout|default layout}), or by using {@link Union#getVariant|getVariant} and examining the resulting VariantLayout instance.

A variant compatible with a JavaScript object can be identified using {@link Union#getSourceVariant|getSourceVariant}.

param discr

How to identify the layout used to interpret the union contents. The parameter must be an instance of UnionDiscriminator, an ExternalLayout that satisfies {@link ExternalLayout#isCount|isCount()}, or UInt (or UIntBE). When a non-external layout element is passed the layout appears at the start of the union. In all cases the (synthesized) UnionDiscriminator instance is recorded as {@link Union#discriminator|discriminator}.

param defaultLayout

initializer for {@link Union#defaultLayout|defaultLayout}. If absent defaults to null. If null there is no default layout: the union has data-dependent length and attempts to decode or encode unrecognized variants will throw an exception. A Layout instance must have a non-negative {@link Layout#span|span}, and if it lacks a {@link Layout#property|property} the {@link Union#defaultLayout|defaultLayout} will be a {@link Layout#replicate|replica} with property content.

param [property]

initializer for {@link Layout#property|property}.

Hierarchy

Index

Constructors

constructor

Properties

Optional boundConstructor_

boundConstructor_?: any

configGetSourceVariant

configGetSourceVariant: (getSourceVariant: (src: LayoutObject) => undefined | VariantLayout) => void

Type declaration

defaultLayout

defaultLayout: null | Layout<LayoutObject>

discriminator

discriminator: UnionDiscriminator<any>

getSourceVariant

getSourceVariant: (src: LayoutObject) => undefined | VariantLayout

Type declaration

property

property: string

registry

registry: {}

Type declaration

span

span: number

usesPrefixDiscriminator

usesPrefixDiscriminator: boolean

Methods

addVariant

  • Register a new variant structure within a union. The newly created variant is returned.

    Parameters

    • variant: number

      initializer for {@link VariantLayout#variant|variant}.

    • layout: Layout<LayoutObject>

      initializer for {@link VariantLayout#layout|layout}.

    • property: string

      initializer for {@link Layout#property|property}.

    Returns VariantLayout

decode

  • Implement {@link Layout#decode|decode} for Union.

    If the variant is {@link Union#addVariant|registered} the return value is an instance of that variant, with no explicit discriminator. Otherwise the {@link Union#defaultLayout|default layout} is used to decode the content.

    Parameters

    • b: Uint8Array
    • offset: number = 0

    Returns LayoutObject

defaultGetSourceVariant

  • Method to infer a registered Union variant compatible with src.

    The first satisfied rule in the following sequence defines the return value:

    • If src has properties matching the Union discriminator and the default layout, undefined is returned regardless of the value of the discriminator property (this ensures the default layout will be used);
    • If src has a property matching the Union discriminator, the value of the discriminator identifies a registered variant, and either (a) the variant has no layout, or (b) src has the variant's property, then the variant is returned (because the source satisfies the constraints of the variant it identifies);
    • If src does not have a property matching the Union discriminator, but does have a property matching a registered variant, then the variant is returned (because the source matches a variant without an explicit conflict);
    • An error is thrown (because we either can't identify a variant, or we were explicitly told the variant but can't satisfy it).
    throws

    {Error} - if src cannot be associated with a default or registered variant.

    Parameters

    • src: LayoutObject

      an object presumed to be compatible with the content of the Union.

    Returns undefined | VariantLayout

    • as described above.

encode

  • encode(src: LayoutObject, b: Uint8Array, offset?: number): number
  • Implement {@link Layout#encode|encode} for Union.

    This API assumes the src object is consistent with the union's {@link Union#defaultLayout|default layout}. To encode variants use the appropriate variant-specific {@link VariantLayout#encode} method.

    Parameters

    Returns number

fromArray

  • Create an object from layout properties and an array of values.

    NOTE This function returns undefined if invoked on a layout that does not return its value as an Object. Objects are returned for things that are a Structure, which includes variant layouts if they are structures, and excludes Unions. If you want this feature for a union you must use getVariant to select the desired layout.

    Parameters

    • values: any[]

      an array of values that correspond to the default order for properties. As with {@link Layout#decode|decode} layout elements that have no property name are skipped when iterating over the array values. Only the top-level properties are assigned; arguments are not assigned to properties of contained layouts. Any unused values are ignored.

    Returns undefined | LayoutObject

getSpan

  • getSpan(b: Uint8Array, offset?: number): number

getVariant

  • getVariant(vb: number | Uint8Array, offset?: number): undefined | VariantLayout
  • Get the layout associated with a registered variant.

    If vb does not produce a registered variant the function returns undefined.

    Parameters

    • vb: number | Uint8Array

      either the variant number, or a buffer from which the discriminator is to be read.

    • offset: number = 0

      offset into vb for the start of the union. Used only when vb is an instance of {Uint8Array}.

    Returns undefined | VariantLayout

    |undefined)}

makeDestinationObject

replicate

  • replicate(property: string): Union
  • Replicate the layout using a new property.

    This function must be used to get a structurally-equivalent layout with a different name since all Layout instances are immutable.

    NOTE This is a shallow copy. All fields except {@link Layout#property|property} are strictly equal to the origin layout.

    Parameters

    • property: string

      the value for {@link Layout#property|property} in the replica.

    Returns Union

    • the copy with {@link Layout#property|property} set to property.

Generated using TypeDoc