From 137b62878c1a661e4fccd0a4813a8ac753f54aa1 Mon Sep 17 00:00:00 2001
From: Don Gagne <don@thegagnes.com>
Date: Fri, 12 Feb 2016 15:30:35 -0800
Subject: [PATCH] Use helper routines

---
 src/FirmwarePlugin/PX4/PX4ParameterMetaData.cc | 27 +++--------------------
 src/MissionManager/MissionCommandList.cc       | 30 ++++++++------------------
 src/MissionManager/MissionCommandList.h        |  1 -
 3 files changed, 12 insertions(+), 46 deletions(-)

diff --git a/src/FirmwarePlugin/PX4/PX4ParameterMetaData.cc b/src/FirmwarePlugin/PX4/PX4ParameterMetaData.cc
index d64380b..4fe2703 100644
--- a/src/FirmwarePlugin/PX4/PX4ParameterMetaData.cc
+++ b/src/FirmwarePlugin/PX4/PX4ParameterMetaData.cc
@@ -198,30 +198,9 @@ void PX4ParameterMetaData::_loadParameterFactMetaData(void)
                 qCDebug(PX4ParameterMetaDataLog) << "Found parameter name:" << name << " type:" << type << " default:" << strDefault;
 
                 // Convert type from string to FactMetaData::ValueType_t
-                
-                struct String2Type {
-                    const char*                 strType;
-                    FactMetaData::ValueType_t   type;
-                };
-                
-                static const struct String2Type rgString2Type[] = {
-                    { "FLOAT",  FactMetaData::valueTypeFloat },
-                    { "INT32",  FactMetaData::valueTypeInt32 },
-                };
-                static const size_t crgString2Type = sizeof(rgString2Type) / sizeof(rgString2Type[0]);
-                
-                bool found = false;
-                FactMetaData::ValueType_t foundType;
-                for (size_t i=0; i<crgString2Type; i++) {
-                    const struct String2Type* info = &rgString2Type[i];
-                    
-                    if (type == info->strType) {
-                        found = true;
-                        foundType = info->type;
-                        break;
-                    }
-                }
-                if (!found) {
+                bool unknownType;
+                FactMetaData::ValueType_t foundType = FactMetaData::stringToType(type, unknownType);
+                if (unknownType) {
                     qWarning() << "Parameter meta data with bad type:" << type << " name:" << name;
                     return;
                 }
diff --git a/src/MissionManager/MissionCommandList.cc b/src/MissionManager/MissionCommandList.cc
index 8a0a6dc..2adfabe 100644
--- a/src/MissionManager/MissionCommandList.cc
+++ b/src/MissionManager/MissionCommandList.cc
@@ -26,6 +26,7 @@ This file is part of the QGROUNDCONTROL project
 #include "FirmwarePluginManager.h"
 #include "QGCApplication.h"
 #include "QGroundControlQmlGlobal.h"
+#include "JsonHelper.h"
 
 #include <QStringList>
 #include <QJsonDocument>
@@ -62,20 +63,6 @@ MissionCommandList::MissionCommandList(const QString& jsonFilename, QObject* par
     _loadMavCmdInfoJson(jsonFilename);
 }
 
-bool MissionCommandList::_validateKeyTypes(QJsonObject& jsonObject, const QStringList& keys, const QList<QJsonValue::Type>& types)
-{
-    for (int i=0; i<keys.count(); i++) {
-        if (jsonObject.contains(keys[i])) {
-            if (jsonObject.value(keys[i]).type() != types[i]) {
-                qWarning() << "Incorrect type key:type:expected" << keys[i] << jsonObject.value(keys[i]).type() << types[i];
-                return false;
-            }
-        }
-    }
-
-    return true;
-}
-
 void MissionCommandList::_loadMavCmdInfoJson(const QString& jsonFilename)
 {
     if (jsonFilename.isEmpty()) {
@@ -122,13 +109,12 @@ void MissionCommandList::_loadMavCmdInfoJson(const QString& jsonFilename)
         QJsonObject jsonObject = info.toObject();
 
         // Make sure we have the required keys
+        QString errorString;
         QStringList requiredKeys;
         requiredKeys << _idJsonKey << _rawNameJsonKey;
-        foreach (const QString &key, requiredKeys) {
-            if (!jsonObject.contains(key)) {
-                qWarning() << "Mission required key" << key;
-                return;
-            }
+        if (!JsonHelper::validateRequiredKeys(jsonObject, requiredKeys, errorString)) {
+            qWarning() << errorString;
+            return;
         }
 
         // Validate key types
@@ -139,7 +125,8 @@ void MissionCommandList::_loadMavCmdInfoJson(const QString& jsonFilename)
              << _param1JsonKey << _param2JsonKey << _param3JsonKey << _param4JsonKey << _categoryJsonKey;
         types << QJsonValue::Double << QJsonValue::String << QJsonValue::String<< QJsonValue::String << QJsonValue::Bool << QJsonValue::Bool << QJsonValue::Bool
               << QJsonValue::Object << QJsonValue::Object << QJsonValue::Object << QJsonValue::Object << QJsonValue::String;
-        if (!_validateKeyTypes(jsonObject, keys, types)) {
+        if (!JsonHelper::validateKeyTypes(jsonObject, keys, types, errorString)) {
+            qWarning() << errorString;
             return;
         }
 
@@ -184,7 +171,8 @@ void MissionCommandList::_loadMavCmdInfoJson(const QString& jsonFilename)
                 QList<QJsonValue::Type> types;
                 keys << _defaultJsonKey << _decimalPlacesJsonKey << _enumStringsJsonKey << _enumValuesJsonKey << _labelJsonKey << _unitsJsonKey;
                 types << QJsonValue::Double <<  QJsonValue::Double << QJsonValue::String << QJsonValue::String << QJsonValue::String << QJsonValue::String;
-                if (!_validateKeyTypes(paramObject, keys, types)) {
+                if (!JsonHelper::validateKeyTypes(jsonObject, keys, types, errorString)) {
+                    qWarning() << errorString;
                     return;
                 }
 
diff --git a/src/MissionManager/MissionCommandList.h b/src/MissionManager/MissionCommandList.h
index 98c3ef7..55afd27 100644
--- a/src/MissionManager/MissionCommandList.h
+++ b/src/MissionManager/MissionCommandList.h
@@ -143,7 +143,6 @@ public:
     
 private:
     void _loadMavCmdInfoJson(const QString& jsonFilename);
-    bool _validateKeyTypes(QJsonObject& jsonObject, const QStringList& keys, const QList<QJsonValue::Type>& types);
 
 private:
     QMap<MAV_CMD, MavCmdInfo*> _mavCmdInfoMap;