From 856abe12818af590620bc967e279a8c7feece794 Mon Sep 17 00:00:00 2001 From: sam boyer Date: Mon, 23 Jan 2023 14:27:33 -0500 Subject: [PATCH] Kindsys: Add Ptr func (#61948) --- pkg/kindsys/util.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 pkg/kindsys/util.go diff --git a/pkg/kindsys/util.go b/pkg/kindsys/util.go new file mode 100644 index 00000000000..b7d03085e79 --- /dev/null +++ b/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 +}