Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor(encoding/gjson): enhance auto type checks when loading data without type specified#4637

Merged
gqcn merged 5 commits intomasterfrom
refract/gjson-type-check
Jan 20, 2026
Merged

refactor(encoding/gjson): enhance auto type checks when loading data without type specified#4637
gqcn merged 5 commits intomasterfrom
refract/gjson-type-check

Conversation

@gqcn
Copy link
Member

@gqcn gqcn commented Jan 20, 2026

This pull request improves YAML support for i18n translation files and refactors content type detection and loading logic in the gjson package. The main changes include more robust detection of YAML, TOML, INI, and Properties formats, refactoring of content type handling, and the addition of new tests to ensure correct parsing of YAML-based i18n resources.

Improved content type detection and loading

  • Refactored content type detection logic in gjson to use dedicated functions for XML, YAML, TOML, INI, and Properties formats, making the detection more reliable and maintainable.
  • Changed the content loading mechanism in gjson to use specific decode functions (gxml.Decode, gyaml.Decode, etc.) for each format instead of converting everything to JSON first, improving accuracy and extensibility.
  • Updated type definitions and struct field comments in gjson.go for clarity and consistency, including changing ContentType to a type alias and improving documentation. [1] [2]

i18n YAML support

  • Modified i18n manager to use the new gjson.LoadPath method for loading translation files, ensuring correct parsing of YAML files for i18n.
  • Added new test cases and test data for loading and verifying YAML i18n files, including edge cases and real-world translation strings. [1] [2] [3]

Minor improvements

  • Simplified file loading logic in gjson.LoadPath by removing caching and directly reading file bytes, which streamlines the code and avoids potential cache issues.

@gqcn gqcn requested review from Copilot, hailaz and houseme and removed request for Copilot January 20, 2026 07:29
@gqcn gqcn changed the title refract(encoding/gjson): enhance auto type checks when loading data without type specified refactor(encoding/gjson): enhance auto type checks when loading data without type specified Jan 20, 2026
Copy link
Contributor

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 refactors gjson content-type detection/loading to better handle non-JSON formats (especially YAML) and wires that into the i18n manager to correctly load YAML-based translation files, adding regression tests around the new behavior.

Changes:

  • Refactored gjson content-type handling to detect XML/YAML/TOML/INI/Properties more explicitly and to decode each format directly instead of first converting to JSON.
  • Updated gjson.LoadPath and i18n Manager.init to rely on the new loading API, improving YAML i18n support and simplifying file loading (no more read cache).
  • Added YAML test fixtures and tests in both encoding/gjson and gi18n to verify YAML-based i18n resources (including keys with dots) are parsed and translated correctly.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
encoding/gjson/gjson.go Adjusts ContentType to a string alias and clarifies Json and Options field documentation, supporting the refactored type handling.
encoding/gjson/gjson_api_new_load_content.go Reworks loadContentWithOptions to auto-detect types via helper predicates, decode formats directly (JSON/XML/YAML/TOML/INI/Properties), and improve error reporting for unsupported or undetectable types.
encoding/gjson/gjson_api_new_load_path.go Simplifies LoadPath to infer content type from the file extension and to read file bytes without caching before delegating to loadContentWithOptions.
encoding/gjson/testdata/yaml/i18n-issue.yaml Adds YAML fixture mirroring real-world i18n key/value data (including dotted keys and non-ASCII values) for gjson YAML-loading tests.
encoding/gjson/gjson_z_unit_feature_load_test.go Adds Test_Load_YAML_For_I18n to ensure LoadContent correctly parses the new YAML i18n fixture and supports violence-key access.
i18n/gi18n/gi18n_manager.go Switches normal-path i18n file loading from gjson.LoadContent over raw bytes to gjson.LoadPath, leveraging file extensions and the new decoding logic for correct YAML/TOML/etc. parsing.
i18n/gi18n/testdata/issue-yaml/zh.yaml Adds YAML fixture for zh translations (including dotted keys like resourceUsage.workflow) to reproduce and guard against the original i18n YAML issue.
i18n/gi18n/gi18n_z_unit_test.go Adds Test_Issue_Yaml to verify the i18n manager correctly loads the new YAML translation file and resolves YAML keys with dots via the standard translation API.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings January 20, 2026 08:21
Copy link
Contributor

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

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gqcn gqcn merged commit f3f2cb3 into master Jan 20, 2026
19 checks passed
@gqcn gqcn deleted the refract/gjson-type-check branch January 20, 2026 11:25
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.

2 participants