/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-07 14:54:44
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-04-28 15:26:05
 */
#ifndef _APOLLOSERVERRESULTMSG_HPP_
#define _APOLLOSERVERRESULTMSG_HPP_

#include "K8sServerBase.hpp"

using namespace std;

template<typename T>
class ApolloServerResultMsg: public MsgBase
{
public:
    using Ptr = std::shared_ptr<ApolloServerResultMsg<T>>;

    std::string appId;
    std::string cluster;
    std::string namespaceName;
    T configurations;

public:
    ApolloServerResultMsg(){}
    ~ApolloServerResultMsg(){}

    /**
     * @description: objectToJson
     * @param {string&} str
     * @return {*}
     */    
    void ObjectToJson(std::string& str) override{}

    /**
     * @description: jsonToObject
     * @param {string&} json
     * @return {*}
     */    
    bool jsonToObject(const std::string& json) override{
        rapidjson::Document doc;
        if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
            return false;
        }
        // get members
        const auto end = doc.MemberEnd();
        if (end == doc.FindMember("appId") || !doc["appId"].IsString()) {
            return false;
        } else {
            appId = doc["appId"].GetString();
        }

        if (end == doc.FindMember("cluster") || !doc["cluster"].IsString()) {
            return false;
        } else {
            cluster = doc["cluster"].GetString();
        }
        
        if (end == doc.FindMember("namespaceName") || !doc["namespaceName"].IsString()) {
            return false;
        } else {
            namespaceName = doc["namespaceName"].GetString();
        }
        if (end == doc.FindMember("configurations") || !doc["configurations"].IsObject()) {
            return false;
        } else {
            const rapidjson::Value& object = doc["configurations"];
            if(!(configurations.jsonToObject(object))){
                return false;
            }
        }
        return true;
    }

    /**
     * @description: jsonToObject
     * @param {Value&} object
     * @return {*}
     */    
    bool jsonToObject(const rapidjson::Value& object) override{
        
        return true;
    }

    /**
     * @description: 对象转sql
     * @param {string&} json
     * @return {*}
     */    
    bool ObjectToSql(std::string& json) override{
        configurations.USERID = this->USERID;
        configurations.ID = this->ID;
        return configurations.ObjectToSql(json);
    }
};


#endif