Skip to content
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

medschool: validate json snippets in config files #2630

Conversation

infiniteregrets
Copy link
Contributor

closes #2525

@infiniteregrets infiniteregrets marked this pull request as draft August 5, 2024 05:50
@infiniteregrets infiniteregrets marked this pull request as ready for review August 11, 2024 03:35
Copy link
Member

@meowjesty meowjesty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR my bhai.

It's missing docs, overall looks fine, but I wonder why the changes to remove iterator stuff?

Comment on lines +29 to +30
jsonschema = "0.18.0"
serde_json = "1.0.122"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
jsonschema = "0.18.0"
serde_json = "1.0.122"
jsonschema = "0.18"
serde_json = "1.0"

We usually just use major.min everywhere else right?

Comment on lines +104 to +113
let schema = if let Some(schema) = schema {
let schema = fs::read_to_string(schema)?;
let schema: Value =
serde_json::from_str(&schema).map_err(|e| DocsError::Json(e, schema))?;
let compiled_schema =
JSONSchema::compile(&schema).map_err(|e| DocsError::JsonSchema(e.to_string()))?;
Some(compiled_schema)
} else {
None
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if let Some {} else None is just .map.

@@ -200,43 +203,53 @@ pub fn parse_docs_into_set<'a>(
/// DFS helper function to resolve the references of the types. Returns the docs of the fields of
/// the field we're currently looking at search till its leaf nodes. The leaf here means a primitive
/// type for which we don't have a [`PartialType`].
#[tracing::instrument(level = "trace", ret)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[tracing::instrument(level = "trace", ret)]
#[tracing::instrument(level = Level::TRACE, ret)]

For some reason we started doing it like this now.

@@ -279,48 +292,59 @@ fn dfs_fields<'a, const MAX_RECURSION_LEVEL: usize>(
/// be where we resolve all references and return the same HashSet and let the caller
/// decide what the root should be.
#[tracing::instrument(level = "trace", ret)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[tracing::instrument(level = "trace", ret)]
#[tracing::instrument(level = Level::TRACE, ret)]

Also change it here, since you're touching this function.

types: HashSet<PartialType>,
schema: Option<JSONSchema>,
) -> Result<Option<PartialType>, DocsError> {
// Maximum recursion level for safety.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Maximum recursion level for safety.
/// Maximum recursion level for safety.

.inspect_err(|e| error!(error = ?e, context=?field, "Error validating JSON blocks"))?;
}
// get the type of the field from the types set to recurse into it's fields
match types.get(&field.ty) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to change this so much just due to the function returning a Result now?

The logic inside this match looks very similar to the iterator (at a glance at least).

const MAX_RECURSION_LEVEL: usize = 10;
// Cache to perform memoization between recursive calls so we don't have to resolve the same
// type multiple times. Mapping between `ident` -> `resolved_docs`.
// For example, if we have a types [`A`, `B`, `C`] and A has a field of type `B` and `B` has a
// field of type `C`, and `C` has already been resolved, we don't want to resolve `C` again
// as we iterate over the types. A -> (B -> C), (B -> C), (C)
let mut cache = HashMap::with_capacity(types.len());
let mut max_area = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here, the logic looks pretty much the same, except it's more Result-y now? You could use try_collect for example.

@infiniteregrets
Copy link
Contributor Author

Thanks for the PR my bhai.

It's missing docs, overall looks fine, but I wonder why the changes to remove iterator stuff?

of course bhai, the whole closure/functional thing made the code a mess to be honest, its much easier and clear to read it with a for loop

@meowjesty
Copy link
Member

made the code a mess to be honest

Aw, I thought it would be just a matter of changing some collect to try_collect, and maybe some options to results before going into and_then. I asked for this because the diff felt like a bit too much, when the change is really just validate_json_blocks(&type_.docs, schema).

@infiniteregrets
Copy link
Contributor Author

Aw, I thought it would be just a matter of changing some collect to try_collect, and maybe some options to results before going into and_then. I asked for this because the diff felt like a bit too much, when the change is really just validate_json_blocks(&type_.docs, schema).

hi, coming back to this, id like to keep the loop there just to make it more readable! will address the comments! sorry for the delay

@meowjesty
Copy link
Member

id like to keep the loop

Alright! I'm not a loop person, but it was more of a suggestion due to how the bigger the diff got than what I expected.

sorry for the delay

No worries bhai! After your previous fix the tool is already working way better.

@Razz4780
Copy link
Contributor

Hey @infiniteregrets, is there any work in progress on this one?

@Razz4780
Copy link
Contributor

Razz4780 commented Dec 9, 2024

Seems like the PR is dead ☠️

@Razz4780 Razz4780 closed this Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validate JSON snippets in configuration.md (or directly in the rust docs)
3 participants