Skip to content

Commit

Permalink
Add omit-empty tag for json
Browse files Browse the repository at this point in the history
  • Loading branch information
kislaykishore committed Aug 9, 2024
1 parent fc543fc commit fc5a838
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 133 deletions.
154 changes: 77 additions & 77 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,185 +24,185 @@ import (
)

type Config struct {
AppName string `yaml:"app-name,omitempty"`
AppName string `yaml:"app-name,omitempty" json:"app-name,omitempty"`

CacheDir ResolvedPath `yaml:"cache-dir,omitempty"`
CacheDir ResolvedPath `yaml:"cache-dir,omitempty" json:"cache-dir,omitempty"`

Debug DebugConfig `yaml:"debug,omitempty"`
Debug DebugConfig `yaml:"debug,omitempty" json:"debug,omitempty"`

EnableHns bool `yaml:"enable-hns,omitempty"`
EnableHns bool `yaml:"enable-hns,omitempty" json:"enable-hns,omitempty"`

FileCache FileCacheConfig `yaml:"file-cache,omitempty"`
FileCache FileCacheConfig `yaml:"file-cache,omitempty" json:"file-cache,omitempty"`

FileSystem FileSystemConfig `yaml:"file-system,omitempty"`
FileSystem FileSystemConfig `yaml:"file-system,omitempty" json:"file-system,omitempty"`

Foreground bool `yaml:"foreground,omitempty"`
Foreground bool `yaml:"foreground,omitempty" json:"foreground,omitempty"`

GcsAuth GcsAuthConfig `yaml:"gcs-auth,omitempty"`
GcsAuth GcsAuthConfig `yaml:"gcs-auth,omitempty" json:"gcs-auth,omitempty"`

GcsConnection GcsConnectionConfig `yaml:"gcs-connection,omitempty"`
GcsConnection GcsConnectionConfig `yaml:"gcs-connection,omitempty" json:"gcs-connection,omitempty"`

GcsRetries GcsRetriesConfig `yaml:"gcs-retries,omitempty"`
GcsRetries GcsRetriesConfig `yaml:"gcs-retries,omitempty" json:"gcs-retries,omitempty"`

ImplicitDirs bool `yaml:"implicit-dirs,omitempty"`
ImplicitDirs bool `yaml:"implicit-dirs,omitempty" json:"implicit-dirs,omitempty"`

List ListConfig `yaml:"list,omitempty"`
List ListConfig `yaml:"list,omitempty" json:"list,omitempty"`

Logging LoggingConfig `yaml:"logging,omitempty"`
Logging LoggingConfig `yaml:"logging,omitempty" json:"logging,omitempty"`

MetadataCache MetadataCacheConfig `yaml:"metadata-cache,omitempty"`
MetadataCache MetadataCacheConfig `yaml:"metadata-cache,omitempty" json:"metadata-cache,omitempty"`

Metrics MetricsConfig `yaml:"metrics,omitempty"`
Metrics MetricsConfig `yaml:"metrics,omitempty" json:"metrics,omitempty"`

Monitoring MonitoringConfig `yaml:"monitoring,omitempty"`
Monitoring MonitoringConfig `yaml:"monitoring,omitempty" json:"monitoring,omitempty"`

OnlyDir string `yaml:"only-dir,omitempty"`
OnlyDir string `yaml:"only-dir,omitempty" json:"only-dir,omitempty"`

Write WriteConfig `yaml:"write,omitempty"`
Write WriteConfig `yaml:"write,omitempty" json:"write,omitempty"`
}

type DebugConfig struct {
ExitOnInvariantViolation bool `yaml:"exit-on-invariant-violation,omitempty"`
ExitOnInvariantViolation bool `yaml:"exit-on-invariant-violation,omitempty" json:"exit-on-invariant-violation,omitempty"`

Fuse bool `yaml:"fuse,omitempty"`
Fuse bool `yaml:"fuse,omitempty" json:"fuse,omitempty"`

Gcs bool `yaml:"gcs,omitempty"`
Gcs bool `yaml:"gcs,omitempty" json:"gcs,omitempty"`

LogMutex bool `yaml:"log-mutex,omitempty"`
LogMutex bool `yaml:"log-mutex,omitempty" json:"log-mutex,omitempty"`
}

