| 347 | } |
| 348 | |
| 349 | FieldInfo getFieldType(Session *session, Service& fieldInfoService, const std::string& field) { |
| 350 | |
| 351 | Request request = fieldInfoService.createRequest("FieldInfoRequest"); |
| 352 | request.append("id", field.c_str()); |
| 353 | request.set("returnFieldDocumentation", false); |
| 354 | session->sendRequest(request); |
| 355 | |
| 356 | FieldInfo ans; |
| 357 | while (true) { |
| 358 | Event event = session->nextEvent(); |
| 359 | if (event.eventType() != Event::RESPONSE && |
| 360 | event.eventType() != Event::PARTIAL_RESPONSE) { |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | MessageIterator msgIter(event); |
| 365 | while (msgIter.next()) { |
| 366 | Message msg = msgIter.message(); |
| 367 | //msg.asElement().print(std::cout); |
| 368 | Element fields = msg.getElement("fieldData"); |
| 369 | if(fields.numValues() > 1) { |
| 370 | throw std::logic_error("getFieldType: too many fields returned."); |
| 371 | } |
| 372 | Element field = fields.getValueAsElement(0); |
| 373 | if (!field.hasElement("id")) { |
| 374 | throw std::logic_error("Did not find 'id' in repsonse."); |
| 375 | } |
| 376 | if (field.hasElement("fieldError")) { |
| 377 | std::ostringstream err; |
| 378 | err << "Bad field: " << field.getElementAsString("id") << std::endl; |
| 379 | throw std::logic_error(err.str()); |
| 380 | } |
| 381 | if (!field.hasElement("fieldInfo")) { |
| 382 | throw std::logic_error("Did not find fieldInfo in repsonse."); |
| 383 | } |
| 384 | Element fieldInfo = field.getElement("fieldInfo"); |
| 385 | if (!fieldInfo.hasElement("mnemonic") || |
| 386 | !fieldInfo.hasElement("datatype") || |
| 387 | !fieldInfo.hasElement("ftype")) { |
| 388 | throw std::logic_error( |
| 389 | "fieldInfo missing info mnemonic/datatype/ftype."); |
| 390 | } |
| 391 | ans.id = field.getElementAsString("id"); |
| 392 | ans.mnemonic = fieldInfo.getElementAsString("mnemonic"); |
| 393 | ans.datatype = fieldInfo.getElementAsString("datatype"); |
| 394 | ans.ftype = fieldInfo.getElementAsString("ftype"); |
| 395 | } |
| 396 | if (event.eventType() == Event::RESPONSE) { |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | return ans; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | std::vector<FieldInfo> getFieldTypes(Session *session, |