Skip to main content

unitycatalog_common/models/
object.rs

1use crate::Error;
2use crate::models::{
3    ObjectLabel, Resource, ResourceExt, ResourceIdent, ResourceName, ResourceRef, Table,
4};
5
6use super::tables::v1::TableSummary;
7
8/// Project-specific alias for `resource_store::Object` with our generated `ObjectLabel`.
9pub type Object = olai_store::Object<ObjectLabel>;
10
11impl ResourceExt for Object {
12    fn resource_name(&self) -> ResourceName {
13        self.name.clone()
14    }
15
16    fn resource_ref(&self) -> ResourceRef {
17        ResourceRef::Uuid(self.id)
18    }
19
20    fn resource_ident(&self) -> ResourceIdent {
21        self.label.to_ident(self.id)
22    }
23}
24
25impl ResourceExt for Resource {
26    fn resource_name(&self) -> ResourceName {
27        match self {
28            Resource::Agent(obj) => obj.resource_name(),
29            Resource::AgentSkill(obj) => obj.resource_name(),
30            Resource::Share(obj) => obj.resource_name(),
31            Resource::Credential(obj) => obj.resource_name(),
32            Resource::Catalog(obj) => obj.resource_name(),
33            Resource::Schema(obj) => obj.resource_name(),
34            Resource::Table(obj) => obj.resource_name(),
35            Resource::ExternalLocation(obj) => obj.resource_name(),
36            Resource::Recipient(obj) => obj.resource_name(),
37            Resource::Provider(obj) => obj.resource_name(),
38            Resource::Column(obj) => obj.resource_name(),
39            Resource::Volume(obj) => obj.resource_name(),
40            Resource::Function(obj) => obj.resource_name(),
41            Resource::TagPolicy(obj) => obj.resource_name(),
42            Resource::StagingTable(obj) => obj.resource_name(),
43        }
44    }
45
46    fn resource_ref(&self) -> ResourceRef {
47        match self {
48            Resource::Agent(obj) => obj.resource_ref(),
49            Resource::AgentSkill(obj) => obj.resource_ref(),
50            Resource::Share(obj) => obj.resource_ref(),
51            Resource::Credential(obj) => obj.resource_ref(),
52            Resource::Catalog(obj) => obj.resource_ref(),
53            Resource::Schema(obj) => obj.resource_ref(),
54            Resource::Table(obj) => obj.resource_ref(),
55            Resource::ExternalLocation(obj) => obj.resource_ref(),
56            Resource::Recipient(obj) => obj.resource_ref(),
57            Resource::Provider(obj) => obj.resource_ref(),
58            Resource::Column(obj) => obj.resource_ref(),
59            Resource::Volume(obj) => obj.resource_ref(),
60            Resource::Function(obj) => obj.resource_ref(),
61            Resource::TagPolicy(obj) => obj.resource_ref(),
62            Resource::StagingTable(obj) => obj.resource_ref(),
63        }
64    }
65
66    fn resource_ident(&self) -> ResourceIdent {
67        match self {
68            Resource::Agent(obj) => obj.resource_ident(),
69            Resource::AgentSkill(obj) => obj.resource_ident(),
70            Resource::Share(obj) => obj.resource_ident(),
71            Resource::Credential(obj) => obj.resource_ident(),
72            Resource::Catalog(obj) => obj.resource_ident(),
73            Resource::Schema(obj) => obj.resource_ident(),
74            Resource::Table(obj) => obj.resource_ident(),
75            Resource::ExternalLocation(obj) => obj.resource_ident(),
76            Resource::Recipient(obj) => obj.resource_ident(),
77            Resource::Provider(obj) => obj.resource_ident(),
78            Resource::Column(obj) => obj.resource_ident(),
79            Resource::Volume(obj) => obj.resource_ident(),
80            Resource::Function(obj) => obj.resource_ident(),
81            Resource::TagPolicy(obj) => obj.resource_ident(),
82            Resource::StagingTable(obj) => obj.resource_ident(),
83        }
84    }
85}
86
87impl TryFrom<Resource> for Object {
88    type Error = Error;
89
90    fn try_from(resource: Resource) -> Result<Self, Self::Error> {
91        match resource {
92            Resource::Agent(obj) => obj.try_into(),
93            Resource::AgentSkill(obj) => obj.try_into(),
94            Resource::Share(obj) => obj.try_into(),
95            Resource::Credential(obj) => obj.try_into(),
96            Resource::Catalog(obj) => obj.try_into(),
97            Resource::Schema(obj) => obj.try_into(),
98            Resource::Table(obj) => obj.try_into(),
99            Resource::ExternalLocation(obj) => obj.try_into(),
100            Resource::Recipient(obj) => obj.try_into(),
101            Resource::Provider(obj) => obj.try_into(),
102            Resource::Column(obj) => obj.try_into(),
103            Resource::Volume(obj) => obj.try_into(),
104            Resource::Function(obj) => obj.try_into(),
105            Resource::TagPolicy(obj) => obj.try_into(),
106            Resource::StagingTable(obj) => obj.try_into(),
107        }
108    }
109}
110
111impl TryFrom<Object> for Resource {
112    type Error = Error;
113
114    fn try_from(obj: Object) -> Result<Self, Self::Error> {
115        match obj.label {
116            ObjectLabel::Agent => Ok(Resource::Agent(obj.try_into()?)),
117            ObjectLabel::AgentSkill => Ok(Resource::AgentSkill(obj.try_into()?)),
118            ObjectLabel::Share => Ok(Resource::Share(obj.try_into()?)),
119            ObjectLabel::Credential => Ok(Resource::Credential(obj.try_into()?)),
120            ObjectLabel::Catalog => Ok(Resource::Catalog(obj.try_into()?)),
121            ObjectLabel::Schema => Ok(Resource::Schema(obj.try_into()?)),
122            ObjectLabel::Table => Ok(Resource::Table(obj.try_into()?)),
123            ObjectLabel::ExternalLocation => Ok(Resource::ExternalLocation(obj.try_into()?)),
124            ObjectLabel::Recipient => Ok(Resource::Recipient(obj.try_into()?)),
125            ObjectLabel::Provider => Ok(Resource::Provider(obj.try_into()?)),
126            ObjectLabel::Column => Ok(Resource::Column(obj.try_into()?)),
127            ObjectLabel::Volume => Ok(Resource::Volume(obj.try_into()?)),
128            ObjectLabel::Function => Ok(Resource::Function(obj.try_into()?)),
129            ObjectLabel::TagPolicy => Ok(Resource::TagPolicy(obj.try_into()?)),
130            ObjectLabel::StagingTable => Ok(Resource::StagingTable(obj.try_into()?)),
131        }
132    }
133}
134
135impl From<Table> for TableSummary {
136    fn from(table: Table) -> Self {
137        TableSummary {
138            table_type: table.table_type,
139            full_name: table.full_name,
140        }
141    }
142}