guard method Null safety

bool guard(
  1. String ptr,
  2. List<LicenseUsecase> usecases,
  3. {String? origin,
  4. List<String>? destinations,
  5. dynamic onPass(
      )?,
    1. dynamic onFail(
      1. String
      )?}
    )

    Guard against an invalid LicenseRecord for a List of usecases and destinations.

    Use this method to verify a non-expired, LicenseRecord for the ptr exists, and permits the listed usecases and destinations.

    Parameters:

    ptr - The Pointer Record for the asset. Used to located the latest relevant LicenseRecord.

    origin - An optional override of the default origin specified in init.

    usecases - A List of usecases defining how the asset will be used.

    destinations - A List of destinations defining where the asset will be used. Often URLs

    onPass - A Function to execute automatically upon successfully resolving the LicenseRecord against the usecases and destinations

    onFail - A Fucntion to execute automatically upon failure to resolve the LicenseRecord. Accepts a String parameter, holding an error message describing the reason for failure.

    This method can be used in two forms, 1) as a traditional guard, returning a pass/fail boolean. Or 2) as a wrapper around function.

    For example: An http that you want to run IF permitted by a LicenseRecord.

    Option 1:

    bool pass = guard('ptr', [LicenseUsecase.attribution()]);
    if(pass) http.post(...);
    

    Option 2:

    guard('ptr', [LicenseUsecase.attribution()], onPass: () => http.post(...));
    

    Returns the created TitleRecord

    Implementation

    bool guard(String ptr, List<LicenseUsecase> usecases,
        {String? origin,
        List<String>? destinations,
        Function()? onPass,
        Function(String)? onFail}) {
      LicenseRecord? license = latest(ptr, origin: origin);
      if (license == null) {
        if (onFail != null) onFail('Missing license for ptr: $ptr');
        return false;
      }
      String? guardMessage = Guard.check(
          license, [LicenseUse(usecases, destinations: destinations)]);
      if (guardMessage == null) {
        if (onPass != null) onPass();
        return true;
      } else {
        if (onFail != null) onFail(guardMessage);
        return false;
      }
    }