cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.14k stars 294 forks source link

cue/go: cuevalue.Fields report non-concrete value struct #2047

Open FogDong opened 2 years ago

FogDong commented 2 years ago

What version of CUE are you using (cue version)?

$ cue version
 v0.5.0-alpha.1.0.20221022113314-dd1169c686c9

Does this issue reproduce with the latest release?

yes

What did you do?

Here's the repro:

# tip
go mod tidy
go run main.go

-- go.mod --
module test-cue/openapi-resolve

go 1.19

require cuelang.org/go v0.5.0-alpha.1.0.20221022113314-dd1169c686c9

require (
    github.com/cockroachdb/apd/v2 v2.0.2 // indirect
    github.com/google/uuid v1.2.0 // indirect
    github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de // indirect
    github.com/pkg/errors v0.8.1 // indirect
    golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
    golang.org/x/text v0.3.8 // indirect
    gopkg.in/yaml.v3 v3.0.1 // indirect
)
-- main.go --
package main

import (
    "fmt"

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

var test = `
useless: string

#Params: {
    image: string
}

parameter: #Params | {
    containers: [...#Params]
}
`

func main() {
    cueV := cuecontext.New().CompileString(test)
    v := cueV.LookupPath(cue.ParsePath("parameter"))
    b, err := format.Node(v.Syntax())
    if err != nil {
        panic(err)
    }
    fmt.Println(string(b))
    _, err = v.Fields()
    if err != nil {
        panic(err)
    }
}

What did you see instead?

> go run main.go
[stdout]
{
        PARAMS.#x | {
                containers: [...PARAMS.#x]
        }

        //cue:path: #Params
        let PARAMS = {
                #x: {
                        image: string
                }
        }
}

[stderr]
panic: parameter: non-concrete value struct
myitcv commented 1 year ago

This is a blocker for KubeVela. CUE Team to investigate.

FogDong commented 1 year ago

This one's priority is high because there's no workaround. I'll say this is a user-facing problem since we're generating docs from cue parameters for our users, and this issue blocks the generator. Hope this can be fixed in v0.6.

previousdeveloper commented 1 year ago

+1

myitcv commented 1 year ago

@FogDong apologies for the delay in replying here.

To build on @mpvl's answer.

parameter is not concrete as it is a disjunction.

To build on this, the critical point is that because parameter is a disjunction (that cannot be resolved to a single item) it is, by definition, not a struct value. Even though it is a disjunction of two struct values, it is still ambiguous as to what Fields() could/should return in this situation.

AFAICT, you did not specify what you expected to see.

To build on this too, we need to understand what list of fields you're expecting to see in this situation and why.

mpvl commented 1 year ago

@FogDong: Could you specify what you expected to see in this case? AFAICT, the output is correct.