/// <summary> /// Transfers settings contained in this into a <see cref="CallOptions"/>. /// </summary> /// <param name="baseSettings">The base settings for the call. May be null.</param> /// <param name="callSettings">The settings for the specific call. May be null.</param> /// <param name="clock">The clock to use for deadline calculation.</param> /// <returns>A <see cref="CallOptions"/> configured from this <see cref="CallSettings"/>.</returns> internal static CallOptions ToCallOptions(this CallSettings baseSettings, CallSettings callSettings, IClock clock) { CallSettings effectiveSettings = baseSettings.MergedWith(callSettings); if (effectiveSettings == null) { return default(CallOptions); } var metadata = new Metadata(); effectiveSettings.HeaderMutation?.Invoke(metadata); return new CallOptions( headers: metadata, deadline: effectiveSettings.Timing.CalculateDeadline(clock), cancellationToken: effectiveSettings.CancellationToken ?? default(CancellationToken), writeOptions: effectiveSettings.WriteOptions, propagationToken: effectiveSettings.PropagationToken, credentials: effectiveSettings.Credentials); }
/// <summary> /// This method merges the settings in <paramref name="overlaid"/> with those in /// <paramref name="original"/>, with <paramref name="overlaid"/> taking priority. /// If both arguments are null, the result is null. If one argument is null, /// the other argument is returned. Otherwise, a new object is created with a property-wise /// overlay, where null values do not override non-null values. /// Any header mutations are combined, however: the mutation from the original is /// performed, then the mutation in the overlay. /// </summary> /// <param name="original">Original settings. May be null.</param> /// <param name="overlaid">Settings to overlay. May be null.</param> /// <returns>A merged set of call settings, or null if both parameters are null.</returns> public static CallSettings MergedWith(this CallSettings original, CallSettings overlaid) => CallSettings.Merge(original, overlaid);
/// <summary> /// Merges the settings in <paramref name="overlaid"/> with those in /// <paramref name="original"/>, with <paramref name="overlaid"/> taking priority. /// If both arguments are null, the result is null. If one argument is null, /// the other argument is returned. Otherwise, a new object is created with a property-wise /// overlay. Any header mutations are combined, however: the mutation from the original is /// performed, then the mutation in the overlay. /// </summary> /// <param name="original">Original settings. May be null.</param> /// <param name="overlaid">Settings to overlay. May be null.</param> /// <returns>A merged set of call settings.</returns> internal static CallSettings Merge(CallSettings original, CallSettings overlaid) { if (original == null) { return overlaid; } if (overlaid == null) { return original; } return new CallSettings( overlaid.CancellationToken ?? original.CancellationToken, overlaid.Credentials ?? original.Credentials, overlaid.Timing ?? original.Timing, // Combine mutations so that the overlaid mutation happens last; it can overwrite // anything that the previous mutation does. original.HeaderMutation + overlaid.HeaderMutation, overlaid.WriteOptions ?? original.WriteOptions, overlaid.PropagationToken ?? original.PropagationToken); }