evestera / json_typegen

Tools and libraries to create types for Rust, Kotlin, TypeScript and Python from JSON samples
https://typegen.vestera.as
Apache License 2.0
273 stars 26 forks source link

Reuse already defined types #3

Closed evestera closed 2 years ago

evestera commented 7 years ago

We should avoid creating multiple structurally identical types. E.g. for the following JSON:

{
  "pointA": {
    "x": 3,
    "y": 5
  },
  "pointB": {
    "x": 3,
    "y": 5
  }
}

We currently generate this:

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
struct Rect {
    #[serde(rename = "pointA")]
    point_a: PointA,
    #[serde(rename = "pointB")]
    point_b: PointB,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
struct PointB {
    x: i64,
    y: i64,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
struct PointA {
    x: i64,
    y: i64,
}

But we want something like this:

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
struct Rect {
    #[serde(rename = "pointA")]
    point_a: Point,
    #[serde(rename = "pointB")]
    point_b: Point,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
struct Point {
    x: i64,
    y: i64,
}
sudo-ben commented 5 years ago

Also this seems to not generate any code

[{"key": "10"}, {"key": "2"}, {"key": "1"}]

Should be

struct Item {
    key: String,
}
type TypeName = Vec<Item>;
evestera commented 5 years ago

Also this seems to not generate any code

[{"key": "10"}, {"key": "2"}, {"key": "1"}]

Hmm, that seems weird. Getting output both from the CLI and the old web interface. What interface are you using?