Skip to content

feat: add MarshalString and UnmarshalString method#1025

Merged
devhaozi merged 5 commits intomasterfrom
haozi/perf
May 7, 2025
Merged

feat: add MarshalString and UnmarshalString method#1025
devhaozi merged 5 commits intomasterfrom
haozi/perf

Conversation

@devhaozi
Copy link
Member

@devhaozi devhaozi commented May 6, 2025

📑 Description

Closes https://github.com/goravel/goravel/issues/

Add MarshalString and UnmarshalString method and zero-copy string-bytes methods.

✅ Checks

  • Added test cases for my code

Copilot AI review requested due to automatic review settings May 6, 2025 09:20
@devhaozi devhaozi requested a review from a team as a code owner May 6, 2025 09:20
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 introduces two new JSON helper methods—MarshalString and UnmarshalString—to simplify JSON (de)serialization throughout the codebase. Key changes include:

  • Replacing manual conversions with the new JSON string methods in various modules (HTTP, session, logging, caching, etc.).
  • Updating conversion logic in the support/convert package to use unsafe conversions.
  • Adjusting tests to cover the new conversion functions.

Reviewed Changes

Copilot reviewed 17 out of 18 changed files in this pull request and generated no comments.

Show a summary per file
File Description
testing/http/assertable_json.go Updated to use UnmarshalString for JSON deserialization.
support/json/json.go Added new MarshalString and UnmarshalString methods.
support/file/file.go Uses convert.UnsafeString instead of string conversion.
support/convert/convert_test.go Added tests for new string/byte conversion utilities.
support/convert/convert.go Introduced unsafe conversion functions with benchmarking comment.
session/session.go Updated JSON (de)serialization in session Save and read functions.
log/formatter/general.go Switched to MarshalString for logging field output.
http/limit/store.go Replaced Unmarshal and Marshal conversion logic with string versions.
http/client/response.go & http/client/request.go Updated to use UnmarshalString and unsafe string conversion.
hash/bcrypt.go & hash/argon2id.go Changed to use unsafe byte conversion for hash operations.
foundation/json/json.go Extended Json type with new MarshalString and UnmarshalString.
crypt/aes.go Updated key and plaintext conversion to use unsafe methods.
contracts/foundation/json.go Updated JSON interface to include string-based (de)serialization.
auth/jwt_guard.go Modified to use unsafe conversion for secret handling.
Files not reviewed (1)
  • tests/go.mod: Language not supported

h2zi and others added 2 commits May 6, 2025 17:22
@devhaozi devhaozi enabled auto-merge (squash) May 6, 2025 09:23
@devhaozi devhaozi closed this May 6, 2025
auto-merge was automatically disabled May 6, 2025 09:23

Pull request was closed

@devhaozi devhaozi reopened this May 6, 2025
@devhaozi devhaozi enabled auto-merge (squash) May 6, 2025 09:23
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: b9464fe Previous: a232615 Ratio
BenchmarkFile_ReadWrite 382669 ns/op 2073 B/op 28 allocs/op 172419 ns/op 2072 B/op 28 allocs/op 2.22
BenchmarkFile_ReadWrite - ns/op 382669 ns/op 172419 ns/op 2.22

This comment was automatically generated by workflow using github-action-benchmark.

@codecov
Copy link

codecov bot commented May 6, 2025

Codecov Report

Attention: Patch coverage is 74.19355% with 16 lines in your changes missing coverage. Please review.

Project coverage is 71.10%. Comparing base (9da6b2c) to head (0cc7ed6).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
foundation/json/json.go 0.00% 8 Missing ⚠️
support/json/json.go 40.00% 4 Missing and 2 partials ⚠️
http/client/request.go 0.00% 0 Missing and 1 partial ⚠️
http/client/response.go 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1025      +/-   ##
==========================================
- Coverage   71.16%   71.10%   -0.06%     
==========================================
  Files         172      172              
  Lines       11807    11833      +26     
==========================================
+ Hits         8402     8414      +12     
- Misses       3052     3064      +12     
- Partials      353      355       +2     

☔ 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.

@devhaozi devhaozi self-assigned this May 6, 2025
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Great optimization, LGTM.

Comment on lines +88 to +90
func CopyString(s string) string {
return string(UnsafeBytes(s))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: It's the same with a := b.

@devhaozi devhaozi merged commit 1fa2c00 into master May 7, 2025
13 checks passed
@devhaozi devhaozi deleted the haozi/perf branch May 7, 2025 08:05
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