Function ValueSet

  • @ValueSet decorator set the value of the decorated property based on a function passed as a parameter. This decorator don't read anything from the binary file and is just used to add more context to a class during the reading.

    Type Parameters

    • This extends object

      The type of the class the decorator is applied to.

    • Value

    Parameters

    • setter: ValueSetFunction<This, Value>

      Function that will store the return value in the decorated property.

    • Optionalopt: Partial<PrePostOptions>

      Optional configution.

    Returns DecoratorType<This, Value>

    The class or property decorator function.

    In the following example @ValueSet is used to fetch the protocol type name based on an id read in by the binary definition. The protocol_name will just appear when the object is serialized and will gave the object more context.

    const ID_TO_NAME = {
    1: "Record",
    ...
    }

    class Protocol {
    @Relation(PrimitiveSymbol.u8)
    protocol_id: number

    @ValueSet(_ => ID_TO_NAME[_.protocol_id] || 'UNKNOWN')
    protocol_name: string
    }

    Primitive.RelationAlreadyDefinedError if a relation metadata is found.