encoding/openapi: update to new-style definitions

Note that the CUE API is not insufficient to handle new-style
definitions, as it can't handle non-definitions that start with a `#`.
 For now this will have to do.

Change-Id: I2f37457aa0647d070624fdf4744f4801da6ae772
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6001
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/encoding/openapi/build.go b/encoding/openapi/build.go
index 1c8b1b6..05efb62 100644
--- a/encoding/openapi/build.go
+++ b/encoding/openapi/build.go
@@ -117,6 +117,9 @@
 		if c.isInternal(label) {
 			continue
 		}
+		if i.IsDefinition() && strings.HasPrefix(label, "#") {
+			label = label[1:]
+		}
 		ref := c.makeRef(inst, []string{label})
 		if ref == "" {
 			continue
@@ -712,6 +715,9 @@
 		if b.ctx.isInternal(label) {
 			continue
 		}
+		if i.IsDefinition() && strings.HasPrefix(label, "#") {
+			label = label[1:]
+		}
 		var core *builder
 		if b.core != nil {
 			core = b.core.properties[label]
@@ -1198,6 +1204,12 @@
 }
 
 func (b *buildContext) makeRef(inst *cue.Instance, ref []string) string {
+	ref = append([]string{}, ref...)
+	for i, s := range ref {
+		if strings.HasPrefix(s, "#") {
+			ref[i] = s[1:]
+		}
+	}
 	a := make([]string, 0, len(ref)+3)
 	if b.nameFunc != nil {
 		a = append(a, b.nameFunc(inst, ref))
diff --git a/encoding/openapi/testdata/array.cue b/encoding/openapi/testdata/array.cue
index 10cade0..4ef6256 100644
--- a/encoding/openapi/testdata/array.cue
+++ b/encoding/openapi/testdata/array.cue
@@ -1,25 +1,25 @@
 import "list"
 
-Arrays :: {
-	bar?: [...MyEnum]
-	foo?: [...MyStruct]
+#Arrays: {
+	bar?: [...#MyEnum]
+	foo?: [...#MyStruct]
 
 	baz?: list.UniqueItems()
 
 	qux?: list.MinItems(1) & list.MaxItems(3)
 }
 
-Arrays :: {
-	bar?: [...MyEnum]
-	foo?: [...MyStruct]
+#Arrays: {
+	bar?: [...#MyEnum]
+	foo?: [...#MyStruct]
 }
 
 // MyStruct
-MyStruct :: {
+#MyStruct: {
 	a?: int
-	e?: [...MyEnum]
-	e?: [...MyEnum]
+	e?: [...#MyEnum]
+	e?: [...#MyEnum]
 }
 
 // MyEnum
-MyEnum :: *"1" | "2" | "3"
+#MyEnum: *"1" | "2" | "3"
diff --git a/encoding/openapi/testdata/builtins.cue b/encoding/openapi/testdata/builtins.cue
index 4d1b041..cd51239 100644
--- a/encoding/openapi/testdata/builtins.cue
+++ b/encoding/openapi/testdata/builtins.cue
@@ -5,7 +5,7 @@
 
 let _time = time
 
-MyStruct :: {
+#MyStruct: {
 	timestamp1?: time.Time
 	timestamp2?: time.Time()
 	timestamp3?: time.Format(time.RFC3339Nano)
diff --git a/encoding/openapi/testdata/issue131.cue b/encoding/openapi/testdata/issue131.cue
index 5a8dd88..3d07c8e 100644
--- a/encoding/openapi/testdata/issue131.cue
+++ b/encoding/openapi/testdata/issue131.cue
@@ -2,6 +2,6 @@
 
 import "example.com/blocks"
 
-Blocks :: {
-	block1: blocks.Block
+#Blocks: {
+	block1: blocks.#Block
 }
diff --git a/encoding/openapi/testdata/nested.cue b/encoding/openapi/testdata/nested.cue
index cbdfb36..3de5f28 100644
--- a/encoding/openapi/testdata/nested.cue
+++ b/encoding/openapi/testdata/nested.cue
@@ -1,11 +1,11 @@
 // File comment.
 
-Struct :: {
-	T :: int
+#Struct: {
+	#T: int
 
-	a?: T
+	a?: #T
 
-	{b?: T}
+	{b?: #T}
 
-	c?: [...T]
+	c?: [...#T]
 }
diff --git a/encoding/openapi/testdata/nums.cue b/encoding/openapi/testdata/nums.cue
index 02b7c15..c765945 100644
--- a/encoding/openapi/testdata/nums.cue
+++ b/encoding/openapi/testdata/nums.cue
@@ -1,5 +1,5 @@
 import "math"
 
-mul :: math.MultipleOf(5)
+#mul: math.MultipleOf(5)
 
-neq :: !=4
+#neq: !=4
diff --git a/encoding/openapi/testdata/oneof.cue b/encoding/openapi/testdata/oneof.cue
index 95935ac..55431f0 100644
--- a/encoding/openapi/testdata/oneof.cue
+++ b/encoding/openapi/testdata/oneof.cue
@@ -2,32 +2,32 @@
 
 $version: "v1alpha1"
 
-T :: {
+#T: {
 	shared: int
 }
-T :: {} | {
+#T: {} | {
 	exact: string
 } | {
 	regex: string
 }
-T :: {} | {
+#T: {} | {
 	count: int
 } | {
 	amount: int
 }
-T :: {
+#T: {
 	shared2: int
 }
 
-MyInt :: int
+#MyInt: int
 
-Foo :: {
-	include: T
-	exclude: [...T]
-	count: MyInt
+#Foo: {
+	include: #T
+	exclude: [...#T]
+	count: #MyInt
 }
 
-Incompatible :: {
+#Incompatible: {
 	shared: int
 } | {
 	shared: int
@@ -37,7 +37,7 @@
 	extra2: int
 }
 
-WithMap :: {
+#WithMap: {
 	shared: [string]: int
 } | {
 	shared: [string]: int
@@ -47,17 +47,17 @@
 	extra:  int
 }
 
-Embed :: {
+#Embed: {
 	a?: int
 
 	close({}) |
-	close({b: T}) |
+	close({b: #T}) |
 	close({c: int})
 
-	T :: {b?: int}
+	#T: {b?: int}
 
 	close({}) |
-	close({d: T}) |
+	close({d: #T}) |
 	close({e: int})
 
 	// TODO: maybe support builtin to write this as
diff --git a/encoding/openapi/testdata/openapi.cue b/encoding/openapi/testdata/openapi.cue
index 9295d70..83d81c1 100644
--- a/encoding/openapi/testdata/openapi.cue
+++ b/encoding/openapi/testdata/openapi.cue
@@ -9,38 +9,38 @@
 }
 
 // MyMessage is my message.
-MyMessage :: {
-	port?: Port & {} @protobuf(1)
+#MyMessage: {
+	port?: #Port & {} @protobuf(1)
 
-	foo: Int32 & >10 & <1000 & int32 @protobuf(2)
+	foo: #Int32 & >10 & <1000 & int32 @protobuf(2)
 
 	bar: [...string] @protobuf(3)
 }
 
-MyMessage :: {
+#MyMessage: {
 	// Field a.
 	a: 1
 } | {
 	b: string //2: crash
 }
 
-YourMessage :: ({a: number} | {b: string} | {b: number}) & {a?: string}
+#YourMessage: ({a: number} | {b: string} | {b: number}) & {a?: string}
 
-YourMessage2 :: ({a: number} | {b: number}) &
+#YourMessage2: ({a: number} | {b: number}) &
 	({c: number} | {d: number}) &
 	({e: number} | {f: number})
 
-Msg2 :: {b: number} | {a: string}
+#Msg2: {b: number} | {a: string}
 
-Int32 :: int32
+#Int32: int32
 
-Enum :: "foo" | "bar" | "baz"
+#Enum: "foo" | "bar" | "baz"
 
-List :: [...number] | *[1, 2, 3]
+#List: [...number] | *[1, 2, 3]
 
-DefaultStruct :: Port | *{port: 1}
+#DefaultStruct: #Port | *{port: 1}
 
-Port :: {
+#Port: {
 	port: int
 
 	obj: [...int]
diff --git a/encoding/openapi/testdata/pkg/example.com/blocks/blocks.cue b/encoding/openapi/testdata/pkg/example.com/blocks/blocks.cue
index fe9b8ce..87e22b9 100644
--- a/encoding/openapi/testdata/pkg/example.com/blocks/blocks.cue
+++ b/encoding/openapi/testdata/pkg/example.com/blocks/blocks.cue
@@ -1,6 +1,6 @@
 package blocks
 
-Block :: {
+#Block: {
 	a: >50
 	b: <10
 }
diff --git a/encoding/openapi/testdata/refs.cue b/encoding/openapi/testdata/refs.cue
index d69e174..970fd6d 100644
--- a/encoding/openapi/testdata/refs.cue
+++ b/encoding/openapi/testdata/refs.cue
@@ -1,13 +1,13 @@
-Keep :: {
+#Keep: {
 	// This comment is included
-	excludedStruct: ExcludedStruct
-	excludedInt:    ExcludedInt
+	excludedStruct: #ExcludedStruct
+	excludedInt:    #ExcludedInt
 }
 
 // ExcludedStruct is not included in the output.
-ExcludedStruct :: {
+#ExcludedStruct: {
 	A: int
 }
 
 // ExcludedInt is not included in the output.
-ExcludedInt :: int
+#ExcludedInt: int
diff --git a/encoding/openapi/testdata/simple.cue b/encoding/openapi/testdata/simple.cue
index 689490c..92b8475 100644
--- a/encoding/openapi/testdata/simple.cue
+++ b/encoding/openapi/testdata/simple.cue
@@ -1,4 +1,4 @@
-MyStruct :: {
+#MyStruct: {
 	mediumNum: int32
 	smallNum:  int8
 
diff --git a/encoding/openapi/testdata/strings.cue b/encoding/openapi/testdata/strings.cue
index 74ea59e..84e47c7 100644
--- a/encoding/openapi/testdata/strings.cue
+++ b/encoding/openapi/testdata/strings.cue
@@ -1,6 +1,6 @@
 import "strings"
 
-MyType :: {
+#MyType: {
 	myString: strings.MinRunes(1) & strings.MaxRunes(5)
 
 	myPattern: =~"foo.*bar"
diff --git a/encoding/openapi/testdata/struct.cue b/encoding/openapi/testdata/struct.cue
index 0000954..82406f8 100644
--- a/encoding/openapi/testdata/struct.cue
+++ b/encoding/openapi/testdata/struct.cue
@@ -1,8 +1,8 @@
 import "struct"
 
-MyMap :: struct.MinFields(4)
-MyMap :: struct.MaxFields(9)
+#MyMap: struct.MinFields(4)
+#MyMap: struct.MaxFields(9)
 
-MyType :: {
-	map: MyMap
+#MyType: {
+	map: #MyMap
 }
diff --git a/encoding/openapi/testdata/structural.cue b/encoding/openapi/testdata/structural.cue
index 9202681..1f0b892 100644
--- a/encoding/openapi/testdata/structural.cue
+++ b/encoding/openapi/testdata/structural.cue
@@ -1,16 +1,16 @@
 import "time"
 
-Attributes :: {
+#Attributes: {
 	//  A map of attribute name to its value.
 	attributes: {
-		[string]: AttrValue
+		[string]: #AttrValue
 	}
 }
 
 //  The attribute value.
-AttrValue :: {}
+#AttrValue: {}
 
-AttrValue :: {
+#AttrValue: {
 	//  Used for values of type STRING, DNS_NAME, EMAIL_ADDRESS, and URI
 	stringValue: string @protobuf(2,name=string_value)
 } | {
@@ -33,10 +33,10 @@
 	durationValue: time.Duration @protobuf(8,type=google.protobuf.Duration,name=duration_value)
 } | {
 	//  Used for values of type STRING_MAP
-	stringMapValue: Attributes_StringMap @protobuf(9,type=StringMap,name=string_map_value)
+	stringMapValue: #Attributes_StringMap @protobuf(9,type=StringMap,name=string_map_value)
 }
 
-Attributes_StringMap :: {
+#Attributes_StringMap: {
 	//  Holds a set of name/value pairs.
 	entries: {
 		[string]: string