Documentation
¶
Overview ¶
Example (CueModule) ¶
ExampleCueModule demonstrates how to generate types from a CUE module living on the filesystem.
files, err := TypesFromSchema().
CUEModule("/path/to/cue/module").
Golang(GoConfig{}).
Run(context.Background())
if err != nil {
panic(err)
}
if len(files) != 1 {
panic("expected a single file :(")
}
fmt.Println(string(files[0].Data))
Example (CueValue) ¶
ExampleCueValue demonstrates how to generate types from a CUE value.
schema := `
// Contains things.
Container: {
str: string
}
// This is a bar.
Bar: {
type: "bar"
foo: string
}
`
cueValue := cuecontext.New().CompileString(schema)
if cueValue.Err() != nil {
panic(cueValue.Err())
}
files, err := TypesFromSchema().
CUEValue("sandbox", cueValue).
Golang(GoConfig{}).
Run(context.Background())
if err != nil {
panic(err)
}
if len(files) != 1 {
panic("expected a single file :(")
}
fmt.Println(string(files[0].Data))
Example (GoOutput) ¶
ExampleGoOutput demonstrates how to generate Go types from a CUE value.
schema := `
// Contains things.
Container: {
str: string
}
// This is a bar.
Bar: {
type: "bar"
foo: string
}
`
cueValue := cuecontext.New().CompileString(schema)
if cueValue.Err() != nil {
panic(cueValue.Err())
}
files, err := TypesFromSchema().
CUEValue("sandbox", cueValue).
Golang(GoConfig{}).
Run(context.Background())
if err != nil {
panic(err)
}
if len(files) != 1 {
panic("expected a single file :(")
}
fmt.Println(string(files[0].Data))
Example (SchemaTransformations) ¶
ExampleSchemaTransformations demonstrates how apply transformations to input schemas.
schema := `
// Contains things.
Container: {
str: string
}
// This is a bar.
Bar: {
type: "bar"
foo: string
}
`
cueValue := cuecontext.New().CompileString(schema)
if cueValue.Err() != nil {
panic(cueValue.Err())
}
files, err := TypesFromSchema().
CUEValue("sandbox", cueValue).
Golang(GoConfig{}).
SchemaTransformations(
AppendCommentToObjects("Transformed by cog."),
PrefixObjectsNames("Example"),
).
Run(context.Background())
if err != nil {
panic(err)
}
if len(files) != 1 {
panic("expected a single file :(")
}
fmt.Println(string(files[0].Data))
Example (TypescriptOutput) ¶
ExampleTypescriptOutput demonstrates how to generate Typescript types from a CUE value.
schema := `
// Contains things.
Container: {
str: string
}
// This is a bar.
Bar: {
type: "bar"
foo: string
}
`
cueValue := cuecontext.New().CompileString(schema)
if cueValue.Err() != nil {
panic(cueValue.Err())
}
files, err := TypesFromSchema().
CUEValue("sandbox", cueValue).
Typescript(TypescriptConfig{}).
Run(context.Background())
if err != nil {
panic(err)
}
if len(files) != 1 {
panic("expected a single file :(")
}
fmt.Println(string(files[0].Data))
Index ¶
- func AppendCommentToObjects(comment string) compiler.Pass
- func PrefixObjectsNames(prefix string) compiler.Pass
- type CUEOption
- type GoConfig
- type SchemaToTypesPipeline
- func (pipeline *SchemaToTypesPipeline) CUEModule(modulePath string, opts ...CUEOption) *SchemaToTypesPipeline
- func (pipeline *SchemaToTypesPipeline) CUEValue(pkgName string, value cue.Value, opts ...CUEOption) *SchemaToTypesPipeline
- func (pipeline *SchemaToTypesPipeline) Debug(enabled bool) *SchemaToTypesPipeline
- func (pipeline *SchemaToTypesPipeline) Golang(config GoConfig) *SchemaToTypesPipeline
- func (pipeline *SchemaToTypesPipeline) Run(ctx context.Context) (codejen.Files, error)
- func (pipeline *SchemaToTypesPipeline) SchemaTransformations(passes ...compiler.Pass) *SchemaToTypesPipeline
- func (pipeline *SchemaToTypesPipeline) Typescript(config TypescriptConfig) *SchemaToTypesPipeline
- type TypescriptConfig
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendCommentToObjects ¶
AppendCommentToObjects adds the given comment to every object definition.
func PrefixObjectsNames ¶
PrefixObjectsNames adds the given prefix to every object's name.
Types ¶
type CUEOption ¶
func CUEImports ¶ added in v0.0.2
CUEImports allows referencing additional libraries/modules.
func ForceEnvelope ¶
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.
type GoConfig ¶ added in v0.0.2
type GoConfig struct {
// GenerateEqual controls the generation of `Equal()` methods on types.
GenerateEqual bool
}
GoConfig defines a set of configuration options specific to Go outputs.
type SchemaToTypesPipeline ¶
type SchemaToTypesPipeline struct {
// contains filtered or unexported fields
}
SchemaToTypesPipeline represents a simplified codegen.Pipeline, meant to take a single input schema and generates types for it in a single output language.
func TypesFromSchema ¶
func TypesFromSchema() *SchemaToTypesPipeline
TypesFromSchema generates types from a single input schema and a single output language.
func (*SchemaToTypesPipeline) CUEModule ¶ added in v0.0.2
func (pipeline *SchemaToTypesPipeline) CUEModule(modulePath string, opts ...CUEOption) *SchemaToTypesPipeline
CUEModule sets the pipeline's input to the given cue module.
func (*SchemaToTypesPipeline) CUEValue ¶
func (pipeline *SchemaToTypesPipeline) CUEValue(pkgName string, value cue.Value, opts ...CUEOption) *SchemaToTypesPipeline
CUEValue sets the pipeline's input to the given cue value.
func (*SchemaToTypesPipeline) Debug ¶
func (pipeline *SchemaToTypesPipeline) Debug(enabled bool) *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 (*SchemaToTypesPipeline) Golang ¶
func (pipeline *SchemaToTypesPipeline) Golang(config GoConfig) *SchemaToTypesPipeline
Golang sets the output to Golang types.
func (*SchemaToTypesPipeline) Run ¶
Run executes the codegen pipeline and returns the files generated as a result.
func (*SchemaToTypesPipeline) SchemaTransformations ¶
func (pipeline *SchemaToTypesPipeline) SchemaTransformations(passes ...compiler.Pass) *SchemaToTypesPipeline
SchemaTransformations adds the given transformations to the set of transformations that will be applied to the input schema.
func (*SchemaToTypesPipeline) Typescript ¶
func (pipeline *SchemaToTypesPipeline) Typescript(config TypescriptConfig) *SchemaToTypesPipeline
Typescript sets the output to Typescript types.
type TypescriptConfig ¶ added in v0.0.2
type TypescriptConfig struct {
// ImportsMap associates package names to their import path.
ImportsMap map[string]string
// EnumsAsUnionTypes generates enums as a union of values instead of using
// an actual `enum` declaration.
// If EnumsAsUnionTypes is false, an enum will be generated as:
// “`ts
// enum Direction {
// Up = "up",
// Down = "down",
// Left = "left",
// Right = "right",
// }
// “`
// If EnumsAsUnionTypes is true, the same enum will be generated as:
// “`ts
// type Direction = "up" | "down" | "left" | "right";
// “`
EnumsAsUnionTypes bool `yaml:"enums_as_union_types"`
}
TypescriptConfig defines a set of configuration options specific to Go outputs.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
cli
command
|
|
|
cog-config-schemas
command
|
|
|
compiler-passes-docs
command
|
|
|
examples
|
|
|
_as_library
command
|
|
|
_go
command
|
|
|
_go_alerting
command
|
|
|
internal
|
|