type FileCacheConfig struct {
CacheFileForRangeRead bool `yaml:"cache-file-for-range-read,omitempty"`
CacheFileForRangeRead bool `yaml:"cache-file-for-range-read,omitempty" json:"cache-file-for-range-read,omitempty"`

DownloadChunkSizeMb int64 `yaml:"download-chunk-size-mb,omitempty"`
DownloadChunkSizeMb int64 `yaml:"download-chunk-size-mb,omitempty" json:"download-chunk-size-mb,omitempty"`

EnableCrc bool `yaml:"enable-crc,omitempty"`
EnableCrc bool `yaml:"enable-crc,omitempty" json:"enable-crc,omitempty"`

EnableParallelDownloads bool `yaml:"enable-parallel-downloads,omitempty"`
EnableParallelDownloads bool `yaml:"enable-parallel-downloads,omitempty" json:"enable-parallel-downloads,omitempty"`

MaxParallelDownloads int64 `yaml:"max-parallel-downloads,omitempty"`
MaxParallelDownloads int64 `yaml:"max-parallel-downloads,omitempty" json:"max-parallel-downloads,omitempty"`

MaxSizeMb int64 `yaml:"max-size-mb,omitempty"`
MaxSizeMb int64 `yaml:"max-size-mb,omitempty" json:"max-size-mb,omitempty"`

ParallelDownloadsPerFile int64 `yaml:"parallel-downloads-per-file,omitempty"`
ParallelDownloadsPerFile int64 `yaml:"parallel-downloads-per-file,omitempty" json:"parallel-downloads-per-file,omitempty"`
}

type FileSystemConfig struct {
DirMode Octal `yaml:"dir-mode,omitempty"`
DirMode Octal `yaml:"dir-mode,omitempty" json:"dir-mode,omitempty"`

DisableParallelDirops bool `yaml:"disable-parallel-dirops,omitempty"`
DisableParallelDirops bool `yaml:"disable-parallel-dirops,omitempty" json:"disable-parallel-dirops,omitempty"`

FileMode Octal `yaml:"file-mode,omitempty"`
FileMode Octal `yaml:"file-mode,omitempty" json:"file-mode,omitempty"`

FuseOptions []string `yaml:"fuse-options,omitempty"`
FuseOptions []string `yaml:"fuse-options,omitempty" json:"fuse-options,omitempty"`

Gid int64 `yaml:"gid,omitempty"`
Gid int64 `yaml:"gid,omitempty" json:"gid,omitempty"`

IgnoreInterrupts bool `yaml:"ignore-interrupts,omitempty"`
IgnoreInterrupts bool `yaml:"ignore-interrupts,omitempty" json:"ignore-interrupts,omitempty"`

KernelListCacheTtlSecs int64 `yaml:"kernel-list-cache-ttl-secs,omitempty"`
KernelListCacheTtlSecs int64 `yaml:"kernel-list-cache-ttl-secs,omitempty" json:"kernel-list-cache-ttl-secs,omitempty"`

RenameDirLimit int64 `yaml:"rename-dir-limit,omitempty"`
RenameDirLimit int64 `yaml:"rename-dir-limit,omitempty" json:"rename-dir-limit,omitempty"`

TempDir ResolvedPath `yaml:"temp-dir,omitempty"`
TempDir ResolvedPath `yaml:"temp-dir,omitempty" json:"temp-dir,omitempty"`

Uid int64 `yaml:"uid,omitempty"`
Uid int64 `yaml:"uid,omitempty" json:"uid,omitempty"`
}

