diff --git a/encoding/openapi/build.go b/encoding/openapi/build.go index abdfa22af..30631aa2b 100644 --- a/encoding/openapi/build.go +++ b/encoding/openapi/build.go @@ -44,7 +44,7 @@ type buildContext struct { descFunc func(v cue.Value) string fieldFilter *regexp.Regexp - schemas *OrderedMap + schemas *orderedMap // Track external schemas. externalRefs map[string]*externalType @@ -70,8 +70,6 @@ type externalType struct { value cue.Value } -type oaSchema = OrderedMap - type typeFunc func(b *builder, a cue.Value) func schemas(g *Generator, inst cue.InstanceOrValue) (schemas *ast.StructLit, err error) { @@ -105,7 +103,7 @@ func schemas(g *Generator, inst cue.InstanceOrValue) (schemas *ast.StructLit, er structural: g.ExpandReferences, nameFunc: g.NameFunc, descFunc: g.DescriptionFunc, - schemas: &OrderedMap{}, + schemas: &orderedMap{}, externalRefs: map[string]*externalType{}, fieldFilter: fieldFilter, } @@ -726,13 +724,13 @@ func (b *builder) object(v cue.Value) { b.setFilter("Schema", "required", ast.NewList(required...)) } - var properties *OrderedMap + var properties *orderedMap if b.singleFields != nil { properties = b.singleFields.getMap("properties") } hasProps := properties != nil if !hasProps { - properties = &OrderedMap{} + properties = &orderedMap{} } for i, _ := v.Fields(cue.Optional(true), cue.Definitions(true)); i.Next(); { @@ -1077,8 +1075,8 @@ type builder struct { ctx *buildContext typ string format string - singleFields *oaSchema - current *oaSchema + singleFields *orderedMap + current *orderedMap allOf []*ast.StructLit deprecated bool @@ -1123,7 +1121,7 @@ func (b *builder) setType(t, format string) { } } -func setType(t *oaSchema, b *builder) { +func setType(t *orderedMap, b *builder) { if b.typ != "" { if b.core == nil || (b.core.typ != b.typ && !b.ctx.structural) { if !t.exists("type") { @@ -1149,7 +1147,7 @@ func (b *builder) setFilter(schema, key string, v ast.Expr) { // setSingle sets a value of which there should only be one. func (b *builder) setSingle(key string, v ast.Expr, drop bool) { if b.singleFields == nil { - b.singleFields = &OrderedMap{} + b.singleFields = &orderedMap{} } if b.singleFields.exists(key) { if !drop { @@ -1161,10 +1159,10 @@ func (b *builder) setSingle(key string, v ast.Expr, drop bool) { func (b *builder) set(key string, v ast.Expr) { if b.current == nil { - b.current = &OrderedMap{} + b.current = &orderedMap{} b.allOf = append(b.allOf, (*ast.StructLit)(b.current)) } else if b.current.exists(key) { - b.current = &OrderedMap{} + b.current = &orderedMap{} b.allOf = append(b.allOf, (*ast.StructLit)(b.current)) } b.current.setExpr(key, v) @@ -1179,14 +1177,14 @@ func (b *builder) setNot(key string, value ast.Expr) { } func (b *builder) finish() *ast.StructLit { - var t *OrderedMap + var t *orderedMap if b.filled != nil { return b.filled } switch len(b.allOf) { case 0: - t = &OrderedMap{} + t = &orderedMap{} case 1: hasRef := false @@ -1197,7 +1195,7 @@ func (b *builder) finish() *ast.StructLit { } } if !hasRef || b.singleFields == nil { - t = (*OrderedMap)(b.allOf[0]) + t = (*orderedMap)(b.allOf[0]) break } fallthrough @@ -1207,7 +1205,7 @@ func (b *builder) finish() *ast.StructLit { for _, s := range b.allOf { exprs = append(exprs, s) } - t = &OrderedMap{} + t = &orderedMap{} t.setExpr("allOf", ast.NewList(exprs...)) } if b.singleFields != nil { diff --git a/encoding/openapi/crd.go b/encoding/openapi/crd.go index b0c17d6ff..6e09adf49 100644 --- a/encoding/openapi/crd.go +++ b/encoding/openapi/crd.go @@ -74,7 +74,7 @@ func (b *builder) coreSchema() *ast.StructLit { } case cue.StructKind: - p := &OrderedMap{} + p := &orderedMap{} for _, k := range b.keys { sub := b.properties[k] p.setExpr(k, sub.coreSchemaWithName(cue.Str(k))) diff --git a/encoding/openapi/openapi.go b/encoding/openapi/openapi.go index 2e2e5f7e5..f0830858b 100644 --- a/encoding/openapi/openapi.go +++ b/encoding/openapi/openapi.go @@ -172,7 +172,6 @@ func (c *Config) compose(inst cue.InstanceOrValue, schemas *ast.StructLit) (x *a } } - // Support of OrderedMap is mostly for backwards compatibility. switch x := c.Info.(type) { case nil: if title == "" { @@ -196,17 +195,13 @@ func (c *Config) compose(inst cue.InstanceOrValue, schemas *ast.StructLit) (x *a "version", ast.NewString(version), ) } else { - m := (*OrderedMap)(info) + m := (*orderedMap)(info) m.setExpr("title", ast.NewString(title)) m.setExpr("version", ast.NewString(version)) } case *ast.StructLit: info = x - case *OrderedMap: - info = (*ast.StructLit)(x) - case OrderedMap: - info = (*ast.StructLit)(&x) default: x, err := toCUE("info section", x) if err != nil { diff --git a/encoding/openapi/orderedmap.go b/encoding/openapi/orderedmap.go index 1b07b8461..d6ddf5345 100644 --- a/encoding/openapi/orderedmap.go +++ b/encoding/openapi/orderedmap.go @@ -18,22 +18,19 @@ import ( "cuelang.org/go/cue/ast" ) -// An OrderedMap is a set of key-value pairs that preserves the order in which +// An orderedMap is a set of key-value pairs that preserves the order in which // items were added. // // Deprecated: the API now returns an ast.File. This allows OpenAPI to be // represented as JSON, YAML, or CUE data, in addition to being able to use // all the ast-related tooling. -type OrderedMap ast.StructLit +type orderedMap ast.StructLit -// TODO: these functions are here to support backwards compatibility with Istio. -// At some point, once this is removed from Istio, this can be removed. - -func (m *OrderedMap) len() int { +func (m *orderedMap) len() int { return len(m.Elts) } -func (m *OrderedMap) find(key string) *ast.Field { +func (m *orderedMap) find(key string) *ast.Field { for _, v := range m.Elts { f, ok := v.(*ast.Field) if !ok { @@ -47,7 +44,7 @@ func (m *OrderedMap) find(key string) *ast.Field { return nil } -func (m *OrderedMap) setExpr(key string, expr ast.Expr) { +func (m *orderedMap) setExpr(key string, expr ast.Expr) { if f := m.find(key); f != nil { f.Value = expr return @@ -59,15 +56,15 @@ func (m *OrderedMap) setExpr(key string, expr ast.Expr) { } // exists reports whether a key-value pair exists for the given key. -func (m *OrderedMap) exists(key string) bool { +func (m *orderedMap) exists(key string) bool { return m.find(key) != nil } // exists reports whether a key-value pair exists for the given key. -func (m *OrderedMap) getMap(key string) *OrderedMap { +func (m *orderedMap) getMap(key string) *orderedMap { f := m.find(key) if f == nil { return nil } - return (*OrderedMap)(f.Value.(*ast.StructLit)) + return (*orderedMap)(f.Value.(*ast.StructLit)) }