refactor(encoding/gjson): enhance auto type checks when loading data without type specified#4637
refactor(encoding/gjson): enhance auto type checks when loading data without type specified#4637
Conversation
…ithout type specified
There was a problem hiding this comment.
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
gjsoncontent-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.LoadPathand i18nManager.initto 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/gjsonandgi18nto 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.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
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.
…ithout type specified
…ithout type specified
This pull request improves YAML support for i18n translation files and refactors content type detection and loading logic in the
gjsonpackage. 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
gjsonto use dedicated functions for XML, YAML, TOML, INI, and Properties formats, making the detection more reliable and maintainable.gjsonto use specific decode functions (gxml.Decode,gyaml.Decode, etc.) for each format instead of converting everything to JSON first, improving accuracy and extensibility.gjson.gofor clarity and consistency, including changingContentTypeto a type alias and improving documentation. [1] [2]i18n YAML support
gjson.LoadPathmethod for loading translation files, ensuring correct parsing of YAML files for i18n.Minor improvements
gjson.LoadPathby removing caching and directly reading file bytes, which streamlines the code and avoids potential cache issues.