Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #267 #306

Merged
merged 11 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions crypt/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"errors"
"io"

"github.com/bytedance/sonic"
"github.com/gookit/color"

"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/support"
"github.com/goravel/framework/support/json"
)

type AES struct {
Expand Down Expand Up @@ -60,10 +60,12 @@ func (b *AES) EncryptString(value string) (string, error) {

ciphertext := aesgcm.Seal(nil, iv, plaintext, nil)

jsonEncoded, err := sonic.Marshal(map[string][]byte{
var jsonEncoded []byte
jsonEncoded, err = json.Marshal(map[string][]byte{
"iv": iv,
"value": ciphertext,
})

if err != nil {
return "", err
}
Expand All @@ -79,7 +81,7 @@ func (b *AES) DecryptString(payload string) (string, error) {
}

decodeJson := make(map[string][]byte)
err = sonic.Unmarshal(decodePayload, &decodeJson)
err = json.Unmarshal(decodePayload, &decodeJson)
if err != nil {
return "", err
}
Expand Down
28 changes: 14 additions & 14 deletions filesystem/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"mime"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

configmock "github.com/goravel/framework/contracts/config/mocks"
"github.com/goravel/framework/support/carbon"
"github.com/goravel/framework/support/env"
"github.com/goravel/framework/support/file"
)

Expand Down Expand Up @@ -70,28 +70,28 @@ func (s *LocalTestSuite) TestAllDirectories() {
s.True(s.local.Exists("AllDirectories/3/5/6/6.txt"))
files, err := s.local.AllDirectories("AllDirectories")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @kkumar-gcc You can use this method in your PR.

Copy link
Member

Choose a reason for hiding this comment

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

Okay

s.Equal([]string{"3\\", "3\\4\\", "3\\5\\", "3\\5\\6\\"}, files)
} else {
s.Equal([]string{"3/", "3/4/", "3/5/", "3/5/6/"}, files)
}
files, err = s.local.AllDirectories("./AllDirectories")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal([]string{"3\\", "3\\4\\", "3\\5\\", "3\\5\\6\\"}, files)
} else {
s.Equal([]string{"3/", "3/4/", "3/5/", "3/5/6/"}, files)
}
files, err = s.local.AllDirectories("/AllDirectories")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal([]string{"3\\", "3\\4\\", "3\\5\\", "3\\5\\6\\"}, files)
} else {
s.Equal([]string{"3/", "3/4/", "3/5/", "3/5/6/"}, files)
}
files, err = s.local.AllDirectories("./AllDirectories/")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal([]string{"3\\", "3\\4\\", "3\\5\\", "3\\5\\6\\"}, files)
} else {
s.Equal([]string{"3/", "3/4/", "3/5/", "3/5/6/"}, files)
Expand All @@ -110,28 +110,28 @@ func (s *LocalTestSuite) TestAllFiles() {
s.True(s.local.Exists("AllFiles/3/4/4.txt"))
files, err := s.local.AllFiles("AllFiles")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal([]string{"1.txt", "2.txt", "3\\3.txt", "3\\4\\4.txt"}, files)
} else {
s.Equal([]string{"1.txt", "2.txt", "3/3.txt", "3/4/4.txt"}, files)
}
files, err = s.local.AllFiles("./AllFiles")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal([]string{"1.txt", "2.txt", "3\\3.txt", "3\\4\\4.txt"}, files)
} else {
s.Equal([]string{"1.txt", "2.txt", "3/3.txt", "3/4/4.txt"}, files)
}
files, err = s.local.AllFiles("/AllFiles")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal([]string{"1.txt", "2.txt", "3\\3.txt", "3\\4\\4.txt"}, files)
} else {
s.Equal([]string{"1.txt", "2.txt", "3/3.txt", "3/4/4.txt"}, files)
}
files, err = s.local.AllFiles("./AllFiles/")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal([]string{"1.txt", "2.txt", "3\\3.txt", "3\\4\\4.txt"}, files)
} else {
s.Equal([]string{"1.txt", "2.txt", "3/3.txt", "3/4/4.txt"}, files)
Expand Down Expand Up @@ -178,28 +178,28 @@ func (s *LocalTestSuite) TestDirectories() {
s.True(s.local.Exists("Directories/3/5/5.txt"))
files, err := s.local.Directories("Directories")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal([]string{"3\\"}, files)
} else {
s.Equal([]string{"3/"}, files)
}
files, err = s.local.Directories("./Directories")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal([]string{"3\\"}, files)
} else {
s.Equal([]string{"3/"}, files)
}
files, err = s.local.Directories("/Directories")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal([]string{"3\\"}, files)
} else {
s.Equal([]string{"3/"}, files)
}
files, err = s.local.Directories("./Directories/")
s.Nil(err)
if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal([]string{"3\\"}, files)
} else {
s.Equal([]string{"3/"}, files)
Expand Down Expand Up @@ -424,7 +424,7 @@ func (s *LocalTestSuite) TestWithContext() {
func (s *LocalTestSuite) TestUrl() {
s.Equal("https://goravel.dev/Url/1.txt", s.local.Url("Url/1.txt"))

if runtime.GOOS == "windows" {
if env.IsWindows() {
s.Equal("https://goravel.dev/Url/2.txt", s.local.Url(`Url\2.txt`))
}
}
35 changes: 16 additions & 19 deletions log/formatter/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"strings"
"time"

"github.com/bytedance/sonic"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"

"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/support/json"
)

type General struct {
Expand Down Expand Up @@ -53,44 +54,38 @@ func formatData(data logrus.Fields) (string, error) {
var builder strings.Builder

if len(data) > 0 {
dataBytes, err := sonic.Marshal(data)
if err != nil {
return "", err
}

removedData := deleteKey(data, "root")
if len(removedData) > 0 {
removedDataBytes, err := sonic.Marshal(removedData)
removedDataBytes, err := json.Marshal(removedData)
devhaozi marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return "", err
}

builder.WriteString(fmt.Sprintf("fields: %s\n", string(removedDataBytes)))
}

root, err := sonic.Get(dataBytes, "root")
root, err := cast.ToStringMapE(data["root"])
if err != nil {
return "", err
}

for _, key := range []string{"code", "context", "domain", "hint", "owner", "request", "response", "tags", "user"} {
if value := root.Get(key); value.Valid() {
info, err := value.Raw()
if value, exists := root[key]; exists && value != nil {
v, err := json.Marshal(value)
if err != nil {
return "", err
}
builder.WriteString(fmt.Sprintf("%s: %s\n", key, info))

builder.WriteString(fmt.Sprintf(`%s: %v"\n`, key, string(v)))
}
}

if stackTraceValue := root.Get("stacktrace"); stackTraceValue.Valid() {
stackTraces, err := stackTraceValue.Interface()
if err != nil {
return "", err
}
traces, err := formatStackTraces(stackTraces)
if stackTraceValue, exists := root["stacktrace"]; exists && stackTraceValue != nil {
traces, err := formatStackTraces(stackTraceValue)
if err != nil {
return "", err
}

builder.WriteString(traces)
}
}
Expand All @@ -105,6 +100,7 @@ func deleteKey(data logrus.Fields, keyToDelete string) logrus.Fields {
dataCopy[key] = value
}
}

return dataCopy
}

Expand All @@ -121,12 +117,13 @@ type StackTrace struct {

func formatStackTraces(stackTraces any) (string, error) {
var formattedTraces strings.Builder
data, err := sonic.Marshal(stackTraces)
data, err := json.Marshal(stackTraces)

if err != nil {
return "", err
}
var traces StackTrace
err = sonic.Unmarshal(data, &traces)
err = json.Unmarshal(data, &traces)
if err != nil {
return "", err
}
Expand Down
16 changes: 8 additions & 8 deletions log/formatter/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *GeneralTestSuite) TestFormat() {
name: "Data is not empty",
setup: func() {
s.entry.Data = logrus.Fields{
"root": map[string]interface{}{
"root": map[string]any{
"code": "200",
"domain": "example.com",
"owner": "owner",
Expand Down Expand Up @@ -119,14 +119,14 @@ func TestFormatData(t *testing.T) {
name: "Invalid data type",
setup: func() {
data = logrus.Fields{
"root": map[string]interface{}{
"root": map[string]any{
"code": "123",
"context": "sample",
"domain": "example.com",
"hint": make(chan int), // Invalid data type that will cause an error during value extraction
"owner": "owner",
"request": map[string]interface{}{"method": "GET", "uri": "http://localhost"},
"response": map[string]interface{}{"status": 200},
"request": map[string]any{"method": "GET", "uri": "http://localhost"},
"response": map[string]any{"status": 200},
"tags": []string{"tag1", "tag2"},
"user": "user1",
},
Expand All @@ -142,7 +142,7 @@ func TestFormatData(t *testing.T) {
name: "Data is not empty",
setup: func() {
data = logrus.Fields{
"root": map[string]interface{}{
"root": map[string]any{
"code": "200",
"domain": "example.com",
"owner": "owner",
Expand Down Expand Up @@ -234,8 +234,8 @@ func TestFormatStackTraces(t *testing.T) {
{
name: "StackTraces is not nil",
setup: func() {
stackTraces = map[string]interface{}{
"root": map[string]interface{}{
stackTraces = map[string]any{
"root": map[string]any{
"message": "error bad request", // root cause
"stack": []string{
"main.main:/dummy/examples/logging/example.go:143", // original calling method
Expand All @@ -244,7 +244,7 @@ func TestFormatStackTraces(t *testing.T) {
"main.(*Request).Validate:/dummy/examples/logging/example.go:28", // location of the root
},
},
"wrap": []map[string]interface{}{
"wrap": []map[string]any{
{
"message": "received a request with no ID", // additional context
"stack": "main.(*Request).Validate:/dummy/examples/logging/example.go:29", // location of Wrap call
Expand Down
8 changes: 4 additions & 4 deletions support/carbon/json_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package carbon

import (
"encoding/json"
"fmt"
"testing"

"github.com/bytedance/sonic"
"github.com/stretchr/testify/assert"
Comment on lines +4 to 8
Copy link
Member Author

Choose a reason for hiding this comment

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

Seems test file not need import sonic for performance.

)

Expand Down Expand Up @@ -44,7 +44,7 @@ func TestMarshalJSON(t *testing.T) {
CreatedAt3: TimestampMicro{Parse("2025-08-05 13:14:15.999999")},
CreatedAt4: TimestampNano{Parse("2025-08-05 13:14:15.999999999")},
}
data, err := sonic.Marshal(&person)
data, err := json.Marshal(&person)
assert.Nil(t, err)
fmt.Printf("Person output by json:\n%s\n", data)
}
Expand All @@ -67,7 +67,7 @@ func TestUnmarshalJSON(t *testing.T) {
"created_at4": 1596604455999999999
}`

err := sonic.Unmarshal([]byte(str), &person)
err := json.Unmarshal([]byte(str), &person)
assert.Nil(t, err)
fmt.Printf("Json string parse to person:\n%+v\n", person)
}
Expand All @@ -89,7 +89,7 @@ func TestErrorJson(t *testing.T) {
"created_at3": 0,
"created_at4": 0
}`
err := sonic.Unmarshal([]byte(str), &person)
err := json.Unmarshal([]byte(str), &person)
assert.NotNil(t, err)
fmt.Printf("Json string parse to person:\n%+v\n", person)
}
39 changes: 39 additions & 0 deletions support/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package env

import "runtime"

// IsWindows returns whether the current operating system is Windows.
// IsWindows 返回当前操作系统是否为 Windows。
func IsWindows() bool {
return runtime.GOOS == "windows"
}

// IsLinux returns whether the current operating system is Linux.
// IsLinux 返回当前操作系统是否为 Linux。
func IsLinux() bool {
return runtime.GOOS == "linux"
}

// IsDarwin returns whether the current operating system is Darwin.
// IsDarwin 返回当前操作系统是否为 Darwin。
func IsDarwin() bool {
return runtime.GOOS == "darwin"
}

// IsArm returns whether the current CPU architecture is ARM.
// IsArm 返回当前 CPU 架构是否为 ARM。
func IsArm() bool {
return runtime.GOARCH == "arm" || runtime.GOARCH == "arm64"
}

// IsX86 returns whether the current CPU architecture is X86.
// IsX86 返回当前 CPU 架构是否为 X86。
func IsX86() bool {
return runtime.GOARCH == "386" || runtime.GOARCH == "amd64"
}

// Is64Bit returns whether the current CPU architecture is 64-bit.
// Is64Bit 返回当前 CPU 架构是否为 64 位。
func Is64Bit() bool {
return runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64"
}
Loading