Skip to content

Ability for set initor#796

Open
maniak89 wants to merge 1 commit intogoccy:masterfrom
maniak89:initor
Open

Ability for set initor#796
maniak89 wants to merge 1 commit intogoccy:masterfrom
maniak89:initor

Conversation

@maniak89
Copy link
Copy Markdown

@maniak89 maniak89 commented Sep 30, 2025

In some cases, special initialization of slices, structs, and maps is required. For example, allocating a slice from sync.Pool or setting default values ​​for structs in a slice where the number of elements is unknown in advance, so that the external YAML file does not contain anchor tags, etc.

Of course, you can use StructValidator, but it has a number of drawbacks, such as:

  • Setting default values ​​is incorrect, as it modifies the structure being validated, as this operation should have no side effects.
  • The user may have set a default value of zero for a type, in which case we will overwrite it. This means that default values ​​should be set before actual unmarshalling, not after, as is the case with a validator.

Custom unmarshalers can also be used to set default values, but this requires a significant amount of duplicate code that must be maintained as new types are added.

You can use something like https://github.com/mcuadros/go-defaults to set default values, eliminating the need to add wrappers or custom marshalers.

An initator is used to implement the pool/default values.

During initialization, it's often necessary to know how the decoder was initialized, as this can depend on various situations. Therefore, methods for retrieving decoder settings were added.

No new tests were added, as the code is essentially already covered by existing tests.

  • Describe the purpose for which you created this PR.
  • Create test code that corresponds to the modification

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 26.96629% with 130 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.91%. Comparing base (25e5d90) to head (c1e8eea).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #796      +/-   ##
==========================================
- Coverage   77.97%   76.91%   -1.07%     
==========================================
  Files          22       22              
  Lines        8108     8268     +160     
==========================================
+ Hits         6322     6359      +37     
- Misses       1370     1479     +109     
- Partials      416      430      +14     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

3 participants