Skip to content

Fix: prevent panic when using named string types as map keys#298

Merged
inhere merged 1 commit intogookit:masterfrom
RyabovNick:master
Jul 29, 2025
Merged

Fix: prevent panic when using named string types as map keys#298
inhere merged 1 commit intogookit:masterfrom
RyabovNick:master

Conversation

@RyabovNick
Copy link
Contributor

In reflection-based validation we were force‑casting map values to string, which panicked if the key wasn’t a raw string but a named string:

val = strings.ReplaceAll(val.(string), "\"", "") //nolint:forcetypeassert

By switching to key.String(), we obtain the textual form of any named string key without a type assertion.

@inhere inhere requested a review from Copilot July 29, 2025 01:35
Copy link

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

Fixes a panic that occurred when using named string types as map keys in reflection-based validation by replacing a force type assertion with the String() method.

  • Replaces force type assertion val.(string) with key.String() method call
  • Adds test coverage with a CustomString type and corresponding map field
  • Prevents runtime panic when validation encounters maps with named string keys

Reviewed Changes

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

File Description
data_source.go Replaces force type assertion with key.String() to safely handle named string types
validation_test.go Adds CustomString type definition and map field for testing
data_source_test.go Adds test data using the new CustomString map field

@coveralls
Copy link

Pull Request Test Coverage Report for Build 16566182440

Details

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 96.743%

Totals Coverage Status
Change from base Build 15276784428: 0.0%
Covered Lines: 2941
Relevant Lines: 3040

💛 - Coveralls

@inhere inhere merged commit 17116c7 into gookit:master Jul 29, 2025
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants