Skip to content

[FEATURE] Generate exact integer types (int32 or int64) based on JSON schema constraints for Go #2849

@mlkstack

Description

@mlkstack

Context (Input, Language)

Input Format: JSON Schema
Output Language: Go

Description

Currently, quicktype generates int64 for all integer fields in Go, even when the schema specifies integer constraints (minimum, maximum) that would fit within a 32-bit integer range (e.g., minimum: -2147483648, maximum: 2147483647). However, it would be beneficial to generate either int32 or int64 based on the exact range defined in the JSON schema.

For example, if a JSON schema defines an integer range within the bounds of a 32-bit signed integer, quicktype should generate int32. Otherwise, it should generate int64.

This would allow Go developers to work with the most appropriate integer type, based on the schema's constraints, and improve code efficiency and readability.

Current Behaviour / Output

Quicktype defaults to int64 for all integers, regardless of the constraints in the JSON schema.

Proposed Behaviour / Output

Quicktype should generate either int32 or int64 in Go based on the exact minimum and maximum values specified in the JSON schema:

  • If the minimum and maximum range is within the bounds of a 32-bit signed integer (-2147483648 <= minimum <= maximum <= 2147483647), it should generate int32.

If the range exceeds the 32-bit boundary, int64 should be used as it is currently.

Solution

To implement this, quicktype could inspect the minimum and maximum values for each integer field and select the appropriate Go type

Alternatives

Currently, users have to manually adjust the generated Go types to match the schema's integer range, which is cumbersome and error-prone, especially for larger schemas.

Context

This feature would allow Go developers to more accurately match the constraints defined in the JSON schema, avoid unnecessary memory usage by generating int64 when not required, and improve the overall accuracy and efficiency of the generated Go code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions