Skip to content

Commit 6ac43c9

Browse files
author
sam boyer
authored
kindsys: Return Decl from Kinds (grafana#59075)
* kindsys: Return Decl from Kinds * Add funcs to extract DeclForGen from kinds
1 parent 57d6adb commit 6ac43c9

File tree

8 files changed

+41
-13
lines changed

8 files changed

+41
-13
lines changed

pkg/codegen/generators.go

+23
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ func ForGen(rt *thema.Runtime, decl *kindsys.SomeDecl) (*DeclForGen, error) {
2929
}, nil
3030
}
3131

32+
// RawForGen produces a [DeclForGen] from a [kindsys.Raw] kind.
33+
//
34+
// Useful for grafana-external code generators, which depend on the Kind
35+
// representation in registries produced by grafana core (such as
36+
// ["github.com/grafana/grafana/pkg/registry/corekind".NewBase]).
37+
func RawForGen(k kindsys.Raw) *DeclForGen {
38+
return &DeclForGen{
39+
SomeDecl: k.Decl().Some(),
40+
}
41+
}
42+
43+
// StructuredForGen produces a [DeclForGen] from a [kindsys.Structured] kind.
44+
//
45+
// Useful for grafana-external code generators, which depend on the Kind
46+
// representation in registries produced by grafana core (such as
47+
// ["github.com/grafana/grafana/pkg/registry/corekind".NewBase]).
48+
func StructuredForGen(k kindsys.Structured) *DeclForGen {
49+
return &DeclForGen{
50+
SomeDecl: k.Decl().Some(),
51+
lin: k.Lineage(),
52+
}
53+
}
54+
3255
// DeclForGen wraps [kindsys.SomeDecl] to provide trivial caching of
3356
// the lineage declared by the kind (nil for raw kinds).
3457
type DeclForGen struct {

pkg/codegen/tmpl/kind_corestructured.tmpl

+3-2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func (k *Kind) Maturity() kindsys.Maturity {
9090
}
9191

9292
// TODO standard generated docs
93-
func (k *Kind) Meta() kindsys.CoreStructuredMeta {
94-
return k.decl.Meta
93+
func (k *Kind) Decl() *kindsys.Decl[kindsys.CoreStructuredMeta] {
94+
d := k.decl
95+
return &d
9596
}

pkg/codegen/tmpl/kind_raw.tmpl

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (k *Kind) Maturity() kindsys.Maturity {
4242
}
4343

4444
// TODO standard generated docs
45-
func (k *Kind) Meta() kindsys.RawMeta {
46-
return k.decl.Meta
45+
func (k *Kind) Decl() *kindsys.Decl[kindsys.RawMeta] {
46+
d := k.decl
47+
return &d
4748
}

pkg/kinds/dashboard/dashboard_kind_gen.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func (k *Kind) Maturity() kindsys.Maturity {
9999
}
100100

101101
// TODO standard generated docs
102-
func (k *Kind) Meta() kindsys.CoreStructuredMeta {
103-
return k.decl.Meta
102+
func (k *Kind) Decl() *kindsys.Decl[kindsys.CoreStructuredMeta] {
103+
d := k.decl
104+
return &d
104105
}

pkg/kinds/playlist/playlist_kind_gen.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func (k *Kind) Maturity() kindsys.Maturity {
9999
}
100100

101101
// TODO standard generated docs
102-
func (k *Kind) Meta() kindsys.CoreStructuredMeta {
103-
return k.decl.Meta
102+
func (k *Kind) Decl() *kindsys.Decl[kindsys.CoreStructuredMeta] {
103+
d := k.decl
104+
return &d
104105
}

pkg/kinds/svg/svg_kind_gen.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (k *Kind) Maturity() kindsys.Maturity {
4949
}
5050

5151
// TODO standard generated docs
52-
func (k *Kind) Meta() kindsys.RawMeta {
53-
return k.decl.Meta
52+
func (k *Kind) Decl() *kindsys.Decl[kindsys.RawMeta] {
53+
d := k.decl
54+
return &d
5455
}

pkg/kindsys/kind.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type Raw interface {
5858
Interface
5959

6060
// TODO docs
61-
Meta() RawMeta
61+
Decl() *Decl[RawMeta]
6262
}
6363

6464
type Structured interface {
@@ -68,7 +68,7 @@ type Structured interface {
6868
Lineage() thema.Lineage
6969

7070
// TODO docs
71-
Meta() CoreStructuredMeta // TODO figure out how to reconcile this interface with CustomStructuredMeta
71+
Decl() *Decl[CoreStructuredMeta] // TODO figure out how to reconcile this interface with CustomStructuredMeta
7272
}
7373

7474
// type Composable interface {

pkg/kindsys/kindmetas.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package kindsys
22

33
import "github.com/grafana/thema"
44

5-
// CommonMeta contains the kind metadata common to all categories of kinds.
5+
// CommonMeta contains the metadata common to all categories of kinds.
66
type CommonMeta struct {
77
Name string `json:"name"`
88
PluralName string `json:"pluralName"`

0 commit comments

Comments
 (0)