ReferenceMode

Enum ReferenceMode 

Source
pub enum ReferenceMode {
    Inline,
    Reference {
        prefix: String,
    },
}
Expand description

Control whether nested DamlTyCon are referenced or inlined.

If Inline mode is set then the encoder will attempt to emit the target data type nested under the parent data type.

If Reference mode is set then the encoder will attempt to emit an absolute reference to a data type from a given prefix. In this mode it is assumed the the target data type will be emitted elsewhere and made available under the prefix.

It is not possible to emit every possible DamlTyCon in the requested mode and so there are some specific rules which apply based on the requested mode, whether the target data type is recursive and whether the target type expects type parameters.

  • Recursive: Indicates that the DamlTyCon resolves to a data type which (directly or indirectly) contains itself as a field or type argument.

  • Type Parameters: Indicates that the DamlTyCon resolves to a data type which has type one or more parameters that must be resolved before that data type can be emitted.

The following table enumerates how a DamlTyCon will be encoded for all possible cases:

ModeRecursive?Type params?Encoding
InlineNoNo1 - Encode target type inline
InlineNoYes2 - Encode target type inline (with resolved type arguments)
InlineYesNo3 - Encode to accept any object
InlineYesYes4 - Encode to accept any object
ReferenceNoNo5 - Encode as reference to target type
ReferenceNoYes6 - Encode target type inline (fallback to #2)
ReferenceYesNo7 - Encode as reference to target type
ReferenceYesYes8 - Encode as accept any object (no fallback possible)

Cases 1, 2, 5 & 7 are straightforward, whereas cases 3, 4, 6 & 8 are more complex:

  • Cases 3 & 4:

If Inline mode is chosen and the DamlTyCon resolves to a data type which is recursive then the emitter emits a JSON schema object which matches any JSON type:

For example, given:

data Rec = Rec with foo: Text, bar: Rec

The data type Rec includes itself recursively and so cannot be emitted Inline and will instead be emitted as follows:

{
   "description": "Any (Rec)",
   "comment": "inline recursive data types cannot be represented"
}
  • Case 6:

If Reference mode is chosen and the DamlTyCon resolves to a data type which expects type parameters we do not emit a reference as the fully resolved target data type is unknown. In this case the emitter will fallback to Inline mode.

For example, given:

data Bottom a = Bottom with bottom: a
data Middle = Middle with middle: Bottom Int

Attempting to emit the middle: Bottom Int field in Reference mode (with a prefix of #/components/schemas/) cannot emit the below reference as this does not account for the type parameter applied to Bottom, of which there are infinitely many:

{
  "$ref": "#/components/schemas/Bottom"
}

Instead, the schema for Bottom Int will be emitted Inline.

  • Case 8:

Case 8 is similar to case 6, however, the DamlTyCon resolves to a data type which is also recursive. In this case we cannot ‘fallback’ to Inline mode as recursive types cannot be inlined. The emitter therefore emits a JSON schema object which matches any JSON type.

For example, given:

data TopRec a = TopRec with top: TopRec a

The structure TopRec is both recursive and has a type parameter and therefore cannot be emitted as a $ref nor can it ‘fallback’ to Inline mode.

Variants§

§

Inline

Inline nested DamlTyCon.

§

Reference

Reference nested DamlTyCon by $ref from prefix.

Fields

§prefix: String

Trait Implementations§

Source§

impl Clone for ReferenceMode

Source§

fn clone(&self) -> ReferenceMode

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ReferenceMode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ReferenceMode

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,