Skip to content

Commit

Permalink
add support for additional props / HashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Sep 15, 2024
1 parent 4fd8ed6 commit 7f7f305
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
20 changes: 17 additions & 3 deletions core/src/extractor/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::collections::{BTreeMap, HashSet};
use convert_case::{Case, Casing};
/// Records are the "model"s of the MIR world. model is a crazy overloaded word though.
use openapiv3::{
ObjectType, OpenAPI, ReferenceOr, RefOr, RefOrMap, Schema, SchemaData, SchemaKind,
SchemaReference, StringType, Type,
AdditionalProperties, ObjectType, OpenAPI, RefOr, RefOrMap, ReferenceOr, Schema, SchemaData,
SchemaKind, SchemaReference, StringType, Type,
};

use hir::{HirField, HirSpec, NewType, Record, StrEnum, Struct};
Expand Down Expand Up @@ -88,7 +88,21 @@ pub fn extract_schema(
let name = name.to_string();

let k = &schema.kind;
if let SchemaKind::Type(Type::Object(ObjectType { properties, .. })) = k {
if let SchemaKind::Type(Type::Object(ObjectType {
properties,
additional_properties,
..
})) = k
{
if properties.is_empty() && additional_properties.is_some() {
let p = additional_properties.as_ref().unwrap();
return match p {
AdditionalProperties::Any(_) => Some(Ty::HashMap(Box::new(Ty::Any(None)))),
AdditionalProperties::Schema(s) => {
Some(Ty::HashMap(Box::new(schema_ref_to_ty(s, spec))))
}
};
}
let fields = extract_fields(properties, schema, spec, hir);
let s = Struct {
name: name.clone(),
Expand Down
7 changes: 4 additions & 3 deletions libninja/src/rust/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use proc_macro2::TokenStream;
use quote::{quote, ToTokens};

use hir::{
AuthLocation, AuthStrategy, HirSpec, Language, Operation, qualified_env_var
, ServerStrategy,
qualified_env_var, AuthLocation, AuthStrategy, HirSpec, Language, Operation, ServerStrategy,
};
use ln_core::PackageConfig;
use mir::{Class, Field, Visibility};
Expand Down Expand Up @@ -139,7 +138,9 @@ pub fn struct_Client(spec: &HirSpec, opt: &PackageConfig) -> Class<TokenStream>
..Function::default()
});
}
class_methods.push(build_Client_new_with(spec, opt));
if spec.has_security() {
class_methods.push(build_Client_new_with(spec, opt));
}
Class {
name: opt.client_name().to_rust_struct(),
instance_fields,
Expand Down
5 changes: 3 additions & 2 deletions libninja/src/rust/codegen/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use ln_macro::rfunction;
use mir::{File, Import, Ty};
use mir_rust::format_code;

use crate::PackageConfig;
use crate::rust::codegen::{ToRustCode, ToRustType};
use crate::rust::codegen::ToRustIdent;
use crate::rust::codegen::{ToRustCode, ToRustType};
use crate::PackageConfig;

pub trait ToRustExample {
fn to_rust_example(&self, spec: &HirSpec) -> anyhow::Result<TokenStream>;
Expand Down Expand Up @@ -184,6 +184,7 @@ pub fn to_rust_example_value(
Ty::Date { .. } => quote!(chrono::Utc::now().date_naive()),
Ty::DateTime { .. } => quote!(chrono::Utc::now()),
Ty::Currency { .. } => quote!(rust_decimal_macros::dec!(100.01)),
Ty::HashMap(_) => quote!(std::collections::HashMap::new()),
};
Ok(s)
}
10 changes: 10 additions & 0 deletions libninja/src/rust/codegen/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ impl ToRustType for Ty {
Ty::Date { .. } => quote!(chrono::NaiveDate),
Ty::DateTime { .. } => quote!(chrono::DateTime<chrono::Utc>),
Ty::Currency { .. } => quote!(rust_decimal::Decimal),
Ty::HashMap(inner) => {
let inner = inner.to_rust_type();
quote!(std::collections::HashMap<String, #inner>)
}
}
}

Expand All @@ -56,6 +60,10 @@ impl ToRustType for Ty {
Ty::Date { .. } => quote!(chrono::NaiveDate),
Ty::DateTime { .. } => quote!(chrono::DateTime<chrono::Utc>),
Ty::Currency { .. } => quote!(rust_decimal::Decimal),
Ty::HashMap(inner) => {
let inner = inner.to_rust_type();
quote!(std::collections::HashMap<String, #inner>)
}
}
}

Expand Down Expand Up @@ -84,6 +92,7 @@ impl ToRustType for Ty {
Ty::Date { .. } => true,
Ty::DateTime => true,
Ty::Currency { .. } => true,
Ty::HashMap(_) => true,
}
}

Expand All @@ -103,6 +112,7 @@ impl ToRustType for Ty {
Ty::Date { .. } => true,
Ty::DateTime => true,
Ty::Currency { .. } => true,
Ty::HashMap(inner) => inner.implements_dummy(spec),
}
}
}
3 changes: 3 additions & 0 deletions mir/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum Ty {
Float,
Boolean,
Array(Box<Ty>),
HashMap(Box<Ty>),
// OpenAPI name for the model. Hasn't been converted to a language type (e.g. cased, sanitized)
Model(String),
Unit,
Expand All @@ -51,6 +52,7 @@ impl Ty {
match self {
Ty::Model(name) => Some(name),
Ty::Array(ty) => ty.inner_model(),
Ty::HashMap(ty) => ty.inner_model(),
_ => None,
}
}
Expand All @@ -73,6 +75,7 @@ impl Ty {
Ty::Float => true,
Ty::Boolean => true,
Ty::Array(_) => false,
Ty::HashMap(_) => false,
Ty::Model(_) => false,
Ty::Any(_) => false,
Ty::Unit => true,
Expand Down

0 comments on commit 7f7f305

Please sign in to comment.