type GcsAuthConfig struct {
AnonymousAccess bool `yaml:"anonymous-access,omitempty"`
AnonymousAccess bool `yaml:"anonymous-access,omitempty" json:"anonymous-access,omitempty"`

KeyFile ResolvedPath `yaml:"key-file,omitempty"`
KeyFile ResolvedPath `yaml:"key-file,omitempty" json:"key-file,omitempty"`

ReuseTokenFromUrl bool `yaml:"reuse-token-from-url,omitempty"`
ReuseTokenFromUrl bool `yaml:"reuse-token-from-url,omitempty" json:"reuse-token-from-url,omitempty"`

TokenUrl string `yaml:"token-url,omitempty"`
TokenUrl string `yaml:"token-url,omitempty" json:"token-url,omitempty"`
}

type GcsConnectionConfig struct {
BillingProject string `yaml:"billing-project,omitempty"`
BillingProject string `yaml:"billing-project,omitempty" json:"billing-project,omitempty"`

ClientProtocol Protocol `yaml:"client-protocol,omitempty"`
ClientProtocol Protocol `yaml:"client-protocol,omitempty" json:"client-protocol,omitempty"`

CustomEndpoint string `yaml:"custom-endpoint,omitempty"`
CustomEndpoint string `yaml:"custom-endpoint,omitempty" json:"custom-endpoint,omitempty"`

ExperimentalEnableJsonRead bool `yaml:"experimental-enable-json-read,omitempty"`
ExperimentalEnableJsonRead bool `yaml:"experimental-enable-json-read,omitempty" json:"experimental-enable-json-read,omitempty"`

GrpcConnPoolSize int64 `yaml:"grpc-conn-pool-size,omitempty"`
GrpcConnPoolSize int64 `yaml:"grpc-conn-pool-size,omitempty" json:"grpc-conn-pool-size,omitempty"`

HttpClientTimeout time.Duration `yaml:"http-client-timeout,omitempty"`
HttpClientTimeout time.Duration `yaml:"http-client-timeout,omitempty" json:"http-client-timeout,omitempty"`

LimitBytesPerSec float64 `yaml:"limit-bytes-per-sec,omitempty"`
LimitBytesPerSec float64 `yaml:"limit-bytes-per-sec,omitempty" json:"limit-bytes-per-sec,omitempty"`

LimitOpsPerSec float64 `yaml:"limit-ops-per-sec,omitempty"`
LimitOpsPerSec float64 `yaml:"limit-ops-per-sec,omitempty" json:"limit-ops-per-sec,omitempty"`

MaxConnsPerHost int64 `yaml:"max-conns-per-host,omitempty"`
MaxConnsPerHost int64 `yaml:"max-conns-per-host,omitempty" json:"max-conns-per-host,omitempty"`

MaxIdleConnsPerHost int64 `yaml:"max-idle-conns-per-host,omitempty"`
MaxIdleConnsPerHost int64 `yaml:"max-idle-conns-per-host,omitempty" json:"max-idle-conns-per-host,omitempty"`

SequentialReadSizeMb int64 `yaml:"sequential-read-size-mb,omitempty"`
SequentialReadSizeMb int64 `yaml:"sequential-read-size-mb,omitempty" json:"sequential-read-size-mb,omitempty"`
}

type GcsRetriesConfig struct {
MaxRetryAttempts int64 `yaml:"max-retry-attempts,omitempty"`
MaxRetryAttempts int64 `yaml:"max-retry-attempts,omitempty" json:"max-retry-attempts,omitempty"`

MaxRetrySleep time.Duration `yaml:"max-retry-sleep,omitempty"`
MaxRetrySleep time.Duration `yaml:"max-retry-sleep,omitempty" json:"max-retry-sleep,omitempty"`

Multiplier float64 `yaml:"multiplier,omitempty"`
Multiplier float64 `yaml:"multiplier,omitempty" json:"multiplier,omitempty"`
}

type ListConfig struct {
EnableEmptyManagedFolders bool `yaml:"enable-empty-managed-folders,omitempty"`
EnableEmptyManagedFolders bool `yaml:"enable-empty-managed-folders,omitempty" json:"enable-empty-managed-folders,omitempty"`
}

