diff --git a/cmd/cue/cmd/testdata/script/cmd_tags.txtar b/cmd/cue/cmd/testdata/script/cmd_tags.txtar index e23c0bf0a..0847db19f 100644 --- a/cmd/cue/cmd/testdata/script/cmd_tags.txtar +++ b/cmd/cue/cmd/testdata/script/cmd_tags.txtar @@ -27,15 +27,13 @@ stderr 'no tag for "nosuchname"' ! exec cue cmd -t env=notadisjunct tag stderr '^data\.env: 2 errors in empty disjunction' ! exec cue cmd -t enable=notabool tag -stderr 'invalid boolean value "notabool" for injection tag "enable"' -# TODO: this error message seems wrong, it should say "invalid integer value" +stderr 'invalid boolean "notabool" for injection tag "enable"' ! exec cue cmd -t count=notanint tag -stderr '^data\.count: reference "notanint" not found' -# TODO: we should reject a float when type=int with a clear error +stderr 'invalid number for injection tag "count": illegal number start "notanint"' ! exec cue cmd -t count=12.34 tag -stderr 'cannot convert incomplete value' +stderr 'invalid int "12.34" for injection tag "count"' ! exec cue cmd -t toolenable=notabool tag -stderr 'invalid boolean value "notabool" for injection tag "toolenable"' +stderr 'invalid boolean "notabool" for injection tag "toolenable"' -- expect-stdout -- data.json: {"env":"prod","name":"bar","enable":true,"count":5,"mult":1.5} diff --git a/internal/cli/cli.go b/internal/cli/cli.go index d4702e4cc..aee8a901e 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -20,7 +20,7 @@ import ( "cuelang.org/go/cue" "cuelang.org/go/cue/ast" "cuelang.org/go/cue/errors" - "cuelang.org/go/cue/parser" + "cuelang.org/go/cue/literal" "cuelang.org/go/cue/token" ) @@ -28,11 +28,18 @@ func ParseValue(pos token.Pos, name, str string, k cue.Kind) (x ast.Expr, errs e var expr ast.Expr if k&cue.NumberKind != 0 { - var err error - expr, err = parser.ParseExpr(name, str) - if err != nil { + var info literal.NumInfo + if err := literal.ParseNum(str, &info); err != nil { + // Note that the wrapped err already mentions str. errs = errors.Wrapf(err, pos, "invalid number for injection tag %q", name) + } else if info.IsInt() { + expr = ast.NewLit(token.INT, str) + } else if k&cue.FloatKind == 0 { + errs = errors.Newf(pos, + "invalid int %q for injection tag %q", str, name) + } else { + expr = ast.NewLit(token.FLOAT, str) } } @@ -41,7 +48,7 @@ func ParseValue(pos token.Pos, name, str string, k cue.Kind) (x ast.Expr, errs e b, ok := boolValues[str] if !ok { errs = errors.Append(errs, errors.Newf(pos, - "invalid boolean value %q for injection tag %q", str, name)) + "invalid boolean %q for injection tag %q", str, name)) } else if expr != nil || k&cue.StringKind != 0 { // Convert into an expression bl := ast.NewBool(b)