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

omitempty tag does not work for empty time.Time struct field when using HSet #3208

Open
gh73962 opened this issue Dec 12, 2024 · 0 comments
Open

Comments

@gh73962
Copy link

gh73962 commented Dec 12, 2024

Issue tracker is used for reporting bugs and discussing new features. Please use
stackoverflow for supporting issues.

Expected Behavior

type MyHash struct { 
    Key1 string `redis:"key1"`;
    Key2 time.Time `redis:"key2,omitempty"` 
}

should be

hset myhash key1 value1

Current Behavior

hset myhash key1 value1 key2 0001-01-01T00:00:00Z

Possible Solution

https://github.com/redis/go-redis/blob/master/commands.go#L142

func isEmptyValue(v reflect.Value) bool {
	switch v.Kind() {
	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
		return v.Len() == 0
	case reflect.Bool:
		return !v.Bool()
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return v.Int() == 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return v.Uint() == 0
	case reflect.Float32, reflect.Float64:
		return v.Float() == 0
	case reflect.Interface, reflect.Pointer:
		return v.IsNil()
++++++++++++++++++++++++++++++++++++++++++++++
        case reflect.Struct:
                return v.IsZero()
++++++++++++++++++++++++++++++++++++++++++++++
	}
	return false
}

Steps to Reproduce

type MyHash struct { 
    Key1 string `redis:"key1"`;
    Key2 time.Time `redis:"key2,omitempty"` 
}

data := MyHash{
     Key1 : "value1",
     Key2 : time.Time{},
}

if err := rdb.HSet(ctx, "key", model1).Err(); err != nil {
	panic(err)
}

Context (Environment)

Detailed Description

omitempty tag does not work for empty time.Time struct field when using HSet

Possible Implementation

If you agree with the proposal I made in the title "Possible Solution," I can proceed to complete this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant