cue-lang / docs-and-content

A place to discuss, plan, and track documentation on cuelang.org
5 stars 1 forks source link

docs/???/???: Accessing non-concrete constraints with the Go API #113

Open jpluscplusm opened 3 months ago

jpluscplusm commented 3 months ago

Slack thread: https://cuelang.slack.com/archives/C012UU8B72M/p1709805267382589?thread_ts=1706266602.453679&cid=C012UU8B72M

Question:

I define the field labels as this labels: [string]: string Can I get the literal [string]: string somehow?

Solution:

go mod tidy
go run .
cmp stdout stdout.golden

-- go.mod --
module mod.example

go 1.22.1

require cuelang.org/go v0.7.1
-- main.go --
package main

import (
    "fmt"

    "cuelang.org/go/cue"
    "cuelang.org/go/cue/cuecontext"
)

const source = `
labels: {[ string]: string}
`

func main() {
    ctx := cuecontext.New()
    v := ctx.CompileString(source)
    labels := v.LookupPath(cue.ParsePath("labels"))
    fmt.Printf("%#v\n", labels)
}
-- stdout.golden --
[string]: string