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

Commit

Permalink
internal/encoding: pass schema to config
Browse files Browse the repository at this point in the history
The idea here is as follows:
When unifying a bag of CUE and non-CUE, CUE can
function as a template for non-CUE. For instance, an
`int` CUE field could allow JSON string to be interpreted
as a number (as is the case for Protobuf).

Note that for Protobuf this is imperative, as a proto
buffers cannot be parsed without a schema.

Issue #5

Change-Id: I9a70c2e8489e6f7f3c26ee90c2fdb1d65ab6e473
  • Loading branch information
mpvl committed Apr 6, 2021
1 parent 0dc7f24 commit 99023e9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 7 additions & 7 deletions cmd/cue/cmd/common.go
Expand Up @@ -203,7 +203,6 @@ func (i *instanceIterator) id() string { return i.a[i.i].Dir }
type streamingIterator struct {
r *cue.Runtime
inst *cue.Instance
base cue.Value
b *buildPlan
cfg *encoding.Config
a []decoderInfo
Expand Down Expand Up @@ -232,12 +231,13 @@ func newStreamingIterator(b *buildPlan) *streamingIterator {
}
i.r = internal.GetRuntime(inst).(*cue.Runtime)
if b.schema == nil {
i.base = inst.Value()
b.encConfig.Schema = inst.Value()
} else {
i.base = inst.Eval(b.schema)
if err := i.base.Err(); err != nil {
schema := inst.Eval(b.schema)
if err := schema.Err(); err != nil {
return &streamingIterator{e: err}
}
b.encConfig.Schema = schema
}
default:
return &streamingIterator{e: errors.Newf(token.NoPos,
Expand Down Expand Up @@ -293,10 +293,10 @@ func (i *streamingIterator) scan() bool {
return false
}
i.v = inst.Value()
if i.base.Exists() {
i.e = i.base.Err()
if schema := i.b.encConfig.Schema; schema.Exists() {
i.e = schema.Err()
if i.e == nil {
i.v = i.v.Unify(i.base)
i.v = i.v.Unify(schema)
i.e = i.v.Err()
}
i.f = nil
Expand Down
2 changes: 2 additions & 0 deletions internal/encoding/encoding.go
Expand Up @@ -143,6 +143,8 @@ type Config struct {
Stream bool // will potentially write more than one document per file
AllErrors bool

Schema cue.Value // used for schema-based decoding

EscapeHTML bool
ProtoPath []string
Format []format.Option
Expand Down

0 comments on commit 99023e9

Please sign in to comment.