Skip to content

Q: Missing type and simplify generated code #855

Open

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"], )

Activity

  1. ahl commented on Jul 8, 2025

    @ahl
    Collaborator

    If you add a title to the schema, you'll get this type:

    #[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
    #[serde(deny_unknown_fields)]
    pub struct MyStruct {
        #[serde(default, skip_serializing_if = "::std::option::Option::is_none")]
        pub arr: ::std::option::Option<StdArrayUint64T3>,
        #[serde(default, skip_serializing_if = "::std::option::Option::is_none")]
        pub d: ::std::option::Option<Double>,
        #[serde(default, skip_serializing_if = "::std::option::Option::is_none")]
        pub hello: ::std::option::Option<StdString>,
        #[serde(default, skip_serializing_if = "::std::option::Option::is_none")]
        pub i: ::std::option::Option<Int32T>,
        #[serde(default, skip_serializing_if = "::std::option::Option::is_none")]
        pub map: ::std::option::Option<StdMapStdStringInt32T>,
    }

    Or with required as you suggest:

    #[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
    #[serde(deny_unknown_fields)]
    pub struct BigType {
        pub arr: StdArrayUint64T3,
        pub d: Double,
        pub hello: StdString,
        pub i: Int32T,
        pub map: StdMapStdStringInt32T,
    }

    Agreed that the quicktype output is better in many ways. We make the choice of wrapping named types in a newtype struct with the expectation that the name in the schema is potentially significant. I could imagine a mode that's more aggressive about inlining, smarter about detecting when named types are shared, or simply making this configurable. There is no such feature today; I would suggest pre-processing the schema to inline referenced types if that's desirable.

    Note that quicktype's modeling of std::array<uint64_t,3> does seem a bit less stringent than what typify outputs:

    #[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
    #[serde(transparent)]
    pub struct StdArrayUint64T3(pub [Uint64T; 3usize]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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