Skip to content
Merged
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 json_typegen_shared/src/generation/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fn type_from_shape(ctxt: &mut Ctxt, path: &str, shape: &Shape) -> Value {
MapT { val_type: v } => generate_map_type(ctxt, path, v),
Opaque(t) => Value::Object(string_hashmap! { "type" => Value::String(t.clone()) }),
Optional(e) => type_from_shape(ctxt, path, e),
Nullable(e) => type_from_shape(ctxt, path, e),
}
}

Expand Down
8 changes: 8 additions & 0 deletions json_typegen_shared/src/generation/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ fn type_from_shape(ctxt: &mut Ctxt, path: &str, shape: &Shape) -> (Ident, Option
} else {
(format!("{}?", inner), defs)
}
},
Nullable(e) => {
let (inner, defs) = type_from_shape(ctxt, path, e);
if ctxt.options.use_default_for_missing_fields {
(inner, defs)
} else {
(format!("{}?", inner), defs)
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions json_typegen_shared/src/generation/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ fn type_from_shape(ctxt: &mut Ctxt, path: &str, shape: &Shape) -> (Ident, Option
let optional = import(ctxt, Import::Optional);
(format!("{}[{}]", optional, inner), defs)
}
},
Nullable(e) => {
let (inner, defs) = type_from_shape(ctxt, path, e);
if ctxt.options.use_default_for_missing_fields {
(inner, defs)
} else {
let optional = import(ctxt, Import::Optional);
(format!("{}[{}]", optional, inner), defs)
}
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion json_typegen_shared/src/generation/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ fn type_from_shape(ctxt: &mut Ctxt, path: &str, shape: &Shape) -> (Ident, Option
} else {
(format!("Option<{}>", inner), defs)
}
}
},
Nullable(e) => {
let (inner, defs) = type_from_shape(ctxt, path, e);
if ctxt.options.use_default_for_missing_fields {
(inner, defs)
} else {
(format!("Option<{}>", inner), defs)
}
},
}
}

Expand Down
4 changes: 4 additions & 0 deletions json_typegen_shared/src/generation/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ fn type_from_shape(ctxt: &mut Ctxt, shape: &Shape) -> Value {
"__type__" => Value::Str("optional"),
"item" => type_from_shape(ctxt, e),
}),
Nullable(e) => Value::Object(string_hashmap! {
"__type__" => Value::Str("nullable"),
"item" => type_from_shape(ctxt, e),
}),
}
}

Expand Down
4 changes: 4 additions & 0 deletions json_typegen_shared/src/generation/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ fn type_from_shape(ctxt: &mut Ctxt, path: &str, shape: &Shape) -> (Ident, Option
} else {
(format!("{} | undefined", inner), defs)
}
},
Nullable(e) => {
let (inner, defs) = type_from_shape(ctxt, path, e);
(format!("{} | null", inner), defs)
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion json_typegen_shared/src/generation/typescript_type_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ fn type_from_shape(ctxt: &mut Ctxt, shape: &Shape) -> Code {
} else {
format!("{} | undefined", inner)
}
}
},
Nullable(e) => {
let inner = type_from_shape(ctxt, e);
if ctxt.options.use_default_for_missing_fields {
inner
} else {
format!("{} | null", inner)
}
},
}
}

Expand Down
19 changes: 15 additions & 4 deletions json_typegen_shared/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ pub enum Shape {
/// represented by any single shape
Any,

/// `Optional(T)` represents that a value is nullable, or not always present
/// `Optional(T)` represents that a value is not always present
Optional(Box<Shape>),

/// `Nullable(T)` represents that a value is nullable
Nullable(Box<Shape>),
/// Equivalent to `Optional(Bottom)`, `Null` represents optionality with no further information
Null,

Expand Down Expand Up @@ -50,7 +51,7 @@ pub fn common_shape(a: Shape, b: Shape) -> Shape {
match (a, b) {
(a, Bottom) | (Bottom, a) => a,
(Integer, Floating) | (Floating, Integer) => Floating,
(a, Null) | (Null, a) => a.into_optional(),
(a, Null) | (Null, a) => a.into_nullable(),
(a, Optional(b)) | (Optional(b), a) => common_shape(a, *b).into_optional(),
(Tuple(shapes1, n1), Tuple(shapes2, n2)) => {
if shapes1.len() == shapes2.len() {
Expand Down Expand Up @@ -81,6 +82,7 @@ pub fn common_shape(a: Shape, b: Shape) -> Shape {
fields: common_field_shapes(f1, f2),
},
(Opaque(t), _) | (_, Opaque(t)) => Opaque(t),
(a, Nullable(b)) | (Nullable(b), a) => common_shape(a, *b).into_nullable(),
_ => Any,
}
}
Expand Down Expand Up @@ -113,10 +115,19 @@ impl Shape {
fn into_optional(self) -> Self {
use self::Shape::*;
match self {
Null | Any | Bottom | Optional(_) => self,
Null => Nullable(Box::new(self)),
Any | Bottom | Optional(_) => self,
non_nullable => Optional(Box::new(non_nullable)),
}
}
fn into_nullable(self) -> Self {
use self::Shape::*;
match self {
Null => Nullable(Box::new(self)),
Any | Bottom | Nullable(_) => self,
non_nullable => Nullable(Box::new(non_nullable)),
}
}

/// Note: This is asymmetrical because we don't unify based on this,
/// but check if `self` can be used *as is* as a replacement for `other`
Expand Down