feat(v3): apply schema default values via generated SetDefaults (#138) - #326
Merged
Merged
Conversation
Default values declared in the specification were parsed but never used. Object schemas now get a SetDefaults() method that fills optional (pointer) scalar fields with their default when unset, leaving already-set fields untouched. Supports string, boolean, integer and number defaults. Fixes #138 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
Fixes #138 —
defaultvalues in the specification were parsed into the schema but never used in the generated code.Change
Object schemas now get a
SetDefaults()method when at least one optional (pointer) property declares a scalardefault. Calling it fills unset (nil) fields with their default and leaves already-set fields untouched:Supported default types: string, boolean, integer (
int32/int64) and number (float32/float64), matching the field type produced by theschema-nametemplate. Date/time, object and array defaults are intentionally out of scope. Two new v3 template helpers (hasScalarDefault,defaultLiteral) drive the generation.Test
test/v3/issues/138defines aSettingsschema with integer/number/boolean/string defaults; the suite assertsSetDefaults()fills unset fields and preserves set ones. Before the change the method is not generated, so the package fails to compile; after, it passes.test/v3/issues/224's golden file is regenerated (itsmarginproperty gains aSetDefaults).go generate ./...is otherwise clean.🤖 Generated with Claude Code