type LogRotateLoggingConfig struct {
BackupFileCount int64 `yaml:"backup-file-count,omitempty"`
BackupFileCount int64 `yaml:"backup-file-count,omitempty" json:"backup-file-count,omitempty"`

Compress bool `yaml:"compress,omitempty"`
Compress bool `yaml:"compress,omitempty" json:"compress,omitempty"`

MaxFileSizeMb int64 `yaml:"max-file-size-mb,omitempty"`
MaxFileSizeMb int64 `yaml:"max-file-size-mb,omitempty" json:"max-file-size-mb,omitempty"`
}

type LoggingConfig struct {
FilePath ResolvedPath `yaml:"file-path,omitempty"`
FilePath ResolvedPath `yaml:"file-path,omitempty" json:"file-path,omitempty"`

Format string `yaml:"format,omitempty"`
Format string `yaml:"format,omitempty" json:"format,omitempty"`

LogRotate LogRotateLoggingConfig `yaml:"log-rotate,omitempty"`
LogRotate LogRotateLoggingConfig `yaml:"log-rotate,omitempty" json:"log-rotate,omitempty"`

Severity LogSeverity `yaml:"severity,omitempty"`
Severity LogSeverity `yaml:"severity,omitempty" json:"severity,omitempty"`
}

type MetadataCacheConfig struct {
DeprecatedStatCacheCapacity int64 `yaml:"deprecated-stat-cache-capacity,omitempty"`
DeprecatedStatCacheCapacity int64 `yaml:"deprecated-stat-cache-capacity,omitempty" json:"deprecated-stat-cache-capacity,omitempty"`

DeprecatedStatCacheTtl time.Duration `yaml:"deprecated-stat-cache-ttl,omitempty"`
DeprecatedStatCacheTtl time.Duration `yaml:"deprecated-stat-cache-ttl,omitempty" json:"deprecated-stat-cache-ttl,omitempty"`

DeprecatedTypeCacheTtl time.Duration `yaml:"deprecated-type-cache-ttl,omitempty"`
DeprecatedTypeCacheTtl time.Duration `yaml:"deprecated-type-cache-ttl,omitempty" json:"deprecated-type-cache-ttl,omitempty"`

EnableNonexistentTypeCache bool `yaml:"enable-nonexistent-type-cache,omitempty"`
EnableNonexistentTypeCache bool `yaml:"enable-nonexistent-type-cache,omitempty" json:"enable-nonexistent-type-cache,omitempty"`

ExperimentalMetadataPrefetchOnMount string `yaml:"experimental-metadata-prefetch-on-mount,omitempty"`
ExperimentalMetadataPrefetchOnMount string `yaml:"experimental-metadata-prefetch-on-mount,omitempty" json:"experimental-metadata-prefetch-on-mount,omitempty"`

StatCacheMaxSizeMb int64 `yaml:"stat-cache-max-size-mb,omitempty"`
StatCacheMaxSizeMb int64 `yaml:"stat-cache-max-size-mb,omitempty" json:"stat-cache-max-size-mb,omitempty"`

TtlSecs int64 `yaml:"ttl-secs,omitempty"`
TtlSecs int64 `yaml:"ttl-secs,omitempty" json:"ttl-secs,omitempty"`

TypeCacheMaxSizeMb int64 `yaml:"type-cache-max-size-mb,omitempty"`
TypeCacheMaxSizeMb int64 `yaml:"type-cache-max-size-mb,omitempty" json:"type-cache-max-size-mb,omitempty"`
}

type MetricsConfig struct {
PrometheusPort int64 `yaml:"prometheus-port,omitempty"`
PrometheusPort int64 `yaml:"prometheus-port,omitempty" json:"prometheus-port,omitempty"`

StackdriverExportInterval time.Duration `yaml:"stackdriver-export-interval,omitempty"`
StackdriverExportInterval time.Duration `yaml:"stackdriver-export-interval,omitempty" json:"stackdriver-export-interval,omitempty"`
}

