Skip to content

Commit

Permalink
Remove JSONStringify
Browse files Browse the repository at this point in the history
  • Loading branch information
kislaykishore committed Aug 9, 2024
1 parent 0f61b63 commit 9542b72
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 86 deletions.
26 changes: 1 addition & 25 deletions cmd/legacy_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,6 @@ func getUserAgent(appName string, config string) string {
}
}

func logNewConfiguration(newConfig *cfg.Config) {
logger.Logger().Info("GCSFuse config", "config", newConfig)
}

func logLegacyConfiguration(flags *flagStorage, mountConfig *config.MountConfig) {
flagsStringified, err := util.JSONStringify(*flags)
if err != nil {
logger.Warnf("failed to stringify cli flags: %v", err)
} else {
logger.Infof("GCSFuse mount command flags: %s", flagsStringified)
}

mountConfigStringified, err := util.JSONStringify(*mountConfig)
if err != nil {
logger.Warnf("failed to stringify config-file: %v", err)
} else {
logger.Infof("GCSFuse mount config flags: %s", mountConfigStringified)
}
}

func getConfigForUserAgent(mountConfig *cfg.Config) string {
// Minimum configuration details created in a bitset fashion. Right now, its restricted only to File Cache Settings.
isFileCacheEnabled := "0"
Expand Down Expand Up @@ -317,11 +297,7 @@ func runCLIApp(c *cli.Context) (err error) {
// if these are already being logged into a log-file, otherwise
// there will be duplicate logs for these in both places (stdout and log-file).
if newConfig.Foreground || newConfig.Logging.FilePath == "" {
if cfg.IsNewConfigEnabled() {
logNewConfiguration(newConfig)
} else {
logLegacyConfiguration(flags, mountConfig)
}
logger.Logger().Info("GCSFuse config", "config", newConfig)
}

// The following will not warn if the user explicitly passed the default value for StatCacheCapacity.
Expand Down
11 changes: 0 additions & 11 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package util

import (
"context"
"encoding/json"
"fmt"
"math"
"os"
Expand Down Expand Up @@ -75,16 +74,6 @@ func GetResolvedPath(filePath string) (resolvedPath string, err error) {
}
}

// JSONStringify marshals an object (only exported attribute) to a JSON string. If marshalling fails, it returns an empty string.
func JSONStringify(input any) (string, error) {
inputBytes, err := json.Marshal(input)

if err != nil {
return "", fmt.Errorf("error in JSONStringify %w", err)
}
return string(inputBytes), nil
}

// MiBsToBytes returns the bytes equivalent
// of given number of MiBs.
// For reference, each MiB = 2^20 bytes.
Expand Down
50 changes: 0 additions & 50 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package util

import (
"context"
"errors"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -148,55 +147,6 @@ func (ts *UtilTest) ResolveWhenParentProcDirEnvSetAndAbsoluteFilePath() {
assert.Equal(ts.T(), "/var/dir/test.txt", resolvedPath)
}

func (ts *UtilTest) TestStringifyShouldReturnAllFieldsPassedInCustomObjectAsMarshalledString() {
sampleMap := map[string]int{
"1": 1,
"2": 2,
"3": 3,
}
sampleNestedValue := nestedCustomType{
SomeField: 10,
SomeOther: sampleMap,
}
customObject := &customTypeForSuccess{
Value: "test_value",
NestedValue: sampleNestedValue,
}

actual, _ := JSONStringify(customObject)

expected := "{\"Value\":\"test_value\",\"NestedValue\":{\"SomeField\":10,\"SomeOther\":{\"1\":1,\"2\":2,\"3\":3}}}"
assert.Equal(ts.T(), expected, actual)
}

func (ts *UtilTest) TestStringifyShouldReturnEmptyStringWhenMarshalErrorsOut() {
customInstance := customTypeForError{
value: "example",
}

actual, _ := JSONStringify(customInstance)

expected := ""
assert.Equal(ts.T(), expected, actual)
}

type customTypeForSuccess struct {
Value string
NestedValue nestedCustomType
}
type nestedCustomType struct {
SomeField int
SomeOther map[string]int
}
type customTypeForError struct {
value string
}

// MarshalJSON returns an error to simulate a failure during JSON marshaling
func (c customTypeForError) MarshalJSON() ([]byte, error) {
return nil, errors.New("intentional error during JSON marshaling")
}

func (ts *UtilTest) TestMiBsToBytes() {
cases := []struct {
mib uint64
Expand Down

0 comments on commit 9542b72

Please sign in to comment.