wrap method

$Value wrap(
  1. dynamic value, {
  2. bool recursive = false,
})

Attempt to wrap a Dart value into a $Value, and throw if unsuccessful.

Implementation

$Value wrap(dynamic value, {bool recursive = false}) {
  if (value is $Value) {
    return value;
  }
  if (value is List) {
    return recursive
        ? $List.wrap(value.map((v) => wrap(v, recursive: true)).toList())
        : $List.wrap(value);
  } else if (value is Map) {
    return recursive
        ? $Map.wrap(value.map((key, value) => MapEntry(
            wrap(key, recursive: true), wrap(value, recursive: true))))
        : $Map.wrap(value);
  }
  for (final wrapper in _typeAutowrappers) {
    final wrapped = wrapper(value);
    if (wrapped != null) {
      return wrapped;
    }
  }
  return wrapPrimitive(value) ??
      (throw Exception('Cannot wrap $value (${value.runtimeType}).'
          'If the type is known explicitly, use \${TypeName}.wrap(value); '
          'otherwise, try adding a type autowrapper with '
          'runtime.addTypeAutowrapper().'));
}