encoding/openapi: make OrderedMap type opaque

The OrderedMap needs to be a pointer, which may be
a bit unintuitive it being a slice. Change it to as struct.

Info is left as a non-pointer to indicate the non-optional
nature of it. The user of pointers is enforced by making
MarshalJSON only work for pointer types.

Change-Id: I8481dd8974825928815bfef3acd1eb5fc0274029
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2460
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/openapi/build.go b/encoding/openapi/build.go
index 9adfbe6..9b49b1f 100644
--- a/encoding/openapi/build.go
+++ b/encoding/openapi/build.go
@@ -56,7 +56,7 @@
 
 type typeFunc func(b *builder, a cue.Value)
 
-func schemas(g *Generator, inst *cue.Instance) (schemas OrderedMap, err error) {
+func schemas(g *Generator, inst *cue.Instance) (schemas *OrderedMap, err error) {
 	var fieldFilter *regexp.Regexp
 	if g.FieldFilter != "" {
 		fieldFilter, err = regexp.Compile(g.FieldFilter)
@@ -107,7 +107,7 @@
 		if c.isInternal(label) {
 			continue
 		}
-		c.schemas.set(c.makeRef(inst, []string{label}), c.build(label, i.Value()))
+		c.schemas.Set(c.makeRef(inst, []string{label}), c.build(label, i.Value()))
 	}
 
 	// keep looping until a fixed point is reached.
@@ -123,11 +123,11 @@
 
 		for _, k := range external {
 			ext := c.externalRefs[k]
-			c.schemas.set(ext.ref, c.build(ext.ref, ext.value.Eval()))
+			c.schemas.Set(ext.ref, c.build(ext.ref, ext.value.Eval()))
 		}
 	}
 
-	return *c.schemas, nil
+	return c.schemas, nil
 }
 
 func (c *buildContext) build(name string, v cue.Value) *oaSchema {
@@ -459,11 +459,11 @@
 		b.setFilter("Schema", "required", required)
 	}
 
-	properties := map[string]*oaSchema{}
+	properties := &OrderedMap{}
 	for i, _ := v.Fields(cue.Optional(true), cue.Hidden(false)); i.Next(); {
-		properties[i.Label()] = b.schema(i.Label(), i.Value())
+		properties.Set(i.Label(), b.schema(i.Label(), i.Value()))
 	}
-	if len(properties) > 0 {
+	if len(properties.kvs) > 0 {
 		b.set("properties", properties)
 	}
 
@@ -738,9 +738,9 @@
 
 func setType(t *oaSchema, b *builder) {
 	if b.typ != "" {
-		t.set("type", b.typ)
+		t.Set("type", b.typ)
 		if b.format != "" {
-			t.set("format", b.format)
+			t.Set("format", b.format)
 		}
 	}
 }
@@ -762,19 +762,19 @@
 		b.current = &OrderedMap{}
 		b.allOf = append(b.allOf, b.current)
 	}
-	b.current.set(key, v)
+	b.current.Set(key, v)
 }
 
 func (b *builder) kv(key string, value interface{}) *oaSchema {
 	constraint := &OrderedMap{}
 	setType(constraint, b)
-	constraint.set(key, value)
+	constraint.Set(key, value)
 	return constraint
 }
 
 func (b *builder) setNot(key string, value interface{}) {
 	not := &OrderedMap{}
-	not.set("not", b.kv(key, value))
+	not.Set("not", b.kv(key, value))
 	b.add(not)
 }
 
@@ -795,7 +795,7 @@
 
 	default:
 		t := &OrderedMap{}
-		t.set("allOf", b.allOf)
+		t.Set("allOf", b.allOf)
 		return t
 	}
 }