Skip to content

Q: Missing type and simplify generated code #855

@oleid

Description

@oleid

I'm experimenting with code generation based on schemas generated by the glaze C++ library.
The following C++ struct

struct my_struct {
  int i{};
  double d{};
  std::string hello{};
  std::array<uint64_t, 3> arr{};
  std::map<std::string, int> map{};
};

yields this schema:

my_struct.schema.json

And this is the generated rust code.

my_struct.schema.rs.txt

I was wondering the following:

  1. Apparently, there is no rust equivalent of my_struct being generated. Instead just for every type inside my_struct. Is there maybe an issue with the schema?
  2. Is it somehow possible to prevent generating wrapper types for types like Double etc and just use equivalent types directly?

To demonstrate the latter: I pasted the schema to quicktype and the result seems quite idiomatic:

use serde::{Serialize, Deserialize};
use std::collections::HashMap;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MyStruct {
    arr: Option<Vec<i64>>,
    d: Option<f64>,
    hello: Option<String>,
    i: Option<i64>,
    map: Option<HashMap<String, i64>>,
}

(note: one can get rid of generating all the values as Option by adding "required": ["arr", "d", "hello", "i", "map"], )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions