package {{ .PackageName }} import ( "fmt" "sync" {{range .Kinds }} "{{ $.KindPackagePrefix }}/{{ .Meta.MachineName }}"{{end}} "github.com/grafana/grafana/pkg/cuectx" "github.com/grafana/grafana/pkg/kindsys" "github.com/grafana/thema" ) // Base is a registry of kindsys.Interface. It provides two modes for accessing // kinds: individually via literal named methods, or as a slice returned from // an All*() method. // // Prefer the individual named methods for use cases where the particular kind(s) that // are needed are known to the caller. For example, a dashboard linter can know that it // specifically wants the dashboard kind. // // Prefer All*() methods when performing operations generically across all kinds. // For example, a validation HTTP middleware for any kind-schematized object type. type Base struct { all []kindsys.Interface numRaw, numStructured int {{- range .Kinds }} {{ .Meta.MachineName }} *{{ .Meta.MachineName }}.Kind{{end}} } // type guards var ( {{- range .Kinds }} _ kindsys.{{ if .IsRaw }}Raw{{ else }}Structured{{ end }} = &{{ .Meta.MachineName }}.Kind{}{{end}} ) {{range .Kinds }} // {{ .Meta.Name }} returns the [kindsys.Interface] implementation for the {{ .Meta.MachineName }} kind. func (b *Base) {{ .Meta.Name }}() *{{ .Meta.MachineName }}.Kind { return b.{{ .Meta.MachineName }} } {{end}} func doNewBase(rt *thema.Runtime) *Base { var err error reg := &Base{ numRaw: {{ .NumRaw }}, numStructured: {{ .NumStructured }}, } {{range .Kinds }} reg.{{ .Meta.MachineName }}, err = {{ .Meta.MachineName }}.NewKind({{ if .IsCoreStructured }}rt{{ end }}) if err != nil { panic(fmt.Sprintf("error while initializing the {{ .Meta.MachineName }} Kind: %s", err)) } reg.all = append(reg.all, reg.{{ .Meta.MachineName }}) {{end}} return reg }