Smithy Qt 0.1.0-pre
Internal development documentation
Loading...
Searching...
No Matches
Model Class Reference

The Model class provides a Qt representation of a Smithy semantic model. More...

Collaboration diagram for Model:
[legend]

Public Types

enum class  Error {
  NoError = 0 , NoData = 1 , InvalidMetadata = 2 , InvalidShapes = 3 ,
  InvalidShapeId = 4 , InvalidShape = 5 , ConflictingMetadata = 6
}
 

Public Member Functions

 Model ()
 Constructs a new, empty Smithy model.
 
 Model (Model &&other)
 
 Model (const Model &other)
 
Modeloperator= (const Model &model)
 
Modeloperator= (const Model &&model)
 
 ~Model ()
 Destroys this Model object.
 
void clear ()
 
bool insert (const QJsonObject &ast)
 Add the logical content of the JSON AST model file given by ast into this semantic model.
 
bool finish ()
 
Error error () const
 
bool isValid () const
 
QJsonObject metadata () const
 
Shape shape (const ShapeId &shapeId) const
 
QHash< ShapeId, Shapeshapes (const Shape::Type &type=Shape::Type::Undefined) const
 

Protected Attributes

ModelPrivated_ptr
 

Private Member Functions

 Q_DECLARE_TR_FUNCTIONS (Model)
 

Detailed Description

The Model class provides a Qt representation of a Smithy semantic model.

See also
https://awslabs.github.io/smithy/2.0/spec/model.html#semantic-model

Definition at line 25 of file model.h.

Member Enumeration Documentation

◆ Error

enum class Model::Error
strong

Definition at line 30 of file model.h.

30 {
31 NoError = 0,
32 NoData = 1,
33 InvalidMetadata = 2,
34 InvalidShapes = 3,
35 InvalidShapeId = 4,
36 InvalidShape = 5,
37 ConflictingMetadata = 6,
38 };

Constructor & Destructor Documentation

◆ Model() [1/3]

Model::Model ( )

Constructs a new, empty Smithy model.

Todo
Load the Smithy prelude here?

Definition at line 27 of file model.cpp.

27 : d_ptr(new ModelPrivate(this))
28{
29 Q_D(Model);
30 d->error = Error::NoData;
31 /// \todo Load the Smithy prelude here?
32}
Model()
Constructs a new, empty Smithy model.
Definition model.cpp:27
ModelPrivate * d_ptr
Definition model.h:60

References d_ptr, and Model().

Referenced by finish(), insert(), Model(), shape(), and shapes().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Model() [2/3]

Model::Model ( Model && other)

Definition at line 34 of file model.cpp.

34 : d_ptr(new ModelPrivate(this))
35{
36 Q_D(Model);
37 d->error = std::move(other.d_ptr->error);
38 d->mergedMetadata = std::move(other.d_ptr->mergedMetadata);
39 d->mergedShapes = std::move(other.d_ptr->mergedShapes);
40 d->allMetadata = std::move(other.d_ptr->allMetadata);
41 d->allShapes = std::move(other.d_ptr->allShapes);
42}

◆ Model() [3/3]

Model::Model ( const Model & other)

Definition at line 44 of file model.cpp.

44 : d_ptr(new ModelPrivate(this))
45{
46 Q_D(Model);
47 d->error = other.d_ptr->error;
48 d->mergedMetadata = other.d_ptr->mergedMetadata;
49 d->mergedShapes = other.d_ptr->mergedShapes;
50 d->allMetadata = other.d_ptr->allMetadata;
51 d->allShapes = other.d_ptr->allShapes;
52}

◆ ~Model()

Model::~Model ( )

Destroys this Model object.

Definition at line 79 of file model.cpp.

80{
81 delete d_ptr;
82}

References d_ptr.

Member Function Documentation

◆ clear()

void Model::clear ( )

Definition at line 84 of file model.cpp.

85{
86 Q_D(Model);
87 d->error = Error::NoData;
88 d->mergedMetadata = QJsonObject{};
89 d->mergedShapes.clear();
90 d->allMetadata.clear();
91 d->allShapes.clear();
92}

◆ error()

Model::Error Model::error ( ) const

Definition at line 205 of file model.cpp.

206{
207 Q_D(const Model);
208 return d->error;
209}

