go-driver icon indicating copy to clipboard operation
go-driver copied to clipboard

TTL index creation fails when expireAt is 0

Open genert opened this issue 5 years ago • 3 comments

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.

genert avatar Nov 04 '20 14:11 genert

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 avatar May 28 '21 17:05 wolfx

@wolfx Correct.

The issue can be solved by changing ExpireAfter type to integer pointer instead. This way setting a zero value will work.

genert avatar May 29 '21 12:05 genert

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

nikita-vanyasin avatar Mar 18 '22 08:03 nikita-vanyasin