Skip to content

🧹 chore: Handle defaults only when field missing#25

Merged
ReneWerner87 merged 2 commits intomainfrom
codex/überprüfe-default-feature-und-tests-erstellen
May 25, 2025

Hidden character warning

The head ref may contain hidden characters: "codex/\u00fcberpr\u00fcfe-default-feature-und-tests-erstellen"
Merged

🧹 chore: Handle defaults only when field missing#25
ReneWerner87 merged 2 commits intomainfrom
codex/überprüfe-default-feature-und-tests-erstellen

Conversation

@ReneWerner87
Copy link
Copy Markdown
Member

@ReneWerner87 ReneWerner87 commented May 25, 2025

Summary

  • adjust setDefaults to check if form field is provided before applying default values
  • add fieldProvided helper
  • ensure decoder uses new behaviour and update recursive calls
  • test bool default behaviour with missing vs provided values

Testing

  • go test ./...

Summary by CodeRabbit

  • Bug Fixes

    • Default values for fields are now only applied when the input value is missing, preventing user-supplied values from being overwritten.
  • Tests

    • Added a test to ensure default values are applied only when input values are missing, preserving explicit zero and empty values.

@ReneWerner87 ReneWerner87 requested a review from a team as a code owner May 25, 2025 13:34
@ReneWerner87 ReneWerner87 requested review from efectn, gaby and sixcolors and removed request for a team May 25, 2025 13:34
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented May 25, 2025

Walkthrough

The setDefaults method in the decoder logic is updated to accept the source map and a prefix, ensuring default values are only applied to fields not present in the input. A helper function checks if a field is provided in the source data. A new test verifies that defaults are applied only when corresponding input values are missing, preserving explicit zero or empty values.

Changes

File(s) Change Summary
decoder.go Updated setDefaults method signature and logic to accept source map and prefix; added fieldProvided helper to check field presence before applying defaults.
decoder_test.go Added TestDefaultsAppliedOnlyWhenMissing to verify defaults apply only when input values are absent, preserving explicit zero and empty values.

Poem

In the warren of code, where defaults may hide,
A rabbit ensures user values abide.
Only when missing, defaults hop in,
With careful checks, the tests begin.
Boolean fields now behave just right—
The carrot of logic, crisp and bright! 🥕


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f69649c and bad39e1.

📒 Files selected for processing (1)
  • decoder_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • decoder_test.go
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: unit (1.24.x, windows-latest)
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link
Copy Markdown

codecov bot commented May 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.39%. Comparing base (452f46c) to head (bad39e1).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #25      +/-   ##
==========================================
+ Coverage   88.31%   88.39%   +0.07%     
==========================================
  Files           4        4              
  Lines         933      939       +6     
==========================================
+ Hits          824      830       +6     
  Misses         91       91              
  Partials       18       18              
Flag Coverage Δ
unittests 88.39% <100.00%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gaby gaby requested a review from Copilot May 25, 2025 13:49
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adjusts the decoder's default value handling so that default values for boolean fields are only applied when the form field is missing, instead of overwriting provided values. Key changes include:

  • Updating setDefaults to accept the input source and a prefix for proper field detection.
  • Adding a helper function fieldProvided to determine if a field was supplied.
  • Introducing a new test to validate the bool default behaviour.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
decoder_test.go Added test for verifying bool default is applied only if missing.
decoder.go Modified setDefaults signature and logic; added fieldProvided.

return !v.IsZero() && v.Type().Kind() == reflect.Ptr && v.Elem().Type().Kind() == reflect.Struct
}

