Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ applied. Non-required properties with types that already have a default value
(such as a `Vec<T>`) simply get the `#[serde(default)]` attribute (so you won't
see e.g. `Option<Vec<T>>`).

By default, properties are ordered by their original name.
The feature `preserve_order` may be used to keep the order as set in the given schema.
To ensure deserializing a schema using `serde_json` does not change the order of properties,
set the `preserve_order` feature of `serde_json`.

#### Alternate Map types

By default, Typify uses `std::collections::HashMap` as described above.
Expand Down
3 changes: 3 additions & 0 deletions typify-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ description = "typify backend implementation"
repository = "https://github.com/oxidecomputer/typify"
readme = "../README.md"

[features]
preserve_order = []

[dependencies]
heck = { workspace = true }
log = { workspace = true }
Expand Down
15 changes: 14 additions & 1 deletion typify-impl/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,20 @@ impl TypeSpace {
.collect::<Result<Vec<_>>>()?;

// Sort parameters by name to ensure a deterministic result.
properties.sort_by(|a, b| a.name.cmp(&b.name));
#[cfg(not(feature = "preserve_order"))]
properties.sort_by(|a, b| {
let a_name = match &a.rename {
StructPropertyRename::Rename(a_rename) => a_rename,
_ => &a.name,
};

let b_name = match &b.rename {
StructPropertyRename::Rename(b_rename) => b_rename,
_ => &b.name,
};

a_name.cmp(b_name)
});

// If there are additional properties tack them on, flattened, at the
// end. Note that a `None` value for additional_properties is
Expand Down
56 changes: 28 additions & 28 deletions typify-impl/tests/github.out
Original file line number Diff line number Diff line change
Expand Up @@ -26891,6 +26891,8 @@ impl ::std::convert::From<::chrono::DateTime<::chrono::offset::Utc>>
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequest {
#[serde(rename = "_links")]
pub links: PullRequestLinks,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub active_lock_reason: ::std::option::Option<PullRequestActiveLockReason>,
pub additions: i64,
Expand Down Expand Up @@ -26919,8 +26921,6 @@ pub struct PullRequest {
pub id: i64,
pub issue_url: ::std::string::String,
pub labels: ::std::vec::Vec<Label>,
#[serde(rename = "_links")]
pub links: PullRequestLinks,
pub locked: bool,
#[doc = "Indicates whether maintainers can modify the pull request."]
pub maintainer_can_modify: bool,
Expand Down Expand Up @@ -27278,6 +27278,8 @@ impl ::std::convert::TryFrom<::std::string::String> for PullRequestClosedAction
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestClosedPullRequest {
#[serde(rename = "_links")]
pub links: PullRequestClosedPullRequestLinks,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub active_lock_reason: ::std::option::Option<PullRequestClosedPullRequestActiveLockReason>,
pub additions: i64,
Expand Down Expand Up @@ -27305,8 +27307,6 @@ pub struct PullRequestClosedPullRequest {
pub id: i64,
pub issue_url: ::std::string::String,
pub labels: ::std::vec::Vec<Label>,
#[serde(rename = "_links")]
pub links: PullRequestClosedPullRequestLinks,
pub locked: bool,
#[doc = "Indicates whether maintainers can modify the pull request."]
pub maintainer_can_modify: bool,
Expand Down Expand Up @@ -27568,6 +27568,8 @@ impl ::std::convert::TryFrom<::std::string::String> for PullRequestConvertedToDr
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestConvertedToDraftPullRequest {
#[serde(rename = "_links")]
pub links: PullRequestConvertedToDraftPullRequestLinks,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub active_lock_reason:
::std::option::Option<PullRequestConvertedToDraftPullRequestActiveLockReason>,
Expand Down Expand Up @@ -27595,8 +27597,6 @@ pub struct PullRequestConvertedToDraftPullRequest {
pub id: i64,
pub issue_url: ::std::string::String,
pub labels: ::std::vec::Vec<Label>,
#[serde(rename = "_links")]
pub links: PullRequestConvertedToDraftPullRequestLinks,
pub locked: bool,
#[doc = "Indicates whether maintainers can modify the pull request."]
pub maintainer_can_modify: bool,
Expand Down Expand Up @@ -28207,6 +28207,8 @@ impl ::std::convert::TryFrom<::std::string::String> for PullRequestOpenedAction
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestOpenedPullRequest {
#[serde(rename = "_links")]
pub links: PullRequestOpenedPullRequestLinks,
pub active_lock_reason: PullRequestOpenedPullRequestActiveLockReason,
pub additions: i64,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
Expand All @@ -28233,8 +28235,6 @@ pub struct PullRequestOpenedPullRequest {
pub id: i64,
pub issue_url: ::std::string::String,
pub labels: ::std::vec::Vec<Label>,
#[serde(rename = "_links")]
pub links: PullRequestOpenedPullRequestLinks,
pub locked: bool,
#[doc = "Indicates whether maintainers can modify the pull request."]
pub maintainer_can_modify: bool,
Expand Down Expand Up @@ -28467,6 +28467,8 @@ impl ::std::convert::TryFrom<::std::string::String> for PullRequestReadyForRevie
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestReadyForReviewPullRequest {
#[serde(rename = "_links")]
pub links: PullRequestReadyForReviewPullRequestLinks,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub active_lock_reason:
::std::option::Option<PullRequestReadyForReviewPullRequestActiveLockReason>,
Expand Down Expand Up @@ -28494,8 +28496,6 @@ pub struct PullRequestReadyForReviewPullRequest {
pub id: i64,
pub issue_url: ::std::string::String,
pub labels: ::std::vec::Vec<Label>,
#[serde(rename = "_links")]
pub links: PullRequestReadyForReviewPullRequestLinks,
pub locked: bool,
#[doc = "Indicates whether maintainers can modify the pull request."]
pub maintainer_can_modify: bool,
Expand Down Expand Up @@ -28756,6 +28756,8 @@ impl ::std::convert::TryFrom<::std::string::String> for PullRequestReopenedActio
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestReopenedPullRequest {
#[serde(rename = "_links")]
pub links: PullRequestReopenedPullRequestLinks,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub active_lock_reason: ::std::option::Option<PullRequestReopenedPullRequestActiveLockReason>,
pub additions: i64,
Expand Down Expand Up @@ -28783,8 +28785,6 @@ pub struct PullRequestReopenedPullRequest {
pub id: i64,
pub issue_url: ::std::string::String,
pub labels: ::std::vec::Vec<Label>,
#[serde(rename = "_links")]
pub links: PullRequestReopenedPullRequestLinks,
pub locked: bool,
#[doc = "Indicates whether maintainers can modify the pull request."]
pub maintainer_can_modify: bool,
Expand Down Expand Up @@ -28998,6 +28998,8 @@ impl ::std::convert::From<Team> for PullRequestRequestedReviewersItem {
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestReviewComment {
#[serde(rename = "_links")]
pub links: PullRequestReviewCommentLinks,
pub author_association: AuthorAssociation,
#[doc = "The text of the comment."]
pub body: ::std::string::String,
Expand All @@ -29016,8 +29018,6 @@ pub struct PullRequestReviewComment {
#[doc = "The line of the blob to which the comment applies. The last line of the range for a multi-line comment"]
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub line: ::std::option::Option<i64>,
#[serde(rename = "_links")]
pub links: PullRequestReviewCommentLinks,
#[doc = "The node ID of the pull request review comment."]
pub node_id: ::std::string::String,
#[doc = "The SHA of the original commit to which the comment applies."]
Expand Down Expand Up @@ -29116,6 +29116,8 @@ impl ::std::convert::TryFrom<::std::string::String> for PullRequestReviewComment
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestReviewCommentCreatedPullRequest {
#[serde(rename = "_links")]
pub links: PullRequestReviewCommentCreatedPullRequestLinks,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub active_lock_reason:
::std::option::Option<PullRequestReviewCommentCreatedPullRequestActiveLockReason>,
Expand All @@ -29140,8 +29142,6 @@ pub struct PullRequestReviewCommentCreatedPullRequest {
pub id: i64,
pub issue_url: ::std::string::String,
pub labels: ::std::vec::Vec<Label>,
#[serde(rename = "_links")]
pub links: PullRequestReviewCommentCreatedPullRequestLinks,
pub locked: bool,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub merge_commit_sha: ::std::option::Option<::std::string::String>,
Expand Down Expand Up @@ -29400,6 +29400,8 @@ impl ::std::convert::TryFrom<::std::string::String> for PullRequestReviewComment
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestReviewCommentDeletedPullRequest {
#[serde(rename = "_links")]
pub links: PullRequestReviewCommentDeletedPullRequestLinks,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub active_lock_reason:
::std::option::Option<PullRequestReviewCommentDeletedPullRequestActiveLockReason>,
Expand All @@ -29424,8 +29426,6 @@ pub struct PullRequestReviewCommentDeletedPullRequest {
pub id: i64,
pub issue_url: ::std::string::String,
pub labels: ::std::vec::Vec<Label>,
#[serde(rename = "_links")]
pub links: PullRequestReviewCommentDeletedPullRequestLinks,
pub locked: bool,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub merge_commit_sha: ::std::option::Option<::std::string::String>,
Expand Down Expand Up @@ -29699,6 +29699,8 @@ pub struct PullRequestReviewCommentEditedChangesBody {
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestReviewCommentEditedPullRequest {
#[serde(rename = "_links")]
pub links: PullRequestReviewCommentEditedPullRequestLinks,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub active_lock_reason:
::std::option::Option<PullRequestReviewCommentEditedPullRequestActiveLockReason>,
Expand All @@ -29723,8 +29725,6 @@ pub struct PullRequestReviewCommentEditedPullRequest {
pub id: i64,
pub issue_url: ::std::string::String,
pub labels: ::std::vec::Vec<Label>,
#[serde(rename = "_links")]
pub links: PullRequestReviewCommentEditedPullRequestLinks,
pub locked: bool,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub merge_commit_sha: ::std::option::Option<::std::string::String>,
Expand Down Expand Up @@ -30117,6 +30117,8 @@ impl ::std::convert::TryFrom<::std::string::String> for PullRequestReviewDismiss
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestReviewDismissedReview {
#[serde(rename = "_links")]
pub links: PullRequestReviewDismissedReviewLinks,
pub author_association: AuthorAssociation,
#[doc = "The text of the review."]
#[serde(deserialize_with = "::std::option::Option::deserialize")]
Expand All @@ -30126,8 +30128,6 @@ pub struct PullRequestReviewDismissedReview {
pub html_url: ::std::string::String,
#[doc = "Unique identifier of the review"]
pub id: i64,
#[serde(rename = "_links")]
pub links: PullRequestReviewDismissedReviewLinks,
pub node_id: ::std::string::String,
pub pull_request_url: ::std::string::String,
pub state: PullRequestReviewDismissedReviewState,
Expand Down Expand Up @@ -30268,6 +30268,8 @@ pub struct PullRequestReviewEditedChangesBody {
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestReviewEditedReview {
#[serde(rename = "_links")]
pub links: PullRequestReviewEditedReviewLinks,
pub author_association: AuthorAssociation,
#[doc = "The text of the review."]
#[serde(deserialize_with = "::std::option::Option::deserialize")]
Expand All @@ -30277,8 +30279,6 @@ pub struct PullRequestReviewEditedReview {
pub html_url: ::std::string::String,
#[doc = "Unique identifier of the review"]
pub id: i64,
#[serde(rename = "_links")]
pub links: PullRequestReviewEditedReviewLinks,
pub node_id: ::std::string::String,
pub pull_request_url: ::std::string::String,
pub state: ::std::string::String,
Expand Down Expand Up @@ -30634,6 +30634,8 @@ impl ::std::convert::TryFrom<::std::string::String> for PullRequestReviewSubmitt
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PullRequestReviewSubmittedReview {
#[serde(rename = "_links")]
pub links: PullRequestReviewSubmittedReviewLinks,
pub author_association: AuthorAssociation,
#[doc = "The text of the review."]
#[serde(deserialize_with = "::std::option::Option::deserialize")]
Expand All @@ -30643,8 +30645,6 @@ pub struct PullRequestReviewSubmittedReview {
pub html_url: ::std::string::String,
#[doc = "Unique identifier of the review"]
pub id: i64,
#[serde(rename = "_links")]
pub links: PullRequestReviewSubmittedReviewLinks,
pub node_id: ::std::string::String,
pub pull_request_url: ::std::string::String,
pub state: ::std::string::String,
Expand Down Expand Up @@ -34669,6 +34669,8 @@ pub struct SecurityAdvisoryWithdrawnSecurityAdvisoryVulnerabilitiesItemPackage {
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct SimplePullRequest {
#[serde(rename = "_links")]
pub links: SimplePullRequestLinks,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub active_lock_reason: ::std::option::Option<SimplePullRequestActiveLockReason>,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
Expand All @@ -34690,8 +34692,6 @@ pub struct SimplePullRequest {
pub id: i64,
pub issue_url: ::std::string::String,
pub labels: ::std::vec::Vec<Label>,
#[serde(rename = "_links")]
pub links: SimplePullRequestLinks,
pub locked: bool,
#[serde(deserialize_with = "::std::option::Option::deserialize")]
pub merge_commit_sha: ::std::option::Option<::std::string::String>,
Expand Down
10 changes: 5 additions & 5 deletions typify-impl/tests/vega.out
Original file line number Diff line number Diff line change
Expand Up @@ -11205,6 +11205,11 @@ impl<'de> ::serde::Deserialize<'de> for EncodeKey {
#[doc = "`Everything`"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug, Default)]
pub struct Everything {
#[serde(
rename = "$schema",
skip_serializing_if = "::std::option::Option::is_none"
)]
pub schema: ::std::option::Option<::std::string::String>,
#[serde(skip_serializing_if = "::std::option::Option::is_none")]
pub autosize: ::std::option::Option<Autosize>,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
Expand Down Expand Up @@ -11233,11 +11238,6 @@ pub struct Everything {
pub projections: ::std::vec::Vec<Projection>,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub scales: ::std::vec::Vec<Scale>,
#[serde(
rename = "$schema",
skip_serializing_if = "::std::option::Option::is_none"
)]
pub schema: ::std::option::Option<::std::string::String>,
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub signals: ::std::vec::Vec<Signal>,
#[serde(skip_serializing_if = "::std::option::Option::is_none")]
Expand Down
1 change: 1 addition & 0 deletions typify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ categories = ["api-bindings", "compilers"]
[features]
default = ["macro"]
macro = ["typify-macro"]
preserve_order = ["typify-impl/preserve_order", "serde_json/preserve_order"]

[dependencies]
typify-macro = { workspace = true, optional = true }
Expand Down