Skip to main content

conductor/models/
schema.rs

1// Copyright {{.Year}} Conductor OSS
2// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3
4use serde::{Deserialize, Serialize};
5
6/// Schema definition.
7#[derive(Debug, Clone, Default, Serialize, Deserialize)]
8#[serde(rename_all = "camelCase")]
9pub struct SchemaDef {
10    /// Schema name.
11    pub name: String,
12
13    /// Schema version.
14    pub version: i32,
15
16    /// Schema type (e.g., "JSON").
17    #[serde(rename = "type", default = "default_schema_type")]
18    pub schema_type: String,
19
20    /// The actual schema definition (JSON Schema).
21    #[serde(default)]
22    pub data: serde_json::Value,
23
24    /// Whether this schema is used for external storage.
25    #[serde(default)]
26    pub external_ref: bool,
27
28    /// Create time.
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub create_time: Option<i64>,
31
32    /// Created by.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub created_by: Option<String>,
35
36    /// Update time.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub update_time: Option<i64>,
39
40    /// Updated by.
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub updated_by: Option<String>,
43}
44
45fn default_schema_type() -> String {
46    "JSON".to_owned()
47}
48
49impl SchemaDef {
50    /// Create a new schema definition.
51    pub fn new(name: impl Into<String>, version: i32, data: serde_json::Value) -> Self {
52        Self {
53            name: name.into(),
54            version,
55            schema_type: default_schema_type(),
56            data,
57            ..Default::default()
58        }
59    }
60
61    /// Create a JSON schema.
62    pub fn json_schema(name: impl Into<String>, version: i32, schema: serde_json::Value) -> Self {
63        Self {
64            name: name.into(),
65            version,
66            schema_type: "JSON".to_owned(),
67            data: schema,
68            ..Default::default()
69        }
70    }
71}