Kindsys: Add Ptr func (#61948)

pull/61953/head
sam boyer 2 years ago committed by GitHub
parent 6e9eb0d931
commit 856abe1281
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      pkg/kindsys/util.go

@ -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…
Cancel
Save