Skip to content

Commit 1b2c0c5

Browse files
committed
rename Extra.field to Extra.field_we_are_currently_assigning_to
1 parent afc4bad commit 1b2c0c5

File tree

4 files changed

+38
-31
lines changed

4 files changed

+38
-31
lines changed

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly
1+
nightly-2023-03-01

src/serializers/shared.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,16 @@ impl CombinedSerializer {
7474
build_context: &mut BuildContext<CombinedSerializer>,
7575
) -> PyResult<CombinedSerializer> {
7676
match lookup_type {
77-
// TODO: sort alphabetically?
77+
// The left hand side of this match is a 1:1 match with the `type` field we use as a discriminator
78+
// in our CoreSchema union
79+
// The right hand side is _generally_ a 1:1 match but there are cases where we use a `Builder`
80+
// on the right that may have internal logic to return different validators or just build a
81+
// a more complex validator (e.g. building a union of isinstance validators or something).
82+
// So to get from Python -> Rust implementation you should trace the `type` on the left hand side
83+
// of the match and then if the right hand side is a `{Type}Validator` you've found the implementation.
84+
// If the right hand side is a `{Type}Builder` you'll have to look into it's `build()` method to see
85+
// what it actually returns.
86+
// TODO: sort alphabetically? By left hand side string or right hand side type? Or RHS type's module filename?
7887
"any" => super::type_serializers::any::AnySerializer::build(schema, config, build_context),
7988
"none" => super::type_serializers::simple::NoneSerializer::build(schema, config, build_context),
8089
"bool" => super::type_serializers::simple::BoolSerializer::build(schema, config, build_context),

src/validators/function.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,11 @@ use crate::recursion_guard::RecursionGuard;
1414
use super::generator::InternalValidator;
1515
use super::{build_validator, BuildContext, BuildValidator, CombinedValidator, Extra, Validator};
1616

17-
pub struct FunctionBuilder;
18-
19-
impl BuildValidator for FunctionBuilder {
20-
const EXPECTED_TYPE: &'static str = "function";
21-
22-
fn build(
23-
schema: &PyDict,
24-
config: Option<&PyDict>,
25-
build_context: &mut BuildContext<CombinedValidator>,
26-
) -> PyResult<CombinedValidator> {
27-
let type_: &str = schema.get_as_req(intern!(schema.py(), "type"))?;
28-
match type_ {
29-
"function-before" => FunctionBeforeValidator::build(schema, config, build_context),
30-
"function-after" => FunctionAfterValidator::build(schema, config, build_context),
31-
"function-wrap" => FunctionWrapValidator::build(schema, config, build_context),
32-
// must be "plain"
33-
_ => FunctionPlainValidator::build(schema, config),
34-
}
35-
}
36-
}
37-
3817
macro_rules! impl_build {
3918
($impl_name:ident, $name:literal) => {
40-
impl $impl_name {
41-
pub fn build(
19+
impl BuildValidator for $impl_name {
20+
const EXPECTED_TYPE: &'static str = $name;
21+
fn build(
4222
schema: &PyDict,
4323
config: Option<&PyDict>,
4424
build_context: &mut BuildContext<CombinedValidator>,
@@ -153,8 +133,14 @@ pub struct FunctionPlainValidator {
153133
name: String,
154134
}
155135

156-
impl FunctionPlainValidator {
157-
pub fn build(schema: &PyDict, config: Option<&PyDict>) -> PyResult<CombinedValidator> {
136+
impl BuildValidator for FunctionPlainValidator {
137+
const EXPECTED_TYPE: &'static str = "function-plain";
138+
139+
fn build(
140+
schema: &PyDict,
141+
config: Option<&PyDict>,
142+
_build_context: &mut BuildContext<CombinedValidator>,
143+
) -> PyResult<CombinedValidator> {
158144
let py = schema.py();
159145
let function = schema.get_as_req::<&PyAny>(intern!(py, "function"))?;
160146
Ok(Self {

src/validators/mod.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,16 @@ pub fn build_validator<'a>(
348348
let schema: &PyDict = schema.downcast()?;
349349
let type_: &str = schema.get_as_req(intern!(schema.py(), "type"))?;
350350
let val = match type_ {
351+
// The left hand side of this match is a 1:1 match with the `type` field we use as a discriminator
352+
// in our CoreSchema union
353+
// The right hand side is _generally_ a 1:1 match but there are cases where we use a `Builder`
354+
// on the right that may have internal logic to return different validators or just build a
355+
// a more complex validator (e.g. building a union of isinstance validators or something).
356+
// So to get from Python -> Rust implementation you should trace the `type` on the left hand side
357+
// of the match and then if the right hand side is a `{Type}Validator` you've found the implementation.
358+
// If the right hand side is a `{Type}Builder` you'll have to look into it's `build()` method to see
359+
// what it actually returns.
360+
// TODO: sort alphabetically? By left hand side string or right hand side type? Or RHS type's module filename?
351361
"any" => build_specific_validator::<any::AnyValidator>(schema, type_, config, build_context)?,
352362
"none" => build_specific_validator::<none::NoneValidator>(schema, type_, config, build_context)?,
353363
"bool" => build_specific_validator::<bool::BoolValidator>(schema, type_, config, build_context)?,
@@ -379,15 +389,17 @@ pub fn build_validator<'a>(
379389
"generator" => build_specific_validator::<generator::GeneratorValidator>(schema, type_, config, build_context)?,
380390
"dict" => build_specific_validator::<dict::DictValidator>(schema, type_, config, build_context)?,
381391
"function-plain" => {
382-
build_specific_validator::<function::FunctionBuilder>(schema, type_, config, build_context)?
392+
build_specific_validator::<function::FunctionPlainValidator>(schema, type_, config, build_context)?
383393
}
384394
"function-before" => {
385-
build_specific_validator::<function::FunctionBuilder>(schema, type_, config, build_context)?
395+
build_specific_validator::<function::FunctionBeforeValidator>(schema, type_, config, build_context)?
386396
}
387397
"function-after" => {
388-
build_specific_validator::<function::FunctionBuilder>(schema, type_, config, build_context)?
398+
build_specific_validator::<function::FunctionAfterValidator>(schema, type_, config, build_context)?
399+
}
400+
"function-wrap" => {
401+
build_specific_validator::<function::FunctionWrapValidator>(schema, type_, config, build_context)?
389402
}
390-
"function-wrap" => build_specific_validator::<function::FunctionBuilder>(schema, type_, config, build_context)?,
391403
"default" => {
392404
build_specific_validator::<with_default::WithDefaultValidator>(schema, type_, config, build_context)?
393405
}

0 commit comments

Comments
 (0)