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

Commit

Permalink
cmd/cue/cmd: extract cue constraints in tags in get go
Browse files Browse the repository at this point in the history
Change-Id: I9bd0a2307a364c0ab9c8d9b3e8f69b8f162c29f6
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6341
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jun 16, 2020
1 parent eb7706b commit 019060d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions cmd/cue/cmd/get_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,14 @@ func (e *extractor) addFields(x *types.Struct, st *cueast.StructLit) {
field, cueType := e.makeField(name, kind, f.Type(), docs[i], count > 0)
add(field)

if s := reflect.StructTag(tag).Get("cue"); s != "" {
expr, err := parser.ParseExpr("get go", s)
if err != nil {
e.logf("error parsing struct tag %q:", s, err)
}
field.Value = cueast.NewBinExpr(cuetoken.AND, field.Value, expr)
}

// Add field tag to convert back to Go.
typeName := f.Type().String()
// simplify type names:
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/code/go/pkg1/file1.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Foozer struct {
AnyJSON json.Marshaler
AnyText encoding.TextMarshaler

Bar int `json:"bar,omitempty"`
Bar int `json:"bar,omitempty" cue:">10"`

exclude int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
CustomYAML?: null | #CustomYAML @go(,*CustomYAML)
AnyJSON: _ @go(,json.Marshaler)
AnyText: string @go(,encoding.TextMarshaler)
bar?: int @go(Bar)
bar?: int & >10 @go(Bar)

// Time is mapped to CUE's internal type.
Time: time.Time
Expand Down

0 comments on commit 019060d

Please sign in to comment.