mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Kindsys: Add Ptr func (#61948)
This commit is contained in:
parent
6e9eb0d931
commit
856abe1281
24
pkg/kindsys/util.go
Normal file
24
pkg/kindsys/util.go
Normal file
@ -0,0 +1,24 @@
|
||||
package kindsys
|
||||
|
||||
// Ptr returns a pointer to a value of an arbitrary type.
|
||||
//
|
||||
// This function is provided to compensate for Grafana's Go code generation that
|
||||
// represents optional fields using pointers.
|
||||
//
|
||||
// Pointers are the only technically [correct, non-ambiguous] way of
|
||||
// representing an optional field in Go's type system. However, Go does not
|
||||
// allow taking the address of certain primitive types inline. That is,
|
||||
// this is invalid Go code:
|
||||
//
|
||||
// var str *string
|
||||
// str = &"colorless green ideas sleep furiously"
|
||||
//
|
||||
// This func allows making such declarations in a single line:
|
||||
//
|
||||
// var str *string
|
||||
// str = kindsys.Ptr("colorless green ideas sleep furiously")
|
||||
//
|
||||
// [correct, non-ambiguous]: https://github.com/grafana/grok/issues/1
|
||||
func Ptr[T any](v T) *T {
|
||||
return &v
|
||||
}
|
Loading…
Reference in New Issue
Block a user