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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//! Smithy model helpers
//! - some constants used for model validation
//! - ModelIndex is a "cache" of a smithy model grouped by shape kind and sorted by identifier name
//! - various macros used in the codegen crate
//!
use std::ops::Deref;
//use crate::strings::{to_pascal_case, to_snake_case};
use crate::{
    error::{Error, Result},
    JsonValue,
};
use atelier_core::model::shapes::StructureOrUnion;
use atelier_core::{
    model::{
        shapes::{AppliedTraits, HasTraits, MemberShape, Operation, ShapeKind},
        values::{Number, Value as NodeValue},
        HasIdentity, Identifier, Model, NamespaceID, ShapeID,
    },
    prelude::prelude_namespace_id,
};
use lazy_static::lazy_static;
use serde::{de::DeserializeOwned, Deserialize};
use std::str::FromStr;

const WASMCLOUD_MODEL_NAMESPACE: &str = "org.wasmcloud.model";
const WASMCLOUD_CORE_NAMESPACE: &str = "org.wasmcloud.core";

//const TRAIT_ACTOR_RECEIVER: &str = "actorReceiver";
//const TRAIT_CAPABILITY: &str = "capability";
//const TRAIT_PROVIDER_RECEIVER: &str = "providerReceiver";
const TRAIT_CODEGEN_RUST: &str = "codegenRust";
const TRAIT_SERIALIZATION: &str = "serialization";
const TRAIT_WASMBUS: &str = "wasmbus";
const TRAIT_WASMBUS_DATA: &str = "wasmbusData";
const TRAIT_FIELD_NUM: &str = "n";
const TRAIT_RENAME: &str = "rename";
//const TRAIT_SERIALIZE_RENAME: &str = "rename";

lazy_static! {
    static ref WASMCLOUD_MODEL_NAMESPACE_ID: NamespaceID =
        NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE);
    static ref WASMCLOUD_CORE_NAMESPACE_ID: NamespaceID =
        NamespaceID::new_unchecked(WASMCLOUD_CORE_NAMESPACE);
    static ref SERIALIZATION_TRAIT_ID: ShapeID = ShapeID::new(
        NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE),
        Identifier::from_str(TRAIT_SERIALIZATION).unwrap(),
        None
    );
    static ref CODEGEN_RUST_TRAIT_ID: ShapeID = ShapeID::new(
        NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE),
        Identifier::from_str(TRAIT_CODEGEN_RUST).unwrap(),
        None
    );
    static ref WASMBUS_TRAIT_ID: ShapeID = ShapeID::new(
        NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE),
        Identifier::from_str(TRAIT_WASMBUS).unwrap(),
        None
    );
    static ref WASMBUS_DATA_TRAIT_ID: ShapeID = ShapeID::new(
        NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE),
        Identifier::from_str(TRAIT_WASMBUS_DATA).unwrap(),
        None
    );
    static ref FIELD_NUM_TRAIT_ID: ShapeID = ShapeID::new(
        NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE),
        Identifier::from_str(TRAIT_FIELD_NUM).unwrap(),
        None
    );
    static ref RENAME_TRAIT_ID: ShapeID = ShapeID::new(
        NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE),
        Identifier::from_str(TRAIT_RENAME).unwrap(),
        None
    );
}

/// namespace for org.wasmcloud.model
pub fn wasmcloud_model_namespace() -> &'static NamespaceID {
    &WASMCLOUD_MODEL_NAMESPACE_ID
}

#[cfg(feature = "wasmbus")]
/// shape id of trait @wasmbus
pub fn wasmbus_trait() -> &'static ShapeID {
    &WASMBUS_TRAIT_ID
}

#[allow(dead_code)]
#[cfg(feature = "wasmbus")]
/// shape id of trait @wasmbusData
pub fn wasmbus_data_trait() -> &'static ShapeID {
    &WASMBUS_DATA_TRAIT_ID
}

/// shape id of trait @serialization
pub fn serialization_trait() -> &'static ShapeID {
    &SERIALIZATION_TRAIT_ID
}

/// shape id of trait @codegenRust
pub fn codegen_rust_trait() -> &'static ShapeID {
    &CODEGEN_RUST_TRAIT_ID
}

/// shape id of trait @n
pub fn field_num_trait() -> &'static ShapeID {
    &FIELD_NUM_TRAIT_ID
}

/// shape id of trait @rename
pub fn rename_trait() -> &'static ShapeID {
    &RENAME_TRAIT_ID
}

