rawGraphTo method

  1. @override
Object rawGraphTo(
  1. Object value,
  2. {String typeName,
  3. FullType specifiedType,
  4. MapperDecodeContext context}
)
override

Implementation

@override
Object rawGraphTo(
  Object value, {
  String typeName,
  FullType specifiedType,
  MapperDecodeContext context,
}) {
  if (value == null) {
    return null;
  } else if (value is Map<String, Object>) {
    //
    // This should be a GeneratedMessage
    //
    if (typeName == null && specifiedType == null) {
      //
      // We can't choose the right GeneratedMessage without type information
      //
      final nextMapper = this.nextMapper;
      if (nextMapper != null) {
        return nextMapper.rawGraphTo(
          value,
          typeName: typeName,
          specifiedType: specifiedType,
          context: context,
        );
      }
      throw ArgumentError(
        'Either `typeName` or `specifiedType` must be non-null.',
      );
    }

    //
    // Construct GeneratedMessage
    //
    final message = _newMessage(
      typeName: typeName,
      specifiedType: specifiedType,
    );
    if (message == null) {
      final nextMapper = this.nextMapper;
      if (nextMapper != null) {
        return nextMapper.rawGraphTo(
          value,
          typeName: typeName,
          specifiedType: specifiedType,
          context: context,
        );
      }
      throw ArgumentError(
        'Could not find factory for: $value',
      );
    }

    //
    // Set fields of the GeneratedMessage
    //
    final fieldInfosByName = message.info_.byName;
    for (var fieldName in fieldInfosByName.keys) {
      final fieldInfo = fieldInfosByName[fieldName];
      message.setField(
        fieldInfo.tagNumber,
        _dartFromPb(value, fieldInfo),
      );
    }

    // The GeneratedMessage is ready
    return message;
  } else {
    throw ArgumentError.value(
      value,
      'value',
      'Should be a Map<String,Object>',
    );
  }
}