github.com/blend/go-sdk@v1.20220411.3/configutil/lazy_int.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 "context"
    11  
    12  // LazyInt returns an IntSource for a given int pointer.
    13  //
    14  // LazyInt differs from IntPtr in that it treats 0 values as unset.
    15  // If 0 is a valid value, use a IntPtr.
    16  func LazyInt(value *int) LazyIntSource {
    17  	return LazyIntSource{Value: value}
    18  }
    19  
    20  var (
    21  	_ IntSource = (*LazyIntSource)(nil)
    22  )
    23  
    24  // LazyIntSource implements value provider.
    25  //
    26  // Note: LazyInt treats 0 as unset, if 0 is a valid value you must use configutil.IntPtr.
    27  type LazyIntSource struct {
    28  	Value *int
    29  }
    30  
    31  // Int returns the value for a constant.
    32  func (i LazyIntSource) Int(_ context.Context) (*int, error) {
    33  	if i.Value != nil && *i.Value > 0 {
    34  		return i.Value, nil
    35  	}
    36  	return nil, nil
    37  }