type method

ConfigType type(
  1. String key
)

Identifies the type of a key's value.

The special value of ConfigType.unavailable is returned if the key does not exist in the map.

The special value of ConfigType.unknownList is returned if the value is an empty list. The type of the list cannot be automatically determined, since it has no members to examine.

A ConfigExceptionKey is thrown if the value is invalid. One situation where this can occur is if a list contains more than one type of value.

This is usually used with the keys method.

Implementation

ConfigType type(String key) {
  if (_yamlMap.containsKey(key)) {
    final yamlValue = _yamlMap[key];

    if (yamlValue is bool) {
      return ConfigType.boolean;
    } else if (yamlValue is String) {
      return ConfigType.string;
    } else if (yamlValue is int) {
      return ConfigType.integer;
    } else if (yamlValue is YamlMap) {
      return ConfigType.map;
    } else if (yamlValue is YamlList) {
      // List

      if (yamlValue.isEmpty) {
        return ConfigType.unknownList;
      } else {
        final firstValue = yamlValue.first;

        if (firstValue is bool) {
          for (final v in yamlValue) {
            if (v is! bool) {
              throw _keyException('mixed types in list', key);
            }
          }
          return ConfigType.booleans;
        } else if (firstValue is int) {
          for (final v in yamlValue) {
            if (v is! int) {
              throw _keyException('mixed types in list', key);
            }
          }
          return ConfigType.integers;
        } else if (firstValue is String) {
          for (final v in yamlValue) {
            if (v is! String) {
              throw _keyException('mixed types in list', key);
            }
          }
          return ConfigType.strings;
        } else if (firstValue is YamlMap) {
          for (final v in yamlValue) {
            if (v is! YamlMap) {
              throw _keyException('mixed types in list', key);
            }
          }
          return ConfigType.maps;
        } else {
          throw _keyException('unexpected type of list', key);
        }
      }
    } else {
      throw _keyException('unexpected type of value', key);
    }
  } else {
    return ConfigType.unavailable;
  }
}