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 2 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
19 changes: 15 additions & 4 deletions crypt/aes.go
devhaozi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"io"

Expand All @@ -13,6 +14,7 @@ import (

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

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

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

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

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/environment"
"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 environment.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 environment.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 environment.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 environment.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 environment.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 environment.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 environment.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 environment.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 environment.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 environment.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 environment.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 environment.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 environment.IsWindows() {
s.Equal("https://goravel.dev/Url/2.txt", s.local.Url(`Url\2.txt`))
}
}
45 changes: 27 additions & 18 deletions log/formatter/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package formatter

import (
"bytes"
"encoding/json"
"fmt"
"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/environment"
)

type General struct {
Expand Down Expand Up @@ -53,44 +56,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 {
value, err = cast.ToStringE(value)
if err != nil {
return "", err
}
builder.WriteString(fmt.Sprintf("%s: %s\n", key, info))

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

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 +102,7 @@ func deleteKey(data logrus.Fields, keyToDelete string) logrus.Fields {
dataCopy[key] = value
}
}

return dataCopy
}

Expand All @@ -121,12 +119,23 @@ type StackTrace struct {

func formatStackTraces(stackTraces any) (string, error) {
var formattedTraces strings.Builder
data, err := sonic.Marshal(stackTraces)
var data []byte
var err error
if environment.IsX86() {
data, err = sonic.Marshal(stackTraces)
} else {
data, err = json.Marshal(stackTraces)
}

if err != nil {
return "", err
}
var traces StackTrace
err = sonic.Unmarshal(data, &traces)
if environment.IsX86() {
err = sonic.Unmarshal(data, &traces)
} else {
err = json.Unmarshal(data, &traces)
}
devhaozi marked this conversation as resolved.
Show resolved Hide resolved
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)
}
27 changes: 27 additions & 0 deletions support/environment/environment.go
devhaozi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package environment

import "runtime"

func IsWindows() bool {
return runtime.GOOS == "windows"
}

func IsLinux() bool {
return runtime.GOOS == "linux"
}

func IsDarwin() bool {
return runtime.GOOS == "darwin"
}

func IsArm() bool {
return runtime.GOARCH == "arm" || runtime.GOARCH == "arm64"
}

func IsX86() bool {
return runtime.GOARCH == "386" || runtime.GOARCH == "amd64"
}

func Is64Bit() bool {
return runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64"
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Add some helper methods for judge environment.