func fieldProvided(src map[string][]string, prefix string, f *fieldInfo) bool {
Copy link

Copilot AI May 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a doc comment for the fieldProvided function to explain its purpose and the expected format of the prefix argument, enhancing maintainability.

Copilot uses AI. Check for mistakes.
@gaby gaby changed the title Handle bool defaults only when field missing 🧹 chore: Handle bool defaults only when field missing May 25, 2025
Copy link
Copy Markdown
Member

@gaby gaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM

sixcolors

This comment was marked as outdated.

Copy link
Copy Markdown
Member

@sixcolors sixcolors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ReneWerner87 ReneWerner87 changed the title 🧹 chore: Handle bool defaults only when field missing 🧹 chore: Handle defaults only when field missing May 25, 2025
@ReneWerner87
Copy link
Copy Markdown
Member Author

                                     │   old.txt    │                new.txt                │
                                     │    sec/op    │    sec/op      vs base                │
All-12                                 77.14n ± ∞ ¹    78.35n ± ∞ ¹       ~ (p=0.700 n=3) ²
DecoderMultipartFiles-12               5.078µ ± ∞ ¹    5.101µ ± ∞ ¹       ~ (p=1.000 n=3) ²
IsMultipartFile/IsMultipartFile-0-12   3.625n ± ∞ ¹    3.595n ± ∞ ¹       ~ (p=0.400 n=3) ²
IsMultipartFile/IsMultipartFile-1-12   3.629n ± ∞ ¹    3.607n ± ∞ ¹       ~ (p=1.000 n=3) ²
IsMultipartFile/IsMultipartFile-2-12   3.025n ± ∞ ¹    2.992n ± ∞ ¹       ~ (p=0.400 n=3) ²
IsMultipartFile/IsMultipartFile-3-12   3.638n ± ∞ ¹    3.680n ± ∞ ¹       ~ (p=0.700 n=3) ²
IsMultipartFile/IsMultipartFile-4-12   2.126n ± ∞ ¹    2.141n ± ∞ ¹       ~ (p=1.000 n=3) ²
IsMultipartFile/IsMultipartFile-5-12   3.618n ± ∞ ¹    3.786n ± ∞ ¹       ~ (p=0.700 n=3) ²
IsMultipartFile/IsMultipartFile-6-12   6.647n ± ∞ ¹    6.770n ± ∞ ¹       ~ (p=0.400 n=3) ²
HandleMultipartField-12                98.23n ± ∞ ¹   102.10n ± ∞ ¹       ~ (p=0.200 n=3) ²
LargeStructDecode-12                   5.785µ ± ∞ ¹    5.813µ ± ∞ ¹       ~ (p=0.400 n=3) ²
LargeStructDecodeParallel-12           2.786µ ± ∞ ¹    2.911µ ± ∞ ¹       ~ (p=0.100 n=3) ²
SimpleStructDecode-12                  2.470µ ± ∞ ¹    2.611µ ± ∞ ¹       ~ (p=0.100 n=3) ²
CheckRequiredFields-12                 868.6n ± ∞ ¹    847.7n ± ∞ ¹       ~ (p=0.400 n=3) ²
TimeDurationDecoding-12                519.8n ± ∞ ¹    522.1n ± ∞ ¹       ~ (p=0.400 n=3) ²
SimpleStructEncode-12                  1.306µ ± ∞ ¹    1.280µ ± ∞ ¹       ~ (p=0.600 n=3) ²
SimpleStructEncodeParallel-12          193.3n ± ∞ ¹    192.8n ± ∞ ¹       ~ (p=1.000 n=3) ²
LargeStructEncode-12                   2.293µ ± ∞ ¹    2.265µ ± ∞ ¹       ~ (p=0.400 n=3) ²
LargeStructEncodeParallel-12           369.8n ± ∞ ¹    345.4n ± ∞ ¹       ~ (p=0.100 n=3) ²
TimeDurationEncoding-12                230.4n ± ∞ ¹    228.7n ± ∞ ¹       ~ (p=1.000 n=3) ²

benchmark is not really changed

@ReneWerner87 ReneWerner87 merged commit 713caa4 into main May 25, 2025
18 checks passed
@ReneWerner87 ReneWerner87 deleted the codex/überprüfe-default-feature-und-tests-erstellen branch May 25, 2025 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants