Skip to content

Commit

Permalink
Serialize TypeOwner::None to JSON null (#1207)
Browse files Browse the repository at this point in the history
* wit-parser: serialize TypeOwner::None to JSON null

* wit-parser: cargo fmt

* wit-parser: run BLESS=1 to regenerate test results
  • Loading branch information
ydnar authored Sep 18, 2023
1 parent d9f5dcf commit ea8d3f2
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 42 deletions.
4 changes: 3 additions & 1 deletion crates/wit-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ mod live;
pub use live::LiveTypes;
mod serde_;
use serde_::{
serialize_anon_result, serialize_id, serialize_id_map, serialize_optional_id, serialize_params,
serialize_anon_result, serialize_id, serialize_id_map, serialize_none, serialize_optional_id,
serialize_params,
};

/// Checks if the given string is a legal identifier in wit.
Expand Down Expand Up @@ -427,6 +428,7 @@ pub enum TypeOwner {
Interface(InterfaceId),
/// This type wasn't inherently defined anywhere, such as a `list<T>`, which
/// doesn't need an owner.
#[serde(untagged, serialize_with = "serialize_none")]
None,
}

Expand Down
7 changes: 7 additions & 0 deletions crates/wit-parser/src/serde_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ use indexmap::IndexMap;
use serde::ser::{SerializeMap, SerializeSeq, Serializer};
use serde::Serialize;

pub fn serialize_none<S>(serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_none()
}

pub fn serialize_arena<T, S>(arena: &Arena<T>, serializer: S) -> Result<S::Ok, S::Error>
where
T: Serialize,
Expand Down
6 changes: 3 additions & 3 deletions crates/wit-parser/tests/ui/functions.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@
]
}
},
"owner": "none"
"owner": null
},
{
"name": null,
"kind": {
"option": "u32"
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -151,7 +151,7 @@
"err": "float32"
}
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-parser/tests/ui/random.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"kind": {
"list": "u8"
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
4 changes: 2 additions & 2 deletions crates/wit-parser/tests/ui/resources-empty.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"borrow": 0
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -57,7 +57,7 @@
"own": 0
}
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"borrow": 0
}
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"borrow": 0
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -68,7 +68,7 @@
"own": 0
}
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
10 changes: 5 additions & 5 deletions crates/wit-parser/tests/ui/resources-multiple.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
"borrow": 0
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -240,14 +240,14 @@
]
}
},
"owner": "none"
"owner": null
},
{
"name": null,
"kind": {
"option": "u32"
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -257,7 +257,7 @@
"err": "float32"
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -266,7 +266,7 @@
"own": 0
}
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"borrow": 0
}
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
4 changes: 2 additions & 2 deletions crates/wit-parser/tests/ui/resources-return-own.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"borrow": 0
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -63,7 +63,7 @@
"own": 0
}
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
14 changes: 7 additions & 7 deletions crates/wit-parser/tests/ui/resources.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
"borrow": 3
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -218,7 +218,7 @@
"borrow": 4
}
},
"owner": "none"
"owner": null
},
{
"name": "a",
Expand Down Expand Up @@ -286,7 +286,7 @@
"own": 1
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -295,7 +295,7 @@
"own": 2
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -304,7 +304,7 @@
"own": 3
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -313,7 +313,7 @@
"own": 4
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -322,7 +322,7 @@
"own": 13
}
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
4 changes: 2 additions & 2 deletions crates/wit-parser/tests/ui/resources1.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"borrow": 0
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -81,7 +81,7 @@
"own": 0
}
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
4 changes: 2 additions & 2 deletions crates/wit-parser/tests/ui/shared-types.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"kind": {
"list": "u8"
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -68,7 +68,7 @@
]
}
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
6 changes: 3 additions & 3 deletions crates/wit-parser/tests/ui/types.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@
"kind": {
"option": "u32"
},
"owner": "none"
"owner": null
},
{
"name": "t37",
Expand Down Expand Up @@ -618,14 +618,14 @@
"kind": {
"list": 31
},
"owner": "none"
"owner": null
},
{
"name": null,
"kind": {
"list": 42
},
"owner": "none"
"owner": null
},
{
"name": "t45",
Expand Down
6 changes: 3 additions & 3 deletions crates/wit-parser/tests/ui/world-top-level-funcs.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@
"kind": {
"list": "u32"
},
"owner": "none"
"owner": null
},
{
"name": null,
"kind": {
"option": "u32"
},
"owner": "none"
"owner": null
},
{
"name": null,
"kind": {
"list": 1
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down
14 changes: 7 additions & 7 deletions crates/wit-parser/tests/ui/world-top-level-resources.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@
"borrow": 0
}
},
"owner": "none"
"owner": null
},
{
"name": null,
"kind": {
"list": "u32"
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -168,7 +168,7 @@
"borrow": 1
}
},
"owner": "none"
"owner": null
},
{
"name": "request",
Expand All @@ -195,7 +195,7 @@
"borrow": 5
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -204,7 +204,7 @@
"borrow": 6
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -213,7 +213,7 @@
"own": 5
}
},
"owner": "none"
"owner": null
},
{
"name": null,
Expand All @@ -222,7 +222,7 @@
"own": 6
}
},
"owner": "none"
"owner": null
}
],
"packages": [
Expand Down

0 comments on commit ea8d3f2

Please sign in to comment.