Skip to content

Commit

Permalink
encoding/openapi: unexport OrderedMap
Browse files Browse the repository at this point in the history
Now that no other exported APIs directly reference OrderedMap,
and OrderedMap itself does not export any methods either,
start unexporting it after being deprecated for over four years.

We remove support from Config.Info, as end users can no longer
reference the OrderedMap type for use in that API.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I81be113edfe1a058e43c9f4f028d6891cec1044d
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201937
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
mvdan committed Sep 30, 2024
1 parent 3fb46c3 commit 9f2c2fe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 34 deletions.
30 changes: 14 additions & 16 deletions encoding/openapi/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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(); {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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") {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion encoding/openapi/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
7 changes: 1 addition & 6 deletions encoding/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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 {
Expand Down
19 changes: 8 additions & 11 deletions encoding/openapi/orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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))
}

0 comments on commit 9f2c2fe

Please sign in to comment.