◆ finish()

bool Model::finish ( )
Todo
resolve shape conflicts.
Todo
Todo
resolve trait conflicts; include 'apply' statements.

Definition at line 186 of file model.cpp.

187{
188 Q_D(Model);
189 if (d->error != Error::NoError) {
190 qCWarning(d->lc).noquote() << tr("Model::finish() called with Model errors present");
191 return false;
192 }
193
194 d->mergedMetadata = ModelPrivate::mergeMetadata(d->allMetadata);
195 if (d->allMetadata.isEmpty() != d->mergedMetadata.isEmpty()) {
196 if (d->error == Error::NoError) d->error = Error::ConflictingMetadata;
197 }
198
199 /// \todo resolve shape conflicts.
200 /// \todo resolve trait conflicts; include 'apply' statements.
201 Q_UNIMPLEMENTED();
202 return isValid();
203}

References Model().

Here is the call graph for this function:

◆ insert()

bool Model::insert ( const QJsonObject & ast)

Add the logical content of the JSON AST model file given by ast into this semantic model.

A Smithy semantic model is split into one or more model files. Use this method to add all model files that comprise this semantic model.

See also
https://awslabs.github.io/smithy/2.0/spec/model.html

Definition at line 102 of file model.cpp.

103{
104 Q_D(Model);
105 if (d->error == Error::NoData) {
106 d->error = Error::NoError;
107 }
108
109 // Clear any previously-merged data; we'll need to re-merge later.
110 d->mergedMetadata = QJsonObject{};
111 d->mergedShapes.clear();
112
113 // Fetch the Smithy version.
114 const QVersionNumber version = d->smithyVersion(ast);
115 if (version.majorVersion() > 2) {
116 qCWarning(d->lc).noquote() << tr("Unknown Smithy version %1").arg(version.toString());
117 }
118
119 // Warn on any unrecognised top-level Smithy AST properties.
120 // https://awslabs.github.io/smithy/2.0/spec/json-ast.html#top-level-properties
121 const QStringList keys = ast.keys();
122 for (const QString &key: keys) {
123 const QStringList knownKeys{
124 QStringLiteral("smithy"),QStringLiteral("metadata"), QStringLiteral("shapes") };
125 if (!knownKeys.contains(key)) {
126 qCWarning(d->lc).noquote() << tr("Ignoring unknown Smithy AST property %1").arg(key);
127 }
128 }
129
130 // Process the (optional) metadata.
131 const QJsonValue metadata = ast.value(QStringLiteral("metadata"));
132 if (metadata != QJsonValue::Undefined) {
133 if (!metadata.isObject()) {
134 qCCritical(d->lc).noquote() << tr("Smithy AST metadata is not an object");
135 qDebug().noquote() << metadata;
136 if (d->error == Error::NoError) d->error = Error::InvalidMetadata;
137 return false;
138 }
139 const QJsonObject object = metadata.toObject();
140 qCDebug(d->lc).noquote() << tr("Processing %n metadata entry(s)", nullptr, object.length());
141 for (auto iter = object.constBegin(); iter != object.constEnd(); ++iter) {
142 d->allMetadata.insert(iter.key(), iter.value());
143 }
144 }
145
146 // Process the (optional) shapes.
147 const QJsonValue shapes = ast.value(QStringLiteral("shapes"));
148 if (shapes != QJsonValue::Undefined) {
149 if (!shapes.isObject()) {
150 qCCritical(d->lc).noquote() << tr("Smithy AST shapes is not an object");
151 if (d->error == Error::NoError) d->error = Error::InvalidShapes;
152 return false;
153 }
154 const QJsonObject object = shapes.toObject();
155 qCDebug(d->lc).noquote() << tr("Processing %n shape(s)", nullptr, object.length());
156 for (auto iter = object.constBegin(); iter != object.constEnd(); ++iter) {
157 const ShapeId shapeId(iter.key());
158 if (!shapeId.isValid()) {
159 qCCritical(d->lc).noquote() << tr("Failed to parse shape ID %1").arg(iter.key());
160 if (d->error == Error::NoError) d->error = Error::InvalidShapeId;
161 return false;
162 }
163 if (!shapeId.hasNameSpace()) {
164 qCCritical(d->lc).noquote() << tr("Shape ID %1 has no namespace").arg(iter.key());
165 if (d->error == Error::NoError) d->error = Error::InvalidShapeId;
166 return false;
167 }
168 if (!iter.value().isObject()) {
169 qCCritical(d->lc).noquote() << tr("Shape %1 is not a JSON object").arg(iter.key());
170 if (d->error == Error::NoError) d->error = Error::InvalidShape;
171 return false;
172 }
173 qCDebug(d->lc).noquote() << tr("Processing shape %1").arg(shapeId.toString());
174 const Shape shape{iter.value().toObject(), shapeId};
175 if (!shape.isValid()) {
176 qCCritical(d->lc).noquote() << tr("Failed to process shape %1").arg(iter.key());
177 if (d->error == Error::NoError) d->error = Error::InvalidShape;
178 return false;
179 }
180 d->allShapes.insert(iter.key(), shape);
181 }
182 }
183 return true;
184}
QHash< ShapeId, Shape > shapes(const Shape::Type &type=Shape::Type::Undefined) const
Definition model.cpp:230
Shape shape(const ShapeId &shapeId) const
Definition model.cpp:223

