github.com/blend/go-sdk@v1.20220411.3/configutil/lazy_string.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 // LazyString returns a StringSource for a given string pointer. 13 // 14 // LazyString differs from StringPtr in that it treats empty strings as unset. 15 // If an empty string is a valid value, use a StringPtr. 16 func LazyString(value *string) LazyStringSource { 17 return LazyStringSource{Value: value} 18 } 19 20 var ( 21 _ StringSource = (*LazyStringSource)(nil) 22 ) 23 24 // LazyStringSource implements the LazyString resolver. 25 type LazyStringSource struct { 26 Value *string 27 } 28 29 // String yields the underlying pointer if references a non-empty string. 30 func (s LazyStringSource) String(_ context.Context) (*string, error) { 31 if s.Value == nil || *s.Value == "" { 32 return nil, nil 33 } 34 return s.Value, nil 35 }