go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/configutil/set.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  // Set sets a field from a given list of sources.
    13  func Set[T any](destination *T, sources ...Source[T]) ResolveAction {
    14  	return func(ctx context.Context) error {
    15  		var value *T
    16  		var err error
    17  		for _, source := range sources {
    18  			value, err = source(ctx)
    19  			if err != nil {
    20  				return err
    21  			}
    22  			if value != nil {
    23  				*destination = *value
    24  				return nil
    25  			}
    26  		}
    27  		return nil
    28  	}
    29  }