References ShapeId::hasNameSpace(), ShapeId::isValid(), Model(), shape(), shapes(), and ShapeId::toString().

Here is the call graph for this function:

◆ isValid()

bool Model::isValid ( ) const

Definition at line 211 of file model.cpp.

212{
213 Q_D(const Model);
214 return (d->error == Error::NoError);
215}

◆ metadata()

QJsonObject Model::metadata ( ) const

Definition at line 217 of file model.cpp.

218{
219 Q_D(const Model);
220 return d->mergedMetadata;
221}

◆ operator=() [1/2]

Model & Model::operator= ( const Model && model)

Definition at line 65 of file model.cpp.

66{
67 Q_D(Model);
68 d->error = std::move(model.d_ptr->error);
69 d->mergedMetadata = std::move(model.d_ptr->mergedMetadata);
70 d->mergedShapes = std::move(model.d_ptr->mergedShapes);
71 d->allMetadata = std::move(model.d_ptr->allMetadata);
72 d->allShapes = std::move(model.d_ptr->allShapes);
73 return *this;
74}

◆ operator=() [2/2]

Model & Model::operator= ( const Model & model)

Definition at line 54 of file model.cpp.

55{
56 Q_D(Model);
57 d->error = model.d_ptr->error;
58 d->mergedMetadata = model.d_ptr->mergedMetadata;
59 d->mergedShapes = model.d_ptr->mergedShapes;
60 d->allMetadata = model.d_ptr->allMetadata;
61 d->allShapes = model.d_ptr->allShapes;
62 return *this;
63}

◆ shape()

Shape Model::shape ( const ShapeId & shapeId) const
Todo
this should be d->mergedShapes, but using allShapes while finish() is incomplete.

Definition at line 223 of file model.cpp.

224{
225 Q_D(const Model);
226 /// \todo this should be d->mergedShapes, but using allShapes while finish() is incomplete.
227 return d->allShapes.value(shapeId);
228}

References Model().

Referenced by insert().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shapes()

QHash< ShapeId, Shape > Model::shapes ( const Shape::Type & type = Shape::Type::Undefined) const
Todo
this should be d->mergedShapes, but using allShapes while finish() is incomplete.

Definition at line 230 of file model.cpp.

231{
232 Q_D(const Model);
233 if (type == Shape::Type::Undefined) {
234 return d->mergedShapes;
235 }
236 QHash<ShapeId, Shape> shapes;
237 /// \todo this should be d->mergedShapes, but using allShapes while finish() is incomplete.
238 for (auto iter = d->allShapes.constBegin(); iter != d->allShapes.constEnd(); ++iter) {
239 if (iter.value().type() == type) {
240 shapes.insert(iter.key(), iter.value());
241 }
242 }
243 return shapes;
244}
@ Undefined
The shape is undefined, usually the result of an error condition.
Definition shape.h:38

References Model(), shapes(), and Shape::Undefined.

Referenced by insert(), and shapes().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ d_ptr

ModelPrivate* Model::d_ptr
protected

Internal d-pointer.

Definition at line 60 of file model.h.

Referenced by Model(), and ~Model().


The documentation for this class was generated from the following files: