encode<V> static method

V encode<V>(
  1. Coupon instance,
  2. Encoder<V> encoder
)
override

Implementation

static V encode<V>(
  Coupon instance,
  Encoder<V> encoder,
) {
  final container = encoder.container<String>();
  if (instance.amountOff != null) {
    container.encodeInt(
      'amount_off',
      instance.amountOff!,
    );
  }
  if (instance.appliesTo != null) {
    CouponAppliesTo.encode(
      instance.appliesTo!,
      container.nestedSingleValueContainer('applies_to').encoder,
    );
  }
  container.encodeDateTime(
    'created',
    instance.created,
  );
  if (instance.currency != null) {
    container.encodeString(
      'currency',
      instance.currency!,
    );
  }
  if (instance.currencyOptions != null) {
    container.encodeMap(
      'currency_options',
      (container) => instance.currencyOptions!.forEach((
        key,
        value,
      ) =>
          CouponCurrencyOption.encode(
            value,
            container.nestedSingleValueContainer(key).encoder,
          )),
    );
  }
  container.encodeString(
    'duration',
    instance.duration,
  );
  if (instance.durationInMonths != null) {
    container.encodeInt(
      'duration_in_months',
      instance.durationInMonths!,
    );
  }
  container.encodeString(
    'id',
    instance.id,
  );
  container.encodeBool(
    'livemode',
    instance.livemode,
  );
  if (instance.maxRedemptions != null) {
    container.encodeInt(
      'max_redemptions',
      instance.maxRedemptions!,
    );
  }
  if (instance.metadata != null) {
    container.encodeMap(
      'metadata',
      (container) => instance.metadata!.forEach((
        key,
        value,
      ) =>
          container.encodeString(
            key,
            value,
          )),
    );
  }
  if (instance.name != null) {
    container.encodeString(
      'name',
      instance.name!,
    );
  }
  container.encodeString(
    'object',
    'coupon',
  );
  if (instance.percentOff != null) {
    container.encodeDouble(
      'percent_off',
      instance.percentOff!,
    );
  }
  if (instance.redeemBy != null) {
    container.encodeDateTime(
      'redeem_by',
      instance.redeemBy!,
    );
  }
  container.encodeInt(
    'times_redeemed',
    instance.timesRedeemed,
  );
  container.encodeBool(
    'valid',
    instance.valid,
  );
  return container.value;
}