From ba156b01b67970c25102928b037973c1c7ad7dca 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 Dispatch-Trailer: {"type":"trybot","CL":1201036,"patchset":3,"ref":"refs/changes/36/1201036/3","targetBranch":"master"} --- pkg/gen.go | 12 +++++++++++- pkg/time/pkg.go | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/gen.go b/pkg/gen.go index d126a9e29..979d9a1a7 100644 --- a/pkg/gen.go +++ b/pkg/gen.go @@ -410,7 +410,17 @@ func (g *generator) callCtxtGetter(typ types.Type) string { case "error": return "Bottom" // for [generator.cueTypeExpression] } - return "Value" // for [generator.cueTypeExpression] + // Some builtin functions return custom types, like [cuelang.org/go/pkg/time.Split]. + // Inspect the underlying type behind any pointer and return an appropriate shape. + if ptr, ok := typ.(*types.Pointer); ok { + typ = ptr.Elem() + } + switch typ.Underlying().(type) { + case *types.Struct: // e.g. [cuelang.org/go/pkg/time.Parts] + return "Struct" + } + 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 80a3a17ef..6ed8ff7aa 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() {