Line data Source code
1 : // SPDX-License-Identifier: EUPL-1.2
2 : //! The system attributes of a write: `createdAt`/`modifiedAt` on the
3 : //! entity and on every attribute instance (4.8, 5.2.4), stamped at the
4 : //! moment the operation is applied.
5 :
6 : use antares_model::is_meta;
7 : use serde_json::Value;
8 :
9 : /// Inject server-managed timestamps into a freshly expanded doc.
10 17698 : pub fn stamp_new(doc: &mut Value, ts: &str) {
11 17698 : if let Some(obj) = doc.as_object_mut() {
12 17698 : obj.insert("createdAt".into(), Value::String(ts.to_owned()));
13 17698 : obj.insert("modifiedAt".into(), Value::String(ts.to_owned()));
14 99970 : for (k, v) in obj.iter_mut() {
15 99970 : if !is_meta(k) {
16 28496 : stamp_instances(v, ts);
17 71474 : }
18 : }
19 0 : }
20 17698 : }
21 :
22 : /// Stamp one Attribute's instances, and their sub-Attributes, with the
23 : /// 4.8 timestamps: createdAt and modifiedAt are "the temporal Property at
24 : /// which the Entity, Property or Relationship was entered into"/"last
25 : /// modified in an NGSI-LD system", a sub-Property is a Property, and the
26 : /// value is server-generated — whatever the client sent is overwritten.
27 : /// Every write path that brings a new Attribute in uses this one: 5.6.1
28 : /// through `stamp_new`, 5.6.2 and 5.6.3 through `attrs.rs`, so the served
29 : /// representation does not depend on which operation wrote the Attribute.
30 : /// The temporal write path stamps differently
31 : /// (`temporal::stamp_temporal_instances`):
32 : /// there an instance is the unit of history and carries an instanceId, and
33 : /// sub-Attributes are part of the instance, not stamped separately.
34 81732 : pub fn stamp_instances(v: &mut Value, ts: &str) {
35 81732 : if let Some(arr) = v.as_array_mut() {
36 82462 : for inst in arr {
37 82462 : if let Some(o) = inst.as_object_mut() {
38 82462 : o.insert("createdAt".into(), Value::String(ts.to_owned()));
39 82462 : o.insert("modifiedAt".into(), Value::String(ts.to_owned()));
40 386620 : for (k, sub) in o.iter_mut() {
41 386620 : if sub.is_array() && !antares_jsonld::RESERVED_MEMBERS.contains(&k.as_str()) {
42 1392 : stamp_instances(sub, ts);
43 385228 : }
44 : }
45 0 : }
46 : }
47 0 : }
48 81732 : }
|