Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
encoding/gocode: make work for definitions
Browse files Browse the repository at this point in the history
The Go code could only be correlated with
values so far. Allow either.

Change-Id: I3ebf8fd532a66eb11fe933f8e40a8e28b8eb5c42
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4981
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Feb 17, 2020
1 parent 0afa377 commit 0308a53
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
19 changes: 16 additions & 3 deletions encoding/gocode/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func Generate(pkgPath string, inst *cue.Instance, c *Config) (b []byte, err erro
pkgName = g.pkg.Name

for _, obj := range g.pkg.TypesInfo.Defs {
if obj == nil || obj.Pkg() != g.pkg.Types {
if obj == nil || obj.Pkg() != g.pkg.Types || obj.Parent() == nil {
continue
}
g.typeMap[obj.Name()] = obj.Type()
Expand All @@ -152,7 +152,7 @@ func Generate(pkgPath string, inst *cue.Instance, c *Config) (b []byte, err erro
"pkgName": pkgName,
})

iter, err := inst.Value().Fields()
iter, err := inst.Value().Fields(cue.Definitions(true))
g.addErr(err)

for iter.Next() {
Expand Down Expand Up @@ -231,7 +231,10 @@ func (g *generator) decl(name string, v cue.Value) {

zero := "nil"

typ := g.typeMap[goTypeName]
typ, ok := g.typeMap[goTypeName]
if !ok && !mappedGoTypes(goTypeName) {
return
}
if goType == "" {
goType = goTypeName
if typ != nil {
Expand Down Expand Up @@ -276,3 +279,13 @@ func strValue(have, fallback string) string {
}
return have
}

func mappedGoTypes(s string) bool {
switch s {
case "bool", "float32", "float64",
"int", "int8", "int16", "int32", "int64", "string",
"uint", "uint8", "uint16", "uint32", "uint64":
return true
}
return false
}
7 changes: 4 additions & 3 deletions encoding/gocode/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import "text/template"
// Inputs:
// .pkgName the Go package name
var headerCode = template.Must(template.New("header").Parse(
`// Code generated by gogen.Generate; DO NOT EDIT.
`// Code generated by gocode.Generate; DO NOT EDIT.
package {{.pkgName}}
Expand Down Expand Up @@ -80,10 +80,11 @@ var {{.prefix}}Codec, {{.prefix}}Instance = func() (*gocodec.Codec, *cue.Instanc
// {{.prefix}}Make is called in the init phase to initialize CUE values for
// validation functions.
func {{.prefix}}Make(name string, x interface{}) cue.Value {
v := {{.prefix}}Instance.Lookup(name)
if !v.Exists() {
f, err := {{.prefix}}Instance.LookupField(name)
if err != nil {
panic(fmt.Errorf("could not find type %q in instance", name))
}
v := f.Value
if x != nil {
w, err := {{.prefix}}Codec.ExtractType(x)
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions encoding/gocode/testdata/pkg1/cue_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions encoding/gocode/testdata/pkg1/instance.cue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ String: !="" @go(,validate=ValidateCUE)

SpecialString: =~"special" @go(,type=string)

IgnoreThis: =~"foo" // No corresponding Go type

Omit: int @go(-)

// NonExisting will be omitted as there is no equivalent Go type.
Expand Down
7 changes: 4 additions & 3 deletions encoding/gocode/testdata/pkg2/cue_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0308a53

Please sign in to comment.