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

Commit

Permalink
encoding/openapi: fix title handling regression
Browse files Browse the repository at this point in the history
This regression was a result of forgetting to unskip the
def_openapi test in the move to v0.3, which allowed the
regression to be introduced by 304a3e0 to go unnoticed.

This partially unwinds 304a3e0: it still assumes the main
operation is based on Value, but if available, it also stores
the Instance so that its meta information can be extracted.

In principle all information available in the Instance is also
available in the auxiliary structures of the Value (conjuncts).
But for now this is an easier approach.

The tests have a few changes as a result. The updated meta
data is correct. Furthermore there are additional file comments
at the top. It is debatable whether the latter is desirable or not,
but we'll leave it for now, as it is the current behavior.

Change-Id: Iaf40a9e525ff582e24ab4bc9fe690a40fa146559
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9066
Reviewed-by: CUE cueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
mpvl committed Mar 21, 2021
1 parent d4cb2c8 commit fec7a9c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
22 changes: 13 additions & 9 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ func (b *buildPlan) instances() iterator {
type iterator interface {
scan() bool
value() cue.Value
file() *ast.File // may return nil
instance() *cue.Instance // may return nil
file() *ast.File // may return nil
err() error
close()
id() string
Expand All @@ -186,11 +187,12 @@ func (i *instanceIterator) scan() bool {
return i.i < len(i.a) && i.e == nil
}

func (i *instanceIterator) close() {}
func (i *instanceIterator) err() error { return i.e }
func (i *instanceIterator) value() cue.Value { return i.a[i.i].Value() }
func (i *instanceIterator) file() *ast.File { return nil }
func (i *instanceIterator) id() string { return i.a[i.i].Dir }
func (i *instanceIterator) close() {}
func (i *instanceIterator) err() error { return i.e }
func (i *instanceIterator) value() cue.Value { return i.a[i.i].Value() }
func (i *instanceIterator) instance() *cue.Instance { return i.a[i.i] }
func (i *instanceIterator) file() *ast.File { return nil }
func (i *instanceIterator) id() string { return i.a[i.i].Dir }

type streamingIterator struct {
r *cue.Runtime
Expand Down Expand Up @@ -239,8 +241,9 @@ func newStreamingIterator(b *buildPlan) *streamingIterator {
return i
}

func (i *streamingIterator) file() *ast.File { return i.f }
func (i *streamingIterator) value() cue.Value { return i.v }
func (i *streamingIterator) file() *ast.File { return i.f }
func (i *streamingIterator) value() cue.Value { return i.v }
func (i *streamingIterator) instance() *cue.Instance { return nil }

func (i *streamingIterator) id() string {
if i.inst != nil {
Expand Down Expand Up @@ -333,7 +336,8 @@ func (i *expressionIter) scan() bool {
return true
}

func (i *expressionIter) file() *ast.File { return nil }
func (i *expressionIter) file() *ast.File { return nil }
func (i *expressionIter) instance() *cue.Instance { return nil }

func (i *expressionIter) value() cue.Value {
if len(i.expr) == 0 {
Expand Down
10 changes: 6 additions & 4 deletions cmd/cue/cmd/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ func runDef(cmd *Command, args []string) error {
iter := b.instances()
defer iter.close()
for i := 0; iter.scan(); i++ {
var err error
if f := iter.file(); f != nil {
err := e.EncodeFile(f)
exitOnErr(cmd, err, true)
err = e.EncodeFile(f)
} else if i := iter.instance(); i != nil {
err = e.EncodeInstance(iter.instance())
} else {
err := e.Encode(iter.value())
exitOnErr(cmd, err, true)
err = e.Encode(iter.value())
}
exitOnErr(cmd, err, true)
}
exitOnErr(cmd, iter.err(), true)
return nil
Expand Down
9 changes: 5 additions & 4 deletions cmd/cue/cmd/testdata/script/def_openapi.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
skip 'fix comments and value printing'

cue def openapi+cue: expect-cue-out -o -

cue def foo.cue -o openapi:-
Expand Down Expand Up @@ -255,11 +253,14 @@ info: {
}
-- expect-cue3 --
// My OpenAPI

// My OpenAPI2
package foo

info: {
title: string | *_|_
version: *"v1alpha1" | string}
title: (*"My OpenAPI" | string) & (*"My OpenAPI2" | string)
version: (*"v1alpha1" | string) & (*"v1alpha1" | string)
}
#Bar: {
foo: #Foo
...
Expand Down
15 changes: 14 additions & 1 deletion internal/encoding/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Encoder struct {
encValue func(cue.Value) error
autoSimplify bool
concrete bool
instance *cue.Instance
}

// IsConcrete reports whether the output is required to be concrete.
Expand Down Expand Up @@ -80,7 +81,10 @@ func NewEncoder(f *build.File, cfg *Config) (*Encoder, error) {
// TODO: get encoding options
cfg := &openapi.Config{}
e.interpret = func(v cue.Value) (*ast.File, error) {
i := internal.MakeInstance(v).(*cue.Instance)
i := e.instance
if i == nil {
i = internal.MakeInstance(v).(*cue.Instance)
}
return openapi.Generate(i, cfg)
}
// case build.JSONSchema:
Expand Down Expand Up @@ -205,6 +209,15 @@ func (e *Encoder) EncodeFile(f *ast.File) error {
return e.encodeFile(f, e.interpret)
}

// EncodeInstance is as Encode, but stores instance information. This should
// all be retrievable from the value itself.
func (e *Encoder) EncodeInstance(v *cue.Instance) error {
e.instance = v
err := e.Encode(v.Value())
e.instance = nil
return err
}

func (e *Encoder) Encode(v cue.Value) error {
e.autoSimplify = true
if e.interpret != nil {
Expand Down

0 comments on commit fec7a9c

Please sign in to comment.