Skip to content

Commit

Permalink
new: Support excluding fields. (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Oct 11, 2023
1 parent db5c500 commit 33fccec
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 7 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## Unreleased

#### 🚀 Updates

- Added an `exclude` attribute for `#[setting]` and `#[schema]` that excludes the field from the
generated schema.
- For `Schematic`, excludes from the schema.
- For `Config`, excludes from the schema, but is still required for the partial config.

## 0.12.1

#### 🚀 Updates
Expand Down
2 changes: 2 additions & 0 deletions crates/config/tests/macros_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub struct ValueTypes {
map: HashMap<String, u64>,
enums: SomeEnum,
s3_value: String,
#[setting(nested, exclude)]
other: OptionalValues,
}

#[derive(Config)]
Expand Down
9 changes: 9 additions & 0 deletions crates/config/tests/schematic_enum_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ pub enum SomeEnum {
A,
B,
C,
#[schema(exclude)]
D,
}

pub struct NonSchematic {
string: String,
}

#[derive(Schematic)]
Expand All @@ -21,4 +27,7 @@ pub struct ValueTypes {
map: HashMap<String, u64>,
enums: SomeEnum,
s3_value: String,

#[schema(exclude)]
other: NonSchematic,
}
16 changes: 14 additions & 2 deletions crates/macros/src/common/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ impl<'l> Container<'l> {
Self::NamedStruct { fields, .. } => {
let schema_types = fields
.iter()
.map(|s| s.generate_schema_type(casing_format))
.filter_map(|f| {
if f.is_excluded() {
None
} else {
Some(f.generate_schema_type(casing_format))
}
})
.collect::<Vec<_>>();

quote! {
Expand All @@ -55,7 +61,13 @@ impl<'l> Container<'l> {
Self::Enum { variants } => {
let variants_types = variants
.iter()
.map(|v| v.generate_schema_type(casing_format, &tagged_format))
.filter_map(|v| {
if v.is_excluded() {
None
} else {
Some(v.generate_schema_type(casing_format, &tagged_format))
}
})
.collect::<Vec<_>>();

quote! {
Expand Down
10 changes: 8 additions & 2 deletions crates/macros/src/common/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ pub struct FieldSerdeArgs {
pub skip: bool,
}

// #[field()], #[setting()]
// #[schema()], #[setting()]
#[derive(FromAttributes, Default)]
#[darling(default, attributes(field, setting))]
#[darling(default, attributes(schema, setting))]
pub struct FieldArgs {
pub exclude: bool,

// config
#[darling(with = "preserve_str_literal", map = "Some")]
pub default: Option<Expr>,
Expand Down Expand Up @@ -75,6 +77,10 @@ impl<'l> Field<'l> {
field
}

pub fn is_excluded(&self) -> bool {
self.args.exclude
}

pub fn is_extendable(&self) -> bool {
self.args.extend
}
Expand Down
9 changes: 7 additions & 2 deletions crates/macros/src/common/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ pub enum TaggedFormat {
Adjacent(String, String),
}

// #[setting()], #[variant()]
// #[setting()], #[schema()]
#[derive(FromAttributes, Default)]
#[darling(default, attributes(setting, variant))]
#[darling(default, attributes(setting, schema))]
pub struct VariantArgs {
pub exclude: bool,
pub null: bool,

// config
Expand Down Expand Up @@ -52,6 +53,10 @@ impl<'l> Variant<'l> {
self.args.default
}

pub fn is_excluded(&self) -> bool {
self.args.exclude
}

pub fn is_nested(&self) -> bool {
self.args.nested
}
Expand Down
2 changes: 1 addition & 1 deletion crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn config_enum(item: TokenStream) -> TokenStream {

// #[derive(Schematic)]
#[cfg(feature = "schema")]
#[proc_macro_derive(Schematic, attributes(schematic, field, variant))]
#[proc_macro_derive(Schematic, attributes(schematic, schema))]
pub fn schematic(item: TokenStream) -> TokenStream {
let input: DeriveInput = parse_macro_input!(item);
let output = schematic::SchematicMacro(Macro::from(&input));
Expand Down

0 comments on commit 33fccec

Please sign in to comment.