github.com/blend/go-sdk@v1.20220411.3/configutil/lazy_duration.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package configutil
     9  
    10  import (
    11  	"context"
    12  	"time"
    13  )
    14  
    15  // LazyDuration returns an DurationSource for a given duration pointer.
    16  //
    17  // LazyDuration differs from DurationPtr in that it treats 0 values as unset.
    18  // If 0 is a valid value, use a DurationPtr.
    19  func LazyDuration(value *time.Duration) LazyDurationSource {
    20  	return LazyDurationSource{Value: value}
    21  }
    22  
    23  var (
    24  	_ DurationSource = (*LazyDurationSource)(nil)
    25  )
    26  
    27  // LazyDurationSource implements value provider.
    28  //
    29  // Note: LazyDuration treats 0 as unset, if 0 is a valid value you must use configutil.DurationPtr.
    30  type LazyDurationSource struct {
    31  	Value *time.Duration
    32  }
    33  
    34  // Duration returns the value for a constant.
    35  func (i LazyDurationSource) Duration(_ context.Context) (*time.Duration, error) {
    36  	if i.Value != nil && *i.Value > 0 {
    37  		return i.Value, nil
    38  	}
    39  	return nil, nil
    40  }