-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
199 lines (165 loc) · 5.48 KB
/
helpers.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package cog
import (
"context"
"fmt"
"cuelang.org/go/cue"
"github.com/grafana/codejen"
"github.com/grafana/cog/internal/ast/compiler"
"github.com/grafana/cog/internal/codegen"
"github.com/grafana/cog/internal/jennies/golang"
"github.com/grafana/cog/internal/jennies/typescript"
"github.com/grafana/cog/internal/simplecue"
)
// SchemaToTypesPipeline represents a simplified codegen.Pipeline, meant to
// take a single input schema and generates types for it in a single output
// language.
type SchemaToTypesPipeline struct {
debug bool
input *codegen.Input
finalPasses compiler.Passes
output *codegen.OutputLanguage
}
// TypesFromSchema generates types from a single input schema and a single
// output language.
func TypesFromSchema() *SchemaToTypesPipeline {
return &SchemaToTypesPipeline{}
}
// Debug controls whether debug mode is enabled or not.
// When enabled, more information is included in the generated output,
// such as an audit trail of applied transformations.
func (pipeline *SchemaToTypesPipeline) Debug(enabled bool) *SchemaToTypesPipeline {
pipeline.debug = enabled
return pipeline
}
// Run executes the codegen pipeline and returns the files generated as a result.
func (pipeline *SchemaToTypesPipeline) Run(ctx context.Context) (codejen.Files, error) {
// At least a tiny bit of validation
if pipeline.input == nil {
return nil, fmt.Errorf("no input configured")
}
if pipeline.output == nil {
return nil, fmt.Errorf("no output configured")
}
codegenPipeline, err := codegen.NewPipeline()
if err != nil {
return nil, err
}
codegenPipeline.Inputs = []*codegen.Input{pipeline.input}
codegenPipeline.Transforms = codegen.Transforms{
FinalPasses: pipeline.finalPasses,
}
codegenPipeline.Output = codegen.Output{
Types: true,
Languages: []*codegen.OutputLanguage{pipeline.output},
}
generatedFS, err := codegenPipeline.Run(ctx)
if err != nil {
return nil, err
}
return generatedFS.AsFiles(), nil
}
/**********
* Inputs *
**********/
type CUEOption func(*codegen.CueInput)
// ForceEnvelope decorates the parsed cue Value with an envelope whose
// name is given. This is useful for dataqueries for example, where the
// schema doesn't define any suitable top-level object.
func ForceEnvelope(envelopeName string) CUEOption {
return func(input *codegen.CueInput) {
input.ForcedEnvelope = envelopeName
}
}
// NameFunc specifies the naming strategy used for objects and references.
// It is called with the value passed to the top level method or function and
// the path to the entity being parsed.
func NameFunc(nameFunc simplecue.NameFunc) CUEOption {
return func(input *codegen.CueInput) {
input.NameFunc = nameFunc
}
}
// CUEImports allows referencing additional libraries/modules.
func CUEImports(importsMap map[string]string) CUEOption {
return func(input *codegen.CueInput) {
for importPkg, pkgPath := range importsMap {
input.CueImports = append(input.CueImports, fmt.Sprintf("%s:%s", pkgPath, importPkg))
}
}
}
// CUEModule sets the pipeline's input to the given cue module.
func (pipeline *SchemaToTypesPipeline) CUEModule(modulePath string, opts ...CUEOption) *SchemaToTypesPipeline {
cueInput := &codegen.CueInput{
Entrypoint: modulePath,
}
for _, opt := range opts {
opt(cueInput)
}
pipeline.input = &codegen.Input{Cue: cueInput}
return pipeline
}
// CUEValue sets the pipeline's input to the given cue value.
func (pipeline *SchemaToTypesPipeline) CUEValue(pkgName string, value cue.Value, opts ...CUEOption) *SchemaToTypesPipeline {
cueInput := &codegen.CueInput{
Package: pkgName,
Value: &value,
}
for _, opt := range opts {
opt(cueInput)
}
pipeline.input = &codegen.Input{Cue: cueInput}
return pipeline
}
/*******************
* Transformations *
*******************/
// AppendCommentToObjects adds the given comment to every object definition.
func AppendCommentToObjects(comment string) compiler.Pass {
return &compiler.AppendCommentObjects{
Comment: comment,
}
}
// PrefixObjectsNames adds the given prefix to every object's name.
func PrefixObjectsNames(prefix string) compiler.Pass {
return &compiler.PrefixObjectNames{
Prefix: prefix,
}
}
// SchemaTransformations adds the given transformations to the set of
// transformations that will be applied to the input schema.
func (pipeline *SchemaToTypesPipeline) SchemaTransformations(passes ...compiler.Pass) *SchemaToTypesPipeline {
pipeline.finalPasses = append(pipeline.finalPasses, passes...)
return pipeline
}
/***********
* Outputs *
***********/
// GoConfig defines a set of configuration options specific to Go outputs.
type GoConfig struct {
// GenerateEqual controls the generation of `Equal()` methods on types.
GenerateEqual bool `yaml:"generate_equal"`
}
// Golang sets the output to Golang types.
func (pipeline *SchemaToTypesPipeline) Golang(config GoConfig) *SchemaToTypesPipeline {
pipeline.output = &codegen.OutputLanguage{
Go: &golang.Config{
GenerateGoMod: false,
SkipRuntime: true,
GenerateEqual: config.GenerateEqual,
},
}
return pipeline
}
// TypescriptConfig defines a set of configuration options specific to Go outputs.
type TypescriptConfig struct {
// placeholder: to be able to define options later without breaking the API.
}
// Typescript sets the output to Typescript types.
func (pipeline *SchemaToTypesPipeline) Typescript(config TypescriptConfig) *SchemaToTypesPipeline {
pipeline.output = &codegen.OutputLanguage{
Typescript: &typescript.Config{
SkipRuntime: true,
SkipIndex: true,
},
}
return pipeline
}