go-driver
go-driver copied to clipboard
TTL index creation fails when expireAt is 0
According to documentation, you can set expireAt to 0.
However, when using EnsureTTLIndex collection method provided by go-driver package:
_, _, err := col.EnsureTTLIndex(ctx, "expireAt", 0, &driver.EnsureTTLIndexOptions{})
if err != nil {
log.Fatal(err)
}
It fails with error:
Failed to create TTL index on collection cache field expireAt TTL 0: expireAfter attribute must be a number
When using any other positive integer instead of 0, the method call succeeds.
Same issue, i think that's because the "omitempty" is set in the JSON annotation and in this case 0 is a Zero value and then not present in the result of the marshal operation
type indexData struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Fields []string `json:"fields,omitempty"`
Unique *bool `json:"unique,omitempty"`
Deduplicate *bool `json:"deduplicate,omitempty"`
Sparse *bool `json:"sparse,omitempty"`
GeoJSON *bool `json:"geoJson,omitempty"`
InBackground *bool `json:"inBackground,omitempty"`
Estimates *bool `json:"estimates,omitempty"`
MinLength int `json:"minLength,omitempty"`
ExpireAfter int `json:"expireAfter,omitempty"`
Name string `json:"name,omitempty"`
}
@wolfx Correct.
The issue can be solved by changing ExpireAfter type to integer pointer instead. This way setting a zero value will work.
Hello @genert
Thanks for feedback.
Unfortunately, fixing this issue with *int will break backward compatibility for driver.Index interface which now contains ExpireAfter() int getter.
Most probably the issue will be fixed in v2 version of go-driver.
Best, Nikita