encoding/openapi: define strict order on fields

This gives nicer results, but also makes the diffs
when supporting structural OpenAPI (for CRDs)
more pallatable.

The prepend option is then no longer necessary.

This also exposed a bug where the wrong fields
were uniqued in the simplify stage.

Change-Id: Ib73df14800447cf2d63adbd77da7fb01f3e73498
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2802
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/openapi/orderedmap.go b/encoding/openapi/orderedmap.go
index 91a2ec4..9e5113a 100644
--- a/encoding/openapi/orderedmap.go
+++ b/encoding/openapi/orderedmap.go
@@ -33,10 +33,6 @@
 	return m.kvs
 }
 
-func (m *OrderedMap) prepend(key string, value interface{}) {
-	m.kvs = append([]KeyValue{{key, value}}, m.kvs...)
-}
-
 // Set sets a key value pair. If a pair with the same key already existed, it
 // will be replaced with the new value. Otherwise, the new value is added to
 // the end.
@@ -66,6 +62,16 @@
 	return false
 }
 
+// exists reports whether a key-value pair exists for the given key.
+func (m *OrderedMap) getMap(key string) *OrderedMap {
+	for _, v := range m.kvs {
+		if v.Key == key {
+			return v.Value.(*OrderedMap)
+		}
+	}
+	return nil
+}
+
 // MarshalJSON implements json.Marshaler.
 func (m *OrderedMap) MarshalJSON() (b []byte, err error) {
 	// This is a pointer receiever to enforce that we only store pointers to
@@ -76,15 +82,15 @@
 		if i > 0 {
 			b = append(b, ",\n"...)
 		}
-		key, err := json.Marshal(v.Key)
-		if je, ok := err.(*json.MarshalerError); ok {
+		key, ferr := json.Marshal(v.Key)
+		if je, ok := ferr.(*json.MarshalerError); ok {
 			return nil, je.Err
 		}
 		b = append(b, key...)
 		b = append(b, ": "...)
 
 		value, jerr := json.Marshal(v.Value)
-		if je, ok := err.(*json.MarshalerError); ok {
+		if je, ok := jerr.(*json.MarshalerError); ok {
 			err = jerr
 			value, _ = json.Marshal(je.Err.Error())
 		}