github.com/blend/go-sdk@v1.20220411.3/reflectutil/follow_value.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 reflectutil
     9  
    10  import "reflect"
    11  
    12  // FollowValue derefs a reflect.Value until it isn't a pointer, but will preseve it's nilness.
    13  func FollowValue(v reflect.Value) interface{} {
    14  	if v.Kind() == reflect.Ptr && v.IsNil() {
    15  		return nil
    16  	}
    17  
    18  	val := v
    19  	for val.Kind() == reflect.Ptr {
    20  		val = val.Elem()
    21  	}
    22  	return val.Interface()
    23  }