Specifying default value for an object should use object's existing default field values
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
Research direction
Start by tracing the generated Rust for object defaults and the existing builder generation and conversion paths. Compare empty and partially specified defaults with the SeparatorConfig examples; done means nested field defaults are preserved and explicitly supplied properties override them while generated values remain valid.
Written by the indexing model from the issue text.
Description
Let's say I have the following object definition in my schema:
"SeparatorConfig": {
"type": "object",
"additionalProperties": false,
"properties": {
"lineThickness": {
"type": "integer",
"default": 1
},
"lineColor": {
"type": [
"string",
"null"
],
"default": "#B2000000"
}
}
},
So SeparatorConfig has two properties, both of which have default values.
Elsewhere in another object I have a field of type SeparatorConfig, and I'd like to make the entire field optional such that it defaults to an object with the correct default field values as defined above.
I could in theory do the following:
"separator": {
"$ref": "#/definitions/SeparatorConfig",
"default": {}
},
However the resulting generated code which produces this default value is:
super::SeparatorConfig {
line_color: Default::default(),
line_thickness: Default::default(),
}
Which produces:
{
"line_color": null,
"line_thickness": 0
}
Rather than the desired default:
{
"line_color": "#B2000000",
"line_thickness": 1
}
A better implementation for creating the default SeparatorConfig struct would be to use the existing builders you already generate:
builder::SeparatorConfig::default().try_into().expect("default should be valid")
We would also have to use the builder functions for defaults which specify only some properties:
"separator": {
"$ref": "#/definitions/SeparatorConfig",
"default": { "lineThickness": 5 }
},
Could produce:
builder::SeparatorConfig::default()
.line_thickness(5_i64)
.try_into()
.expect("default should be valid")
rather than the current:
super::SeparatorConfig {
line_color: Default::default(),
line_thickness: 5_i64,
}
Overall this would make specifying default values for objects throughout a schema significantly easier.
The current alternatives are to either specify all the default field values at every level, duplicating a lot of default values (error prone), or not specifying defaults in the schema and manually constructing default structs in code when properties are None (cumbersome).
- Dominant language
- Rust
- Stars
- 898
- Forks
- 114
- Avg merge
- 4h 18m
- Merged PRs (30d)
- 14
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from oxidecomputer/typify
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
oxidecomputer/typify#1077 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
oxidecomputer/typify#1075 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 50/100
oxidecomputer/typify#1060 ·
-
Difficulty 5/5 Over a week Newbie friendliness 48/100
oxidecomputer/typify#1059 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 65/100
oxidecomputer/typify#1022 · 1 comment ·
All issues in oxidecomputer/typify
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
bug core
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
JIT-compiled number -> Decimal conversion silently overflows instead of raising DECIMAL_OVERFLOW Openfuzz
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ClickHouse/ClickHouse#122114 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
linebender/vello_svg#90 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100