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 12670 : pub fn stamp_new(doc: &mut Value, ts: &str) {
11 12670 : if let Some(obj) = doc.as_object_mut() {
12 12670 : obj.insert("createdAt".into(), Value::String(ts.to_owned()));
13 12670 : obj.insert("modifiedAt".into(), Value::String(ts.to_owned()));
14 68716 : for (k, v) in obj.iter_mut() {
15 68716 : if !is_meta(k) {
16 17518 : stamp_instances(v, ts);
17 51198 : }
18 : }
19 0 : }
20 12670 : }
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 17852 : pub fn stamp_instances(v: &mut Value, ts: &str) {
35 17852 : if let Some(arr) = v.as_array_mut() {
36 17870 : for inst in arr {
37 17870 : if let Some(o) = inst.as_object_mut() {
38 17870 : o.insert("createdAt".into(), Value::String(ts.to_owned()));
39 17870 : o.insert("modifiedAt".into(), Value::String(ts.to_owned()));
40 72100 : for (k, sub) in o.iter_mut() {
41 72100 : if sub.is_array() && !antares_jsonld::RESERVED_MEMBERS.contains(&k.as_str()) {
42 80 : stamp_instances(sub, ts);
43 72020 : }
44 : }
45 0 : }
46 : }
47 0 : }
48 17852 : }
|