go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/configutil/lazy.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package configutil
     9  
    10  import "context"
    11  
    12  // Lazy returns a Source for a given typed pointer.
    13  //
    14  // Lazy differs from a typical Source[T] that it returns or evaluates
    15  // the passed value when the resolve happens, potentially after the
    16  // value of the pointer has been set from somewhere else.
    17  //
    18  // Lazy also specifically treats the "zero" value of T as unset.
    19  func Lazy[T comparable](value *T) Source[T] {
    20  	return func(_ context.Context) (*T, error) {
    21  		var zero T
    22  		if value == nil || *value == zero {
    23  			return nil, nil
    24  		}
    25  		return value, nil
    26  	}
    27  }