From 2dc6ddd2e33ab7fd17d700718949fd3ce3c27293 Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Mon, 29 May 2023 21:59:18 +0800 Subject: [PATCH] Remove butane_type for FieldType derived enums (#99) Deriving `FieldType` with JSON already added a custom type mapping, but a parallel was missing when deriving `FieldType` for an `enum`. This change adds the latter to bring consistency and avoid the `butane_type` macro for enums. --- butane/tests/custom_enum_derived.rs | 3 +-- butane_codegen/src/lib.rs | 15 +++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/butane/tests/custom_enum_derived.rs b/butane/tests/custom_enum_derived.rs index 4e714b2e..e015a7e1 100644 --- a/butane/tests/custom_enum_derived.rs +++ b/butane/tests/custom_enum_derived.rs @@ -1,12 +1,11 @@ // Tests deriving FieldType for an enum use butane::db::Connection; use butane::prelude::*; -use butane::{butane_type, model, query}; +use butane::{model, query}; use butane::{FieldType, FromSql, ObjectState, SqlVal, ToSql}; use butane_test_helper::*; -#[butane_type(Text)] #[derive(PartialEq, Eq, Debug, Clone, FieldType)] enum Whatsit { Foo, diff --git a/butane_codegen/src/lib.rs b/butane_codegen/src/lib.rs index 9dcec20d..5ac22378 100644 --- a/butane_codegen/src/lib.rs +++ b/butane_codegen/src/lib.rs @@ -3,7 +3,8 @@ extern crate proc_macro; -use butane_core::{codegen, make_compile_error, migrations}; +use butane_core::migrations::adb::{DeferredSqlType, TypeIdentifier}; +use butane_core::{codegen, make_compile_error,SqlType, migrations}; use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use proc_macro2::TokenTree; @@ -189,6 +190,15 @@ fn derive_field_type_for_enum(ident: &Ident, data_enum: syn::DataEnum) -> TokenS return derive_field_type_with_json(ident); } + let mut migrations = migrations_for_dir(); + + codegen::add_custom_type( + &mut migrations, + ident.to_string(), + DeferredSqlType::KnownId(TypeIdentifier::Ty(SqlType::Text)), + ) + .unwrap(); + let match_arms_to_string: Vec = data_enum .variants .iter() @@ -254,9 +264,6 @@ fn derive_field_type_for_enum(ident: &Ident, data_enum: syn::DataEnum) -> TokenS #[cfg(feature = "json")] fn derive_field_type_with_json(struct_name: &Ident) -> TokenStream { - use butane_core::migrations::adb::{DeferredSqlType, TypeIdentifier}; - use butane_core::SqlType; - let mut migrations = migrations_for_dir(); codegen::add_custom_type(