175930019STodd Fiala //===-- SBStructuredData.cpp ------------------------------------*- C++ -*-===//
275930019STodd Fiala //
375930019STodd Fiala //                     The LLVM Compiler Infrastructure
475930019STodd Fiala //
575930019STodd Fiala // This file is distributed under the University of Illinois Open Source
675930019STodd Fiala // License. See LICENSE.TXT for details.
775930019STodd Fiala //
875930019STodd Fiala //===----------------------------------------------------------------------===//
975930019STodd Fiala 
1075930019STodd Fiala #include "lldb/API/SBStructuredData.h"
1175930019STodd Fiala 
1275930019STodd Fiala #include "lldb/API/SBStream.h"
13*3815e702SJim Ingham #include "lldb/API/SBStringList.h"
1475930019STodd Fiala #include "lldb/Core/Event.h"
15d5d8d91cSRavitheja Addepally #include "lldb/Core/StructuredDataImpl.h"
1675930019STodd Fiala #include "lldb/Target/StructuredDataPlugin.h"
1797206d57SZachary Turner #include "lldb/Utility/Status.h"
18bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
19f2a8bccfSPavel Labath #include "lldb/Utility/StructuredData.h"
2075930019STodd Fiala 
2175930019STodd Fiala using namespace lldb;
2275930019STodd Fiala using namespace lldb_private;
2375930019STodd Fiala 
2475930019STodd Fiala #pragma mark--
2575930019STodd Fiala #pragma mark SBStructuredData
2675930019STodd Fiala 
272ef442c6STodd Fiala SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {}
2875930019STodd Fiala 
29b9c1b51eSKate Stone SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
302ef442c6STodd Fiala     : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {}
3175930019STodd Fiala 
32b9c1b51eSKate Stone SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
332ef442c6STodd Fiala     : m_impl_up(new StructuredDataImpl(event_sp)) {}
3475930019STodd Fiala 
35*3815e702SJim Ingham SBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl)
36*3815e702SJim Ingham     : m_impl_up(impl) {}
37*3815e702SJim Ingham 
38b9c1b51eSKate Stone SBStructuredData::~SBStructuredData() {}
3975930019STodd Fiala 
40b9c1b51eSKate Stone SBStructuredData &SBStructuredData::
41b9c1b51eSKate Stone operator=(const lldb::SBStructuredData &rhs) {
4275930019STodd Fiala   *m_impl_up = *rhs.m_impl_up;
4375930019STodd Fiala   return *this;
4475930019STodd Fiala }
4575930019STodd Fiala 
46d5d8d91cSRavitheja Addepally lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
47d5d8d91cSRavitheja Addepally   lldb::SBError error;
48d5d8d91cSRavitheja Addepally   std::string json_str(stream.GetData());
49d5d8d91cSRavitheja Addepally 
50d5d8d91cSRavitheja Addepally   StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str);
51d5d8d91cSRavitheja Addepally   m_impl_up->SetObjectSP(json_obj);
52d5d8d91cSRavitheja Addepally 
535bfee5f1SAbhishek Aggarwal   if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
54d5d8d91cSRavitheja Addepally     error.SetErrorString("Invalid Syntax");
55d5d8d91cSRavitheja Addepally   return error;
56d5d8d91cSRavitheja Addepally }
57d5d8d91cSRavitheja Addepally 
58b9c1b51eSKate Stone bool SBStructuredData::IsValid() const { return m_impl_up->IsValid(); }
5975930019STodd Fiala 
60b9c1b51eSKate Stone void SBStructuredData::Clear() { m_impl_up->Clear(); }
6175930019STodd Fiala 
62b9c1b51eSKate Stone SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
63d5d8d91cSRavitheja Addepally   SBError error;
64d5d8d91cSRavitheja Addepally   error.SetError(m_impl_up->GetAsJSON(stream.ref()));
65d5d8d91cSRavitheja Addepally   return error;
6675930019STodd Fiala }
6775930019STodd Fiala 
68b9c1b51eSKate Stone lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
6997206d57SZachary Turner   Status error = m_impl_up->GetDescription(stream.ref());
702ef442c6STodd Fiala   SBError sb_error;
712ef442c6STodd Fiala   sb_error.SetError(error);
722ef442c6STodd Fiala   return sb_error;
7375930019STodd Fiala }
745bfee5f1SAbhishek Aggarwal 
755bfee5f1SAbhishek Aggarwal StructuredDataType SBStructuredData::GetType() const {
765bfee5f1SAbhishek Aggarwal   return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid);
775bfee5f1SAbhishek Aggarwal }
785bfee5f1SAbhishek Aggarwal 
795bfee5f1SAbhishek Aggarwal size_t SBStructuredData::GetSize() const {
805bfee5f1SAbhishek Aggarwal   return (m_impl_up ? m_impl_up->GetSize() : 0);
815bfee5f1SAbhishek Aggarwal }
825bfee5f1SAbhishek Aggarwal 
83*3815e702SJim Ingham bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
84*3815e702SJim Ingham   if (!m_impl_up)
85*3815e702SJim Ingham     return false;
86*3815e702SJim Ingham 
87*3815e702SJim Ingham   if (GetType() != eStructuredDataTypeDictionary)
88*3815e702SJim Ingham     return false;
89*3815e702SJim Ingham 
90*3815e702SJim Ingham   StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
91*3815e702SJim Ingham   if (!obj_sp)
92*3815e702SJim Ingham     return false;
93*3815e702SJim Ingham 
94*3815e702SJim Ingham   StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
95*3815e702SJim Ingham   // We claimed we were a dictionary, so this can't be null.
96*3815e702SJim Ingham   assert(dict);
97*3815e702SJim Ingham   // The return kind of GetKeys is an Array:
98*3815e702SJim Ingham   StructuredData::ObjectSP array_sp = dict->GetKeys();
99*3815e702SJim Ingham   StructuredData::Array *key_arr = array_sp->GetAsArray();
100*3815e702SJim Ingham   assert(key_arr);
101*3815e702SJim Ingham 
102*3815e702SJim Ingham   key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool {
103*3815e702SJim Ingham     llvm::StringRef key = object->GetStringValue("");
104*3815e702SJim Ingham     keys.AppendString(key.str().c_str());
105*3815e702SJim Ingham     return true;
106*3815e702SJim Ingham   });
107*3815e702SJim Ingham   return true;
108*3815e702SJim Ingham }
109*3815e702SJim Ingham 
1105bfee5f1SAbhishek Aggarwal lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
1115bfee5f1SAbhishek Aggarwal   if (!m_impl_up)
1125bfee5f1SAbhishek Aggarwal     return SBStructuredData();
1135bfee5f1SAbhishek Aggarwal 
1145bfee5f1SAbhishek Aggarwal   SBStructuredData result;
1155bfee5f1SAbhishek Aggarwal   result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
1165bfee5f1SAbhishek Aggarwal   return result;
1175bfee5f1SAbhishek Aggarwal }
1185bfee5f1SAbhishek Aggarwal 
1195bfee5f1SAbhishek Aggarwal lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
1205bfee5f1SAbhishek Aggarwal   if (!m_impl_up)
1215bfee5f1SAbhishek Aggarwal     return SBStructuredData();
1225bfee5f1SAbhishek Aggarwal 
1235bfee5f1SAbhishek Aggarwal   SBStructuredData result;
1245bfee5f1SAbhishek Aggarwal   result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
1255bfee5f1SAbhishek Aggarwal   return result;
1265bfee5f1SAbhishek Aggarwal }
1275bfee5f1SAbhishek Aggarwal 
1285bfee5f1SAbhishek Aggarwal uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
1295bfee5f1SAbhishek Aggarwal   return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value);
1305bfee5f1SAbhishek Aggarwal }
1315bfee5f1SAbhishek Aggarwal 
1325bfee5f1SAbhishek Aggarwal double SBStructuredData::GetFloatValue(double fail_value) const {
1335bfee5f1SAbhishek Aggarwal   return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value);
1345bfee5f1SAbhishek Aggarwal }
1355bfee5f1SAbhishek Aggarwal 
1365bfee5f1SAbhishek Aggarwal bool SBStructuredData::GetBooleanValue(bool fail_value) const {
1375bfee5f1SAbhishek Aggarwal   return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value);
1385bfee5f1SAbhishek Aggarwal }
1395bfee5f1SAbhishek Aggarwal 
1405bfee5f1SAbhishek Aggarwal size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
1415bfee5f1SAbhishek Aggarwal   return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0);
1425bfee5f1SAbhishek Aggarwal }
143