#[allow(dead_code)]
pub enum CommentKind {
    Inner,
    Documentation,
    /// inside a multi-line quote, as in python
    InQuote,
}

// Modifiers on data type
// This enum may be extended in the future if other variations are required.
// It's recursively composable, so you could represent &Option<&Value>
// with `Ty::Ref(Ty::Opt(Ty::Ref(id)))`
pub(crate) enum Ty<'typ> {
    /// write a plain shape declaration
    Shape(&'typ ShapeID),
    /// write a type wrapped in Option<>
    Opt(&'typ ShapeID),
    /// write a reference type: preceeded by &
    Ref(&'typ ShapeID),
}
// verify that the model doesn't contain unsupported types
#[macro_export]
macro_rules! expect_empty {
    ($list:expr, $msg:expr) => {
        if !$list.is_empty() {
            return Err(Error::InvalidModel(format!(
                "{}: {}",
                $msg,
                $list
                    .keys()
                    .map(|k| k.to_string())
                    .collect::<Vec<String>>()
                    .join(",")
            )));
        }
    };
}

#[macro_export]
macro_rules! unsupported_shape {
    ($fn_name:ident, $shape_type:ty, $doc:expr) => {
        #[allow(unused_variables)]
        fn $fn_name(
            &mut self,
            id: &ShapeID,
            traits: &AppliedTraits,
            shape: &$shape_type,
        ) -> Result<()> {
            return Err(crate::error::Error::UnsupportedShape(
                id.to_string(),
                $doc.to_string(),
            ));
        }
    };
}

/// true if namespace matches, or if there is no namespace constraint
pub fn is_opt_namespace(id: &ShapeID, ns: &Option<NamespaceID>) -> bool {
    match ns {
        Some(ns) => id.namespace() == ns,
        None => true,
    }
}

/// Finds the operation in the model or returns error
pub fn get_operation<'model>(
    model: &'model Model,
    operation_id: &'_ ShapeID,
    service_id: &'_ Identifier,
) -> Result<(&'model Operation, &'model AppliedTraits)> {
    let op = model
        .shapes()
        .filter(|t| t.id() == operation_id)
        .find_map(|t| {
            if let ShapeKind::Operation(op) = t.body() {
                Some((op, t.traits()))
            } else {
                None
            }
        })
        .ok_or_else(|| {
            Error::Model(format!(
                "missing operation {} for service {}",
                &operation_id.to_string(),
                &service_id.to_string()
            ))
        })?;
    Ok(op)
}

/// Returns trait as deserialized object, or None if the trait is not defined.
/// Returns error if the deserialization failed.
pub fn get_trait<T: DeserializeOwned>(traits: &AppliedTraits, id: &ShapeID) -> Result<Option<T>> {
    match traits.get(id) {
        Some(Some(val)) => match trait_value(val) {
            Ok(obj) => Ok(Some(obj)),
            Err(e) => Err(e),
        },
        Some(None) => Ok(None),
        None => Ok(None),
    }
}

/// Convert trait object to its native type
pub fn trait_value<T: DeserializeOwned>(value: &NodeValue) -> Result<T> {
    let json = value_to_json(value);
    let obj = serde_json::from_value(json)?;
    Ok(obj)
}

/// Convert smithy model 'Value' to a json object
pub fn value_to_json(value: &NodeValue) -> JsonValue {
    match value {
        NodeValue::None => JsonValue::Null,
        NodeValue::Array(v) => JsonValue::Array(v.iter().map(value_to_json).collect()),
        NodeValue::Object(v) => {
            let mut object = crate::JsonMap::default();
            for (k, v) in v {
                let _ = object.insert(k.clone(), value_to_json(v));
            }
            JsonValue::Object(object)
        }
        NodeValue::Number(v) => match v {
            Number::Integer(v) => JsonValue::Number((*v).into()),
            Number::Float(v) => JsonValue::Number(serde_json::Number::from_f64(*v).unwrap()),
        },
        NodeValue::Boolean(v) => JsonValue::Bool(*v),
        NodeValue::String(v) => JsonValue::String(v.clone()),
    }
}

/// resolve shape to its underlying shape
/// e.g., if you have a declaration "string Foo",
/// it will resolve Foo into smithy.api#String
pub fn resolve<'model>(model: &'model Model, shape: &'model ShapeID) -> &'model ShapeID {
    if let Some(resolved) = model.shape(shape) {
        resolved.id()
    } else {
        shape
    }
}

/// Returns true if the type has a natural default (zero, empty set/list/map, etc.).
/// Doesn't work for user-defined structs, only (most) simple types,
/// and set, list, and map.
///
/// This can be used for deserialization,
/// to allow missing fields to be filled in with the default.
///
/// The smithy developers considered and rejected the idea of being able to declare
/// a default value that is not zero (such as http status with a default 200),
/// which would be in the realm of business logic and outside the scope of smithy.
/// This default only applies to simple types that have a zero value,
/// and empty sets, list, and maps.
pub fn has_default(model: &'_ Model, member: &MemberShape) -> bool {
    let id = resolve(model, member.target());
    #[allow(unused_mut)]
    let mut has = false;

    if id.namespace().eq(prelude_namespace_id()) {
        let name = id.shape_name().to_string();
        cfg_if::cfg_if! {
            if #[cfg(feature = "BigInteger")] {
                has = has || &name == "bigInteger";
            }
        }
        cfg_if::cfg_if! {
            if #[cfg(feature = "BigDecimal")] {
                has = has || &name == "bigDecimal";
            }
        }
        has || matches!(
            name.as_str(),
            // some aggregate types
            "List" | "Set" | "Map"
            // most simple types
            | "Blob" | "Boolean" | "String" | "Byte" | "Short"
            | "Integer" | "Long" | "Float" | "Double"
            | "Timestamp"
        )
        // excluded: Resource, Operation, Service, Document, Union
    } else {
        false
        // for any other type, return false.
        // if there was a need to override this,
        // a trait could be added
    }
}

