Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cue: support runnable examples as part of package documentation #3660

Open
mvdan opened this issue Jan 8, 2025 · 0 comments
Open

cue: support runnable examples as part of package documentation #3660

mvdan opened this issue Jan 8, 2025 · 0 comments

Comments

@mvdan
Copy link
Member

mvdan commented Jan 8, 2025

Go has support for examples (https://pkg.go.dev/testing#hdr-Examples) as part of package docs, and they can be runnable or testable as well, meaning that running the code gives some output - and this output is shown with the docs, and verified to be true when one runs go test.

For instance, see one rendered runnable example of ours here: https://pkg.go.dev/cuelang.org/[email protected]/cue#example-Context

Examples are really useful in documentation, and examples which can be run and verified are even more useful - as they don't go stale, and the user can immediately take the code and try it out. We should support them in CUE too.

One rough idea is to copy or reuse some of the playground's design (https://cuelang.org/play) given that its inputs are:

  • which function to be run (export, def, validate, etc)
  • which input files, and in what format

So, here's a strawman for what an example could look like:

-- schema.cue --
package p

#Person: {
    name!: string
    age!: int & >= 0 & <120
}
-- example.cue --
package p

#Person: {
    name!: string
    age!: int & >= 0 & <120
}

// A valid person conforms to the schema.
_examples: validate: success: validPerson: {
    schema:  #Person
    data:    {name: "John", age: 29}
}

_examples: validate: fail: missingPersonAge: {
    schema: #Person
    data:   {name: "John"}
    error: "age: field is required but not present"
}

For runnable examples in package docs for the central registry, it's probably necessary to support dependency resolution too, so that we can e.g. have runnable examples with k8s schemas from packages which themselves have dependencies on other packages or modules, following their cue.mod.

@myitcv myitcv changed the title support runnable examples as part of package documentation cue: support runnable examples as part of package documentation Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant