internal/encoding: pass schema to config

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
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9321
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go
index 6163ac3..c1b1ed7 100644
--- a/cmd/cue/cmd/common.go
+++ b/cmd/cue/cmd/common.go
@@ -203,7 +203,6 @@
 type streamingIterator struct {
 	r    *cue.Runtime
 	inst *cue.Instance
-	base cue.Value
 	b    *buildPlan
 	cfg  *encoding.Config
 	a    []*decoderInfo
@@ -232,12 +231,13 @@
 		}
 		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,
@@ -293,10 +293,10 @@
 		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
diff --git a/internal/encoding/encoding.go b/internal/encoding/encoding.go
index 8606bcb..67ad181 100644
--- a/internal/encoding/encoding.go
+++ b/internal/encoding/encoding.go
@@ -143,6 +143,8 @@
 	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