StringBuilder codeForScalarFieldExtraction(Descriptors.FieldDescriptor desc, String fieldNameInCode, int indent) {
    StringBuilder code = new StringBuilder();
    if (desc.isRepeated()) {
      code.append(addIndent(String.format("if (msg.%s() > 0) {", getCountMethodName(fieldNameInCode)), indent));
      code.append(completeLine(
          putFieldInMsgMapCode(desc.getName(),
              getProtoFieldListMethodName(fieldNameInCode) + "().toArray", null, null),
          ++indent));
      code.append(addIndent("}", --indent));
    } else if (desc.hasPresence()) {
      code.append(addIndent(String.format("if (msg.%s()) {", hasPresenceMethodName(fieldNameInCode)), indent));
      code.append(completeLine(
          putFieldInMsgMapCode(desc.getName(), getProtoFieldMethodName(fieldNameInCode), null, null),
          ++indent));
      code.append(addIndent("}", --indent));
    } else {
      code.append(completeLine(
          putFieldInMsgMapCode(desc.getName(), getProtoFieldMethodName(fieldNameInCode), null, null), indent));
    }
    return code;
  }
@Test
    public void testCodeForScalarFieldExtraction() {
        MessageCodeGen messageCodeGen = new MessageCodeGen();
        // Simple field
            Descriptors.FieldDescriptor fd = ComplexTypes.TestMessage.getDescriptor().findFieldByName(STRING_FIELD);
        String fieldNameInCode = ProtobufInternalUtils.underScoreToCamelCase(fd.getName(), true);
        assertEquals(messageCodeGen.codeForScalarFieldExtraction(fd, fieldNameInCode, 1).toString(),
            "  msgMap.put(\"string_field\", msg.getStringField());\n");

        // Nullable field Or Has Presence
        fd = ComplexTypes.TestMessage.getDescriptor().findFieldByName(NULLABLE_STRING_FIELD);
        fieldNameInCode = ProtobufInternalUtils.underScoreToCamelCase(fd.getName(), true);
        assertEquals(messageCodeGen.codeForScalarFieldExtraction(fd, fieldNameInCode, 1).toString(),
            "  if (msg.hasNullableStringField()) {\n"
                + "    msgMap.put(\"nullable_string_field\", msg.getNullableStringField());\n" + "  }\n");

        // Repeated field
        fd = ComplexTypes.TestMessage.getDescriptor().findFieldByName(REPEATED_STRINGS);
        fieldNameInCode = ProtobufInternalUtils.underScoreToCamelCase(fd.getName(), true);
        assertEquals(messageCodeGen.codeForScalarFieldExtraction(fd, fieldNameInCode, 1).toString(),
            "  if (msg.getRepeatedStringsCount() > 0) {\n"
                + "    msgMap.put(\"repeated_strings\", msg.getRepeatedStringsList().toArray());\n" + "  }\n");
    }