type MonitoringConfig struct {
ExperimentalOpentelemetryCollectorAddress string `yaml:"experimental-opentelemetry-collector-address,omitempty"`
ExperimentalOpentelemetryCollectorAddress string `yaml:"experimental-opentelemetry-collector-address,omitempty" json:"experimental-opentelemetry-collector-address,omitempty"`
}

type WriteConfig struct {
CreateEmptyFile bool `yaml:"create-empty-file,omitempty"`
CreateEmptyFile bool `yaml:"create-empty-file,omitempty" json:"create-empty-file,omitempty"`
}

func BindFlags(v *viper.Viper, flagSet *pflag.FlagSet) error {
Expand Down
8 changes: 2 additions & 6 deletions cmd/legacy_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,9 @@ func getUserAgent(appName string, config string) string {
}

func logNewConfiguration(newConfig *cfg.Config) {
cfgStr, err := util.YAMLStringify(newConfig)
if err != nil {
logger.Warnf("failed to stringify configuration: %v", err)
return
}
logger.Infof("GCSFuse mount config: %s", cfgStr)
logger.Logger().Info("GCSFuse config", "config", newConfig)
}

func logLegacyConfiguration(flags *flagStorage, mountConfig *config.MountConfig) {
flagsStringified, err := util.JSONStringify(*flags)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ var (
defaultLogger *slog.Logger
)

func Logger() *slog.Logger {
return defaultLogger
}

// InitLogFile initializes the logger factory to create loggers that print to
// a log file.
// In case of empty file, it starts writing the log to syslog file, which
Expand Down
12 changes: 0 additions & 12 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"path/filepath"
"strings"
"time"

"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -87,16 +85,6 @@ func JSONStringify(input any) (string, error) {
return string(inputBytes), nil
}

// YAMLStringify marshals an object to a YAML string. If marshalling fails, it returns an error.
func YAMLStringify(input any) (string, error) {
inputBytes, err := yaml.Marshal(input)

if err != nil {
return "", fmt.Errorf("error in YAMLStringify %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
37 changes: 0 additions & 37 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,43 +148,6 @@ func (ts *UtilTest) ResolveWhenParentProcDirEnvSetAndAbsoluteFilePath() {
assert.Equal(ts.T(), "/var/dir/test.txt", resolvedPath)
}

func TestYAMLStringify(t *testing.T) {
tests := []struct {
name string
obj any
expected string
}{
{
name: "Test Map",
obj: map[string]int{
"1": 1,
"2": 2,
"3": 3,
},
expected: "\"1\": 1\n\"2\": 2\n\"3\": 3\n",
},
{
name: "Test Nested Map",
obj: map[string]any{
"1": 1,
"2": map[string]int{
"3": 1,
},
},
expected: "\"1\": 1\n\"2\":\n \"3\": 1\n",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
s, err := YAMLStringify(tc.obj)

if assert.NoError(t, err) {
assert.Equal(t, tc.expected, s)
}
})
}
}
func (ts *UtilTest) TestStringifyShouldReturnAllFieldsPassedInCustomObjectAsMarshalledString() {
sampleMap := map[string]int{
"1": 1,
Expand Down
2 changes: 1 addition & 1 deletion tools/config-gen/templates/config.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
{{range .TypeTemplateData}}
type {{ .TypeName}} struct {
{{- range $idx, $fld := .Fields}}
{{ $fld.FieldName}} {{ $fld.DataType}} {{$bt}}yaml:"{{$fld.ConfigPath}},omitempty"{{$bt}}
{{ $fld.FieldName}} {{ $fld.DataType}} {{$bt}}yaml:"{{$fld.ConfigPath}},omitempty" json:"{{$fld.ConfigPath}},omitempty"{{$bt}}
{{end}}
}
{{end}}
Expand Down

0 comments on commit fc5a838

Please sign in to comment.