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

feat: support trace api spec #2

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum SpecVersion {
struct RawSpecs {
main: &'static str,
write: &'static str,
trace: &'static str,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -162,6 +163,7 @@ fn main() {
raw_specs: RawSpecs {
main: include_str!("./specs/0.1.0/starknet_api_openrpc.json"),
write: include_str!("./specs/0.1.0/starknet_write_api.json"),
trace: include_str!("./specs/0.1.0/starknet_trace_api_openrpc.json"),
},
options: serde_json::from_str(include_str!("./profiles/0.1.0.json"))
.expect("Unable to parse profile options"),
Expand All @@ -171,6 +173,7 @@ fn main() {
raw_specs: RawSpecs {
main: include_str!("./specs/0.2.1/starknet_api_openrpc.json"),
write: include_str!("./specs/0.2.1/starknet_write_api.json"),
trace: include_str!("./specs/0.2.1/starknet_trace_api_openrpc.json"),
},
options: serde_json::from_str(include_str!("./profiles/0.2.1.json"))
.expect("Unable to parse profile options"),
Expand All @@ -180,6 +183,7 @@ fn main() {
raw_specs: RawSpecs {
main: include_str!("./specs/0.3.0/starknet_api_openrpc.json"),
write: include_str!("./specs/0.3.0/starknet_write_api.json"),
trace: include_str!("./specs/0.3.0/starknet_trace_api_openrpc.json"),
},
options: serde_json::from_str(include_str!("./profiles/0.3.0.json"))
.expect("Unable to parse profile options"),
Expand All @@ -189,6 +193,7 @@ fn main() {
raw_specs: RawSpecs {
main: include_str!("./specs/0.4.0/starknet_api_openrpc.json"),
write: include_str!("./specs/0.4.0/starknet_write_api.json"),
trace: include_str!("./specs/0.4.0/starknet_trace_api_openrpc.json"),
},
options: serde_json::from_str(include_str!("./profiles/0.4.0.json"))
.expect("Unable to parse profile options"),
Expand Down
33 changes: 32 additions & 1 deletion src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ pub struct Specification {
pub components: Components,
}

impl Specification {
pub fn merge(&mut self, other: &mut Self) {
self.methods.append(&mut other.methods);

other.components.schemas.iter().for_each(|(key, value)| {
if let indexmap::map::Entry::Vacant(entry) =
self.components.schemas.entry(key.to_owned())
{
entry.insert(value.to_owned());
}
});

other.components.errors.iter().for_each(|(key, value)| {
if let indexmap::map::Entry::Vacant(entry) =
self.components.errors.entry(key.to_owned())
{
entry.insert(value.to_owned());
}
});
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Info {
Expand Down Expand Up @@ -54,7 +76,8 @@ pub struct Param {
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub summary: Option<String>,
pub required: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub required: Option<bool>,
pub schema: Schema,
}

Expand All @@ -76,6 +99,7 @@ pub enum Schema {
OneOf(OneOf),
AllOf(AllOf),
Primitive(Primitive),
Schema { schema: Box<Schema> },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -155,6 +179,8 @@ pub struct IntegerPrimitive {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct ObjectPrimitive {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -169,6 +195,8 @@ pub struct ObjectPrimitive {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct StringPrimitive {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(rename = "$comment")]
Expand Down Expand Up @@ -205,6 +233,7 @@ impl Schema {
Self::OneOf(schema) => schema.title.as_ref(),
Self::AllOf(schema) => schema.title.as_ref(),
Self::Primitive(schema) => schema.title(),
Self::Schema { schema } => schema.title(),
}
}

Expand All @@ -214,6 +243,7 @@ impl Schema {
Self::OneOf(schema) => schema.description.as_ref(),
Self::AllOf(schema) => schema.description.as_ref(),
Self::Primitive(schema) => schema.description(),
Self::Schema { schema } => schema.description(),
}
}

Expand All @@ -223,6 +253,7 @@ impl Schema {
Self::OneOf(_) => None,
Self::AllOf(_) => None,
Self::Primitive(schema) => schema.summary(),
Self::Schema { schema } => schema.summary(),
}
}
}
Expand Down
24 changes: 11 additions & 13 deletions src/subcommands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,12 @@ impl Generate {
// does not provide additional models).
let mut write_specs: Specification =
serde_json::from_str(profile.raw_specs.write).expect("Failed to parse specification");
specs.methods.append(&mut write_specs.methods);
write_specs
.components
.errors
.iter()
.for_each(|(key, value)| {
if let indexmap::map::Entry::Vacant(entry) =
specs.components.errors.entry(key.to_owned())
{
entry.insert(value.to_owned());
}
});

let mut trace_specs: Specification =
serde_json::from_str(profile.raw_specs.trace).expect("Failed to parse specification");

specs.merge(&mut write_specs);
specs.merge(&mut trace_specs);

println!("// AUTO-GENERATED CODE. DO NOT EDIT");
println!("// To change the code generated, modify the codegen tool instead:");
Expand Down Expand Up @@ -1042,7 +1036,7 @@ fn resolve_types(
request_fields.push(RustField {
description: param.description.clone(),
name: param.name.clone(),
optional: !param.required,
optional: !param.required.unwrap_or_default(),
fixed: None,
arc_wrap: false,
type_name: field_type.type_name,
Expand Down Expand Up @@ -1358,6 +1352,9 @@ fn get_schema_fields(
});
}
}
Schema::Schema { schema } => {
return get_schema_fields(schema, specs, fields, flatten_option);
}
_ => {
dbg!(schema);
anyhow::bail!("Unexpected schema type when getting object fields");
Expand Down Expand Up @@ -1434,6 +1431,7 @@ fn get_rust_type_for_field(schema: &Schema, specs: &Specification) -> Result<Rus
})
}
},
Schema::Schema { schema } => get_rust_type_for_field(schema, specs),
}
}

Expand Down