/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-06 10:01:44
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-06-22 13:58:11
 */
#ifndef __K8SSERVERRESULTMSY_HPP_
#define __K8SSERVERRESULTMSY_HPP_

#include "K8sServerBase.hpp"


template <typename T>
class K8sServerResultMsy:public MsgBase
{
public:
    std::string kind;
    std::string apiVersion;
    Metadata metadata;
    vector<T> items;
    std::string status;
    std::string message;
    std::string code;
    std::string reason;

public:
    K8sServerResultMsy(){}
    ~K8sServerResultMsy(){}

    /**
     * @description: jsonToObject
     * @param {string&} json
     * @return {*}
     */    
    bool jsonToObject(const std::string& json){
        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("kind") || !doc["kind"].IsString()) {
            return false;
        } else {
            kind = doc["kind"].GetString();
        }
        
        if (end == doc.FindMember("apiVersion") || !doc["apiVersion"].IsString()) {
            return false;
        } else {
            apiVersion = doc["apiVersion"].GetString();
        }

        if (end == doc.FindMember("status") || !doc["status"].IsString()) {
            return false;
        } else {
            status = doc["status"].GetString();
        }

        if (end == doc.FindMember("message") || !doc["message"].IsString()) {
            return false;
        } else {
            message = doc["message"].GetString();
        }

        if (end == doc.FindMember("reason") || !doc["reason"].IsString()) {
            return false;
        } else {
            reason = doc["reason"].GetString();
        }

        if (end == doc.FindMember("code") || !doc["code"].IsInt()) {
            return false;
        } else {
            code = std::to_string(doc["code"].GetInt());
        }

        if (end != doc.FindMember("items")) {
            if(doc["items"].IsArray()){
                    const rapidjson::Value& objs = doc["items"];
                    for (size_t i = 0; i < objs.Size(); ++i) {
                        const rapidjson::Value& obj = objs[i];
                        T item;
                        if(!(item.jsonToObject(obj))){
                            return false;
                        }
                        items.push_back(item);
                    }
                }
        }
        return true;
    }
};



#endif
