cue: cannot get the position (line and column) of a CUE value, only the key position is tracked
What version of CUE are you using (cue version)?
v0.10.0
Does this issue reproduce with the latest stable release?
Yes
What did you do?
I have a JSON file:
{
"setting": "value"
}
I parsed the JSON into CUE and got a cue.Value. This is stored in a variable called jsonCue.
What did you expect to see?
When using jsonCue.Value().Pos().Line() and jsonCue.Value().Pos().Column(), I am expecting to see 2:15 (the start position of the value)
What did you see instead?
I see 2:4 (the start position of the key)
NOTE: When the value is a list, the positions of each list element are correctly tracked.
@as09033 thanks for reporting. I suspect what is going on here is that position information is not being properly accounted for in the conversion from an ast.Expr to a cue.Value. Here is the repro I pulled together (would be helpful if you had a repro we could reference):
go mod tidy
go run .
cmp stdout stdout.golden
-- go.mod --
module mod.example
go 1.23.2
require cuelang.org/go v0.11.1
-- main.go --
package main
import (
"fmt"
"log"
"os"
"text/tabwriter"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/encoding/json"
)
func main() {
ctx := cuecontext.New()
contents, err := os.ReadFile("data.json")
if err != nil {
log.Fatal(err)
}
expr, err := json.Extract("data.json", contents)
if err != nil {
log.Fatal(err)
}
v := ctx.BuildExpr(expr)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
printPos := func(v cue.Value, label string) {
fmt.Fprintf(w, "%s:\t%v\n", label, v.Pos())
_, operands := v.Expr()
if len(operands) == 0 {
log.Fatalf("no operands for %v", v)
}
o := operands[0]
fmt.Fprintf(w, "%s expr:\t%v\n", label, o.Pos())
}
printPos(v, ".")
setting := v.LookupPath(cue.ParsePath("setting"))
printPos(setting, "setting")
w.Flush()
}
-- data.json --
{
"setting": "value"
}
-- stdout.golden --
.: data.json:1:1
. expr: data.json:1:1
setting: data.json:2:5
setting expr: data.json:2:16
The tricky thing is that, in general, a cue.Value is the evaluated result of potentially a number of inputs. Indeed, the resulting value of a field can be the result of a number of inputs. This is reflected via the cue.Value.Expr method, which allows you to decompose a value to its component parts.
Even then, I'm not clear that we have sufficient API, given a cue.Value that represents an irreducible component part, to give the position of the input label (LHS) separately from the value (RHS).
In the first instance however it would be useful if you could confirm via a repro what you are trying to do here.
In the first instance however it would be useful if you could confirm via a repro what you are trying to do here.
Just checking, @as09033, whether you saw that question and are able to answer it?