github.com/blend/go-sdk@v1.20220411.3/configutil/set.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  // SetString coalesces a given list of sources into a variable.
    16  func SetString(destination *string, sources ...StringSource) ResolveAction {
    17  	return func(ctx context.Context) error {
    18  		var value *string
    19  		var err error
    20  		for _, source := range sources {
    21  			value, err = source.String(ctx)
    22  			if err != nil {
    23  				return err
    24  			}
    25  			if value != nil {
    26  				*destination = *value
    27  				return nil
    28  			}
    29  		}
    30  		return nil
    31  	}
    32  }
    33  
    34  // SetStringPtr coalesces a given list of sources into a variable.
    35  func SetStringPtr(destination **string, sources ...StringSource) ResolveAction {
    36  	return func(ctx context.Context) error {
    37  		var value *string
    38  		var err error
    39  		for _, source := range sources {
    40  			value, err = source.String(ctx)
    41  			if err != nil {
    42  				return err
    43  			}
    44  			if value != nil {
    45  				*destination = value
    46  				return nil
    47  			}
    48  		}
    49  		return nil
    50  	}
    51  }
    52  
    53  // SetStrings coalesces a given list of sources into a variable.
    54  func SetStrings(destination *[]string, sources ...StringsSource) ResolveAction {
    55  	return func(ctx context.Context) error {
    56  		var value []string
    57  		var err error
    58  		for _, source := range sources {
    59  			value, err = source.Strings(ctx)
    60  			if err != nil {
    61  				return err
    62  			}
    63  			if value != nil {
    64  				*destination = value
    65  				return nil
    66  			}
    67  		}
    68  		return nil
    69  	}
    70  }
    71  
    72  // SetBool coalesces a given list of sources into a variable.
    73  func SetBool(destination *bool, sources ...BoolSource) ResolveAction {
    74  	return func(ctx context.Context) error {
    75  		var value *bool
    76  		var err error
    77  		for _, source := range sources {
    78  			value, err = source.Bool(ctx)
    79  			if err != nil {
    80  				return err
    81  			}
    82  			if value != nil {
    83  				*destination = *value
    84  				return nil
    85  			}
    86  		}
    87  		return nil
    88  	}
    89  }
    90  
    91  // SetBoolPtr coalesces a given list of sources into a variable.
    92  func SetBoolPtr(destination **bool, sources ...BoolSource) ResolveAction {
    93  	return func(ctx context.Context) error {
    94  		var value *bool
    95  		var err error
    96  		for _, source := range sources {
    97  			value, err = source.Bool(ctx)
    98  			if err != nil {
    99  				return err
   100  			}
   101  			if value != nil {
   102  				*destination = value
   103  				return nil
   104  			}
   105  		}
   106  		return nil
   107  	}
   108  }
   109  
   110  // SetInt coalesces a given list of sources into a variable.
   111  func SetInt(destination *int, sources ...IntSource) ResolveAction {
   112  	return func(ctx context.Context) error {
   113  		var value *int
   114  		var err error
   115  		for _, source := range sources {
   116  			value, err = source.Int(ctx)
   117  			if err != nil {
   118  				return err
   119  			}
   120  			if value != nil {
   121  				*destination = *value
   122  				return nil
   123  			}
   124  		}
   125  		return nil
   126  	}
   127  }
   128  
   129  // SetIntPtr coalesces a given list of sources into a variable.
   130  func SetIntPtr(destination **int, sources ...IntSource) ResolveAction {
   131  	return func(ctx context.Context) error {
   132  		var value *int
   133  		var err error
   134  		for _, source := range sources {
   135  			value, err = source.Int(ctx)
   136  			if err != nil {
   137  				return err
   138  			}
   139  			if value != nil {
   140  				*destination = value
   141  				return nil
   142  			}
   143  		}
   144  		return nil
   145  	}
   146  }
   147  
   148  // SetInt32 coalesces a given list of sources into a variable.
   149  func SetInt32(destination *int32, sources ...Int32Source) ResolveAction {
   150  	return func(ctx context.Context) error {
   151  		var value *int32
   152  		var err error
   153  		for _, source := range sources {
   154  			value, err = source.Int32(ctx)
   155  			if err != nil {
   156  				return err
   157  			}
   158  			if value != nil {
   159  				*destination = *value
   160  				return nil
   161  			}
   162  		}
   163  		return nil
   164  	}
   165  }
   166  
   167  // SetInt32Ptr coalesces a given list of sources into a variable.
   168  func SetInt32Ptr(destination **int32, sources ...Int32Source) ResolveAction {
   169  	return func(ctx context.Context) error {
   170  		var value *int32
   171  		var err error
   172  		for _, source := range sources {
   173  			value, err = source.Int32(ctx)
   174  			if err != nil {
   175  				return err
   176  			}
   177  			if value != nil {
   178  				*destination = value
   179  				return nil
   180  			}
   181  		}
   182  		return nil
   183  	}
   184  }
   185  
   186  // SetInt64 coalesces a given list of sources into a variable.
   187  func SetInt64(destination *int64, sources ...Int64Source) ResolveAction {
   188  	return func(ctx context.Context) error {
   189  		var value *int64
   190  		var err error
   191  		for _, source := range sources {
   192  			value, err = source.Int64(ctx)
   193  			if err != nil {
   194  				return err
   195  			}
   196  			if value != nil {
   197  				*destination = *value
   198  				return nil
   199  			}
   200  		}
   201  		return nil
   202  	}
   203  }
   204  
   205  // SetInt64Ptr coalesces a given list of sources into a variable.
   206  func SetInt64Ptr(destination **int64, sources ...Int64Source) ResolveAction {
   207  	return func(ctx context.Context) error {
   208  		var value *int64
   209  		var err error
   210  		for _, source := range sources {
   211  			value, err = source.Int64(ctx)
   212  			if err != nil {
   213  				return err
   214  			}
   215  			if value != nil {
   216  				*destination = value
   217  				return nil
   218  			}
   219  		}
   220  		return nil
   221  	}
   222  }
   223  
   224  // SetFloat64 coalesces a given list of sources into a variable.
   225  func SetFloat64(destination *float64, sources ...Float64Source) ResolveAction {
   226  	return func(ctx context.Context) error {
   227  		var value *float64
   228  		var err error
   229  		for _, source := range sources {
   230  			value, err = source.Float64(ctx)
   231  			if err != nil {
   232  				return err
   233  			}
   234  			if value != nil {
   235  				*destination = *value
   236  				return nil
   237  			}
   238  		}
   239  		return nil
   240  	}
   241  }
   242  
   243  // SetFloat64Ptr coalesces a given list of sources into a variable.
   244  func SetFloat64Ptr(destination **float64, sources ...Float64Source) ResolveAction {
   245  	return func(ctx context.Context) error {
   246  		var value *float64
   247  		var err error
   248  		for _, source := range sources {
   249  			value, err = source.Float64(ctx)
   250  			if err != nil {
   251  				return err
   252  			}
   253  			if value != nil {
   254  				*destination = value
   255  				return nil
   256  			}
   257  		}
   258  		return nil
   259  	}
   260  }
   261  
   262  // SetDuration coalesces a given list of sources into a variable.
   263  func SetDuration(destination *time.Duration, sources ...DurationSource) ResolveAction {
   264  	return func(ctx context.Context) error {
   265  		var value *time.Duration
   266  		var err error
   267  		for _, source := range sources {
   268  			value, err = source.Duration(ctx)
   269  			if err != nil {
   270  				return err
   271  			}
   272  			if value != nil {
   273  				*destination = *value
   274  				return nil
   275  			}
   276  		}
   277  		return nil
   278  	}
   279  }
   280  
   281  // SetDurationPtr coalesces a given list of sources into a variable.
   282  func SetDurationPtr(destination **time.Duration, sources ...DurationSource) ResolveAction {
   283  	return func(ctx context.Context) error {
   284  		var value *time.Duration
   285  		var err error
   286  		for _, source := range sources {
   287  			value, err = source.Duration(ctx)
   288  			if err != nil {
   289  				return err
   290  			}
   291  			if value != nil {
   292  				*destination = value
   293  				return nil
   294  			}
   295  		}
   296  		return nil
   297  	}
   298  }