From 7a26789061a3895fafffb4c036f1c48ae145a888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 11 Sep 2024 15:44:28 +0100 Subject: [PATCH] pkg: do not treat unknown Go types as adt.TopKind MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We had one of these cases with pkg/time.Split, which returned the type Parts, which is a struct. Declaring the result kind as adt.TopKind works, but declaring it as adt.StructKind is more precise. We no longer have a catch-all default, forcing us to handle all types in the best way we can, much like adtKind does now. Signed-off-by: Daniel Martí Change-Id: Ie890f45c0918e0c2f18c5a60679fecebf7133fb4 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201036 Reviewed-by: Roger Peppe TryBot-Result: CUEcueckoo Unity-Result: CUE porcuepine --- pkg/gen.go | 11 +++++++++-- pkg/time/pkg.go | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/gen.go b/pkg/gen.go index d126a9e294c..44738f79088 100644 --- a/pkg/gen.go +++ b/pkg/gen.go @@ -408,9 +408,16 @@ func (g *generator) callCtxtGetter(typ types.Type) string { case "io.Reader": return "Reader" case "error": - return "Bottom" // for [generator.cueTypeExpression] + return "Bottom" // for [generator.adtKind] + + // Some builtin functions return custom types, like [cuelang.org/go/pkg/time.Split]. + // TODO: we can simplify this once the CUE API declarations in ./pkg/... + // use CUE function signatures to validate their parameters and results. + case "*cuelang.org/go/pkg/time.Parts": + return "Struct" } - return "Value" // for [generator.cueTypeExpression] + log.Fatal("unknown Go type: ", typ.String()) + return "" } // adtKind provides a Go expression string which describes diff --git a/pkg/time/pkg.go b/pkg/time/pkg.go index 80a3a17effc..6ed8ff7aa50 100644 --- a/pkg/time/pkg.go +++ b/pkg/time/pkg.go @@ -233,7 +233,7 @@ var p = &pkg.Package{ Params: []pkg.Param{ {Kind: adt.StringKind}, }, - Result: adt.TopKind, + Result: adt.StructKind, Func: func(c *pkg.CallCtxt) { t := c.String(0) if c.Do() {