-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathschema.tmpl
More file actions
73 lines (68 loc) · 3.53 KB
/
schema.tmpl
File metadata and controls
73 lines (68 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// Code generated by grafana-app-sdk. DO NOT EDIT.
//
package {{.Package}}
{{$sfl := len .SelectableFields}}{{$tcl := len .TableColumns}}
{{$needsFmt := false}}{{range .SelectableFields}}{{if ne .Type "string"}}{{$needsFmt = true}}{{end}}{{end}}
{{$needsErrors := false}}{{if gt $sfl 0}}{{$needsErrors = true}}{{end}}{{if gt $tcl 0}}{{$needsErrors = true}}{{end}}
import (
{{if $needsErrors}}
"errors"{{if $needsFmt}}
"fmt"{{end}}
{{end}}
"github.com/grafana/grafana-app-sdk/resource"
)
{{$root:=.}}
// schema is unexported to prevent accidental overwrites
var (
schema{{.Kind}} = resource.NewSimpleSchema("{{.Group}}", "{{.Version}}", New{{.Kind}}(), &{{.Kind}}List{}, resource.WithKind("{{.Kind}}"),
resource.WithPlural("{{.Plural}}"), resource.WithScope(resource.{{.Scope}}Scope) {{if gt $sfl 0}}, resource.WithSelectableFields([]resource.SelectableField{ {{ range .SelectableFields }}resource.SelectableField{
FieldSelector: "{{.Field}}",
FieldValueFunc: func(o resource.Object) (string, error) {
cast, ok := o.(*{{$root.Kind}})
if !ok {
return "", errors.New("provided object must be of type *{{$root.Kind}}")
}{{ range .OptionalFieldsInPath }}
if cast.{{$root.ToObjectPath .}} == nil {
return "", nil
}{{ end }}
{{ if .Optional }}{{ if eq .Type "string" }}
return *cast.{{$root.ToObjectPath .Field}}, nil{{ else if eq .Type "int" }}
return fmt.Sprintf("%d", *cast.{{$root.ToObjectPath .Field}}), nil{{ else }}
return fmt.Sprintf("%v", *cast.{{$root.ToObjectPath .Field}}), nil{{ end }}{{ else }}{{ if eq .Type "string" }}
return cast.{{$root.ToObjectPath .Field}}, nil{{ else if eq .Type "int" }}
return fmt.Sprintf("%d", cast.{{$root.ToObjectPath .Field}}), nil{{ else }}
return fmt.Sprintf("%v", cast.{{$root.ToObjectPath .Field}}), nil{{ end }}{{ end }}
},
},
{{ end }} }){{ end }}{{if gt $tcl 0}}, resource.WithTableColumns([]resource.TableColumn{ {{ range .TableColumns }}{
Name: "{{.Name}}", Type: "{{.Type}}"{{if .Format}}, Format: "{{.Format}}"{{end}}{{if .Description}}, Description: "{{.Description}}"{{end}}{{if .Priority}}, Priority: {{.Priority}}{{end}}, JSONPath: "{{.JSONPath}}",
ValueFunc: func(o resource.Object) (any, error) {
cast, ok := o.(*{{$root.Kind}})
if !ok {
return nil, errors.New("provided object must be of type *{{$root.Kind}}")
}{{ range .OptionalFieldsInPath }}
if cast.{{$root.ToObjectPath .}} == nil {
return nil, nil
}{{ end }}
{{ if .Optional }}return *cast.{{$root.ToObjectPath .JSONPath}}, nil{{ else }}return cast.{{$root.ToObjectPath .JSONPath}}, nil{{ end }}
},
},
{{ end }} }){{ end }})
kind{{.Kind}} = resource.Kind{
Schema: schema{{.Kind}},
Codecs: map[resource.KindEncoding]resource.Codec{
resource.KindEncodingJSON: &{{.FuncPrefix}}JSONCodec{},
},
}
)
// Kind returns a resource.Kind for this Schema with a JSON codec
func {{.FuncPrefix}}Kind() resource.Kind {
return kind{{.Kind}}
}
// Schema returns a resource.SimpleSchema representation of {{.Kind}}
func {{.FuncPrefix}}Schema() *resource.SimpleSchema {
return schema{{.Kind}}
}
// Interface compliance checks
var _ resource.Schema = kind{{.Kind}}