-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PoC for moving enums from common -> Nexus db::model #776
Changes from 2 commits
1c8f8d9
455f645
127d890
90c8777
636c72d
a51793a
8e2cc6f
12100ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,67 @@ macro_rules! impl_enum_type { | |
} | ||
} | ||
|
||
macro_rules! impl_enum_type2 { | ||
( | ||
$(#[$enum_meta:meta])* | ||
pub struct $diesel_type:ident; | ||
|
||
$(#[$model_meta:meta])* | ||
pub enum $model_type:ident; | ||
$($enum_item:ident => $sql_value:literal)+ | ||
) => { | ||
|
||
$(#[$enum_meta])* | ||
pub struct $diesel_type; | ||
|
||
$(#[$model_meta])* | ||
pub enum $model_type { | ||
$( | ||
$enum_item, | ||
)* | ||
} | ||
|
||
impl<DB> ToSql<$diesel_type, DB> for $model_type | ||
where | ||
DB: Backend, | ||
{ | ||
fn to_sql<W: std::io::Write>( | ||
&self, | ||
out: &mut serialize::Output<W, DB>, | ||
) -> serialize::Result { | ||
match self { | ||
$( | ||
$model_type::$enum_item => { | ||
out.write_all($sql_value)? | ||
} | ||
)* | ||
} | ||
Ok(IsNull::No) | ||
} | ||
} | ||
|
||
impl<DB> FromSql<$diesel_type, DB> for $model_type | ||
where | ||
DB: Backend + for<'a> BinaryRawValue<'a>, | ||
{ | ||
fn from_sql(bytes: RawValue<DB>) -> deserialize::Result<Self> { | ||
match DB::as_bytes(bytes) { | ||
$( | ||
$sql_value => { | ||
Ok($model_type::$enum_item) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original did this, i.e., Ok($model_type(<$ext_type>::$enum_item)) |
||
} | ||
)* | ||
_ => { | ||
Err(concat!("Unrecognized enum variant for ", | ||
stringify!{$model_type}) | ||
.into()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Complete diff with the existing macro. @@ -50,7 +50,7 @@ macro_rules! impl_enum_type {
pub struct $diesel_type:ident;
$(#[$model_meta:meta])*
- pub struct $model_type:ident(pub $ext_type:ty);
+ pub enum $model_type:ident;
$($enum_item:ident => $sql_value:literal)+
) => {
@@ -58,7 +58,11 @@ macro_rules! impl_enum_type {
pub struct $diesel_type;
$(#[$model_meta])*
- pub struct $model_type(pub $ext_type);
+ pub enum $model_type {
+ $(
+ $enum_item,
+ )*
+ }
impl<DB> ToSql<$diesel_type, DB> for $model_type
where
@@ -68,9 +72,9 @@ macro_rules! impl_enum_type {
&self,
out: &mut serialize::Output<W, DB>,
) -> serialize::Result {
- match self.0 {
+ match self {
$(
- <$ext_type>::$enum_item => {
+ $model_type::$enum_item => {
out.write_all($sql_value)?
}
)*
@@ -87,7 +91,7 @@ macro_rules! impl_enum_type {
match DB::as_bytes(bytes) {
$(
$sql_value => {
- Ok($model_type(<$ext_type>::$enum_item))
+ Ok($model_type::$enum_item)
}
)*
_ => { |
||
/// Newtype wrapper around [external::Name]. | ||
#[derive( | ||
Clone, | ||
|
@@ -1584,14 +1645,14 @@ impl From<params::VpcSubnetUpdate> for VpcSubnetUpdate { | |
} | ||
} | ||
|
||
impl_enum_type!( | ||
impl_enum_type2!( | ||
#[derive(SqlType, Debug)] | ||
#[postgres(type_name = "vpc_router_kind", type_schema = "public")] | ||
pub struct VpcRouterKindEnum; | ||
|
||
#[derive(Clone, Debug, AsExpression, FromSqlRow)] | ||
#[derive(Clone, Copy, Debug, AsExpression, FromSqlRow, PartialEq)] | ||
#[sql_type = "VpcRouterKindEnum"] | ||
pub struct VpcRouterKind(pub external::VpcRouterKind); | ||
pub enum VpcRouterKind; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do like the idea of this thing actually being an enum now; that's way more intuitive |
||
|
||
// Enum values | ||
System => b"system" | ||
|
@@ -1613,16 +1674,11 @@ impl VpcRouter { | |
pub fn new( | ||
router_id: Uuid, | ||
vpc_id: Uuid, | ||
kind: external::VpcRouterKind, | ||
kind: VpcRouterKind, | ||
params: params::VpcRouterCreate, | ||
) -> Self { | ||
let identity = VpcRouterIdentity::new(router_id, params.identity); | ||
Self { | ||
identity, | ||
vpc_id, | ||
kind: VpcRouterKind(kind), | ||
rcgen: Generation::new(), | ||
} | ||
Self { identity, vpc_id, kind, rcgen: Generation::new() } | ||
} | ||
} | ||
|
||
|
@@ -1633,16 +1689,6 @@ impl DatastoreCollection<RouterRoute> for VpcRouter { | |
type CollectionIdColumn = router_route::dsl::router_id; | ||
} | ||
|
||
impl Into<external::VpcRouter> for VpcRouter { | ||
fn into(self) -> external::VpcRouter { | ||
external::VpcRouter { | ||
identity: self.identity(), | ||
vpc_id: self.vpc_id, | ||
kind: self.kind.0, | ||
} | ||
} | ||
} | ||
|
||
#[derive(AsChangeset)] | ||
#[table_name = "vpc_router"] | ||
pub struct VpcRouterUpdate { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,6 +147,46 @@ impl Into<VpcSubnet> for model::VpcSubnet { | |
} | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, JsonSchema)] | ||
#[serde(rename_all = "snake_case")] | ||
pub enum VpcRouterKind { | ||
System, | ||
Custom, | ||
} | ||
|
||
impl Into<VpcRouterKind> for model::VpcRouterKind { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to I think it's functionally identical to this, but it's preferable to impl From over Into when given the option:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, definitely. We should change them all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 636c72d. I'll change the others in a separate PR to reduce noise. |
||
fn into(self) -> VpcRouterKind { | ||
match self { | ||
model::VpcRouterKind::Custom => VpcRouterKind::Custom, | ||
model::VpcRouterKind::System => VpcRouterKind::System, | ||
} | ||
} | ||
} | ||
|
||
/// A VPC router defines a series of rules that indicate where traffic | ||
/// should be sent depending on its destination. | ||
#[derive(ObjectIdentity, Clone, Debug, Deserialize, Serialize, JsonSchema)] | ||
pub struct VpcRouter { | ||
/// common identifying metadata | ||
#[serde(flatten)] | ||
pub identity: IdentityMetadata, | ||
|
||
pub kind: VpcRouterKind, | ||
|
||
/// The VPC to which the router belongs. | ||
pub vpc_id: Uuid, | ||
} | ||
|
||
impl Into<VpcRouter> for model::VpcRouter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above |
||
fn into(self) -> VpcRouter { | ||
VpcRouter { | ||
identity: self.identity(), | ||
vpc_id: self.vpc_id, | ||
kind: self.kind.into(), | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* RACKS | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the analogous line in the original. Instead of defining the enum, it wrapped the imported external one.