getDefinition method

Definition getDefinition(
  1. String scriptString
)

Get a serialized Definition for a script string if it exists. This function will throw errors if the script is not defined or if the script is not valid.

Implementation

Definition getDefinition(String scriptString) {
  if (!serializedDefinitions.containsKey(scriptString)) {
    final scriptFound = lookup(scriptString);

    /// for when script is not defined at all
    if (scriptFound == null) {
      throw DerryError(
        type: ErrorCode.scriptNotDefined,
        body: {
          'script': scriptString,
          'suggestions': getPaths(),
        },
      );
    }

    // for when script is not a type we want
    if (scriptFound is! Map && scriptFound is! List && scriptFound is! String) {
      throw DerryError(
        type: ErrorCode.invalidScript,
        body: {'script': scriptString},
      );
    }

    // for when script is a map
    if (scriptFound is Map) {
      final scripts = scriptFound[scriptsDefinitionKey];
      final validity = scripts != null && (scripts is List || scripts is String);

      if (!validity) {
        throw DerryError(
          type: ErrorCode.invalidScript,
          body: {'script': scriptString, 'paths': getPaths()},
        );
      }
    }

    serializedDefinitions[scriptString] = Definition.from(scriptFound);
  }

  return serializedDefinitions[scriptString]!;
}