forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ONCatEntity.cpp
116 lines (88 loc) · 4 KB
/
ONCatEntity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2012 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidCatalog/ONCatEntity.h"
#include "MantidCatalog/Exception.h"
#include "MantidKernel/StringTokenizer.h"
#include <sstream>
#include <utility>
namespace Mantid::Catalog::ONCat {
using Mantid::Catalog::Exception::MalformedRepresentationError;
using Mantid::Kernel::StringTokenizer;
ONCatEntity::ONCatEntity(std::string id, std::string type, Content_uptr content)
: m_id(std::move(id)), m_type(std::move(type)), m_content(std::move(content)) {}
ONCatEntity::ONCatEntity(const ONCatEntity &other)
: m_id(other.m_id), m_type(other.m_type), m_content(std::make_unique<Content>(*other.m_content)) {}
ONCatEntity::~ONCatEntity() = default;
std::string ONCatEntity::id() const { return m_id; }
std::string ONCatEntity::type() const { return m_type; }
std::string ONCatEntity::toString() const { return m_content->toStyledString(); }
ONCatEntity ONCatEntity::fromJSONStream(std::istream &streamContent) {
auto content = std::make_unique<Content>();
try {
streamContent >> *content;
} catch (Json::Exception &je) {
throw MalformedRepresentationError(je.what());
}
const auto id = content->get("id", "").asString();
const auto type = content->get("type", "").asString();
if (id == "" || type == "") {
throw MalformedRepresentationError("Expected \"id\" and \"type\" attributes from ONCat API, but these "
"were not found.");
}
return ONCatEntity(id, type, std::move(content));
}
std::vector<ONCatEntity> ONCatEntity::vectorFromJSONStream(std::istream &streamContent) {
auto content = std::make_unique<Content>();
try {
streamContent >> *content;
} catch (Json::Exception &je) {
throw MalformedRepresentationError(je.what());
}
if (!content->isArray()) {
throw MalformedRepresentationError("Expected JSON representation to be an array of entities.");
}
std::vector<ONCatEntity> entities;
for (const auto &subContent : *content) {
const auto id = subContent.get("id", "").asString();
const auto type = subContent.get("type", "").asString();
if (id == "" || type == "") {
throw MalformedRepresentationError("Expected \"id\" and \"type\" attributes from ONCat API, but these "
"were not found.");
}
entities.emplace_back(ONCatEntity(id, type, std::make_unique<Content>(subContent)));
}
return entities;
}
template <>
std::string ONCatEntity::getNestedContentValueAsType(const Content &content, const std::string &path) const {
return getNestedContent(content, path).asString();
}
template <> int ONCatEntity::getNestedContentValueAsType(const Content &content, const std::string &path) const {
return getNestedContent(content, path).asInt();
}
template <> float ONCatEntity::getNestedContentValueAsType(const Content &content, const std::string &path) const {
return getNestedContent(content, path).asFloat();
}
template <> double ONCatEntity::getNestedContentValueAsType(const Content &content, const std::string &path) const {
return getNestedContent(content, path).asDouble();
}
template <> bool ONCatEntity::getNestedContentValueAsType(const Content &content, const std::string &path) const {
return getNestedContent(content, path).asBool();
}
Content ONCatEntity::getNestedContent(const Content &content, const std::string &path) const {
const auto pathTokens = StringTokenizer(path, ".", Mantid::Kernel::StringTokenizer::TOK_TRIM);
auto currentNode = content;
// Use the path tokens to drill down through the JSON nodes.
for (const auto &pathToken : pathTokens) {
if (!currentNode.isMember(pathToken)) {
throw ContentError("");
}
currentNode = currentNode[pathToken];
}
return currentNode;
}
} // namespace Mantid::Catalog::ONCat