pub(crate) struct NumberedMember {
    field_num: Option<u16>,
    shape: MemberShape,
}

impl NumberedMember {
    pub(crate) fn new(member: &MemberShape) -> Result<Self> {
        Ok(NumberedMember {
            shape: member.to_owned(),
            field_num: get_trait::<u16>(member.traits(), field_num_trait()).map_err(|e| {
                Error::Model(format!(
                    "invalid field number @n() for field '{}': {}",
                    member.id(),
                    e
                ))
            })?,
        })
    }
    pub(crate) fn field_num(&self) -> &Option<u16> {
        &self.field_num
    }
}

impl Deref for NumberedMember {
    type Target = MemberShape;

    fn deref(&self) -> &Self::Target {
        &self.shape
    }
}

use std::iter::Iterator;
/// Returns sorted list of fields for the structure, and whether it is numbered.
/// If there are any errors in numbering, returns Error::Model
pub(crate) fn get_sorted_fields(
    id: &Identifier,
    strukt: &StructureOrUnion,
) -> Result<(Vec<NumberedMember>, bool)> {
    let mut fields = strukt
        .members()
        .map(NumberedMember::new)
        .collect::<Result<Vec<NumberedMember>>>()?;
    let has_numbers = crate::model::has_field_numbers(&fields, &id.to_string())?;
    // Sort fields for deterministic output
    // by number, if declared with numbers, otherwise by name
    if has_numbers {
        fields.sort_by_key(|f| f.field_num().unwrap());
    } else {
        fields.sort_by_key(|f| f.id().to_owned());
    }
    Ok((fields, has_numbers))
}

/// Checks whether a struct has complete and valid field numbers.
/// Returns true if all fields have unique numbers.
/// Returns false if no fields are numbered.
/// Returns Err if fields are incompletely numbered, or there are duplicate numbers.
pub(crate) fn has_field_numbers(fields: &[NumberedMember], name: &str) -> Result<bool> {
    let mut numbered = std::collections::BTreeSet::default();
    for f in fields.iter() {
        if let Some(n) = f.field_num() {
            numbered.insert(*n);
        }
    }
    if numbered.is_empty() {
        Ok(false)
    } else if numbered.len() == fields.len() {
        // all fields are numbered uniquely
        Ok(true)
    } else {
        Err(crate::Error::Model(
                format!("structure {} has incomplete or invalid field numbers: either some fields are missing the '@n()' trait, or some fields have duplicate numbers.", name)
            ))
    }
}

/*
pub fn get_metadata(model: &Model) -> JsonMap {
    let mut metadata_map = JsonMap::default();
    for (key, value) in model.metadata() {
        let _ = metadata_map.insert(key.to_string(), value_to_json(value));
    }
    metadata_map
}
 */

/// Map namespace to package
///   rust: crate_name
///   other-languages: TBD
#[derive(Clone, Deserialize)]
pub struct PackageName {
    pub namespace: String,
    #[serde(rename = "crate")]
    pub crate_name: Option<String>,
    #[serde(rename = "py_module")]
    pub py_module: Option<String>,
}