Skip to content

Commit

Permalink
encoding/openapi: remove OrderedMap.Set
Browse files Browse the repository at this point in the history
Both the receiver and method were deprecated for years,
and its only internal use could be directly replaced by setExpr.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I15d0f66e35e91832065b54f7cda26954d94bea49
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201936
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
  • Loading branch information
mvdan committed Sep 30, 2024
1 parent e7d9d47 commit 3fb46c3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 35 deletions.
20 changes: 10 additions & 10 deletions encoding/openapi/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func schemas(g *Generator, inst cue.InstanceOrValue) (schemas *ast.StructLit, er
if ref == "" {
continue
}
c.schemas.Set(ref, c.build(sel, i.Value()))
c.schemas.setExpr(ref, c.build(sel, i.Value()))
}

// keep looping until a fixed point is reached.
Expand All @@ -168,7 +168,7 @@ func schemas(g *Generator, inst cue.InstanceOrValue) (schemas *ast.StructLit, er
last := len(sels) - 1
c.path = sels[:last]
name := sels[last]
c.schemas.Set(ext.ref, c.build(name, cue.Dereference(ext.value)))
c.schemas.setExpr(ext.ref, c.build(name, cue.Dereference(ext.value)))
}
}

Expand Down Expand Up @@ -752,9 +752,9 @@ func (b *builder) object(v cue.Value) {
if ref == "" {
continue
}
b.ctx.schemas.Set(ref, schema)
b.ctx.schemas.setExpr(ref, schema)
case !b.isNonCore() || len(schema.Elts) > 0:
properties.Set(label, schema)
properties.setExpr(label, schema)
}
}

Expand Down Expand Up @@ -1127,13 +1127,13 @@ func setType(t *oaSchema, b *builder) {
if b.typ != "" {
if b.core == nil || (b.core.typ != b.typ && !b.ctx.structural) {
if !t.exists("type") {
t.Set("type", ast.NewString(b.typ))
t.setExpr("type", ast.NewString(b.typ))
}
}
}
if b.format != "" {
if b.core == nil || b.core.format != b.format {
t.Set("format", ast.NewString(b.format))
t.setExpr("format", ast.NewString(b.format))
}
}
}
Expand All @@ -1156,7 +1156,7 @@ func (b *builder) setSingle(key string, v ast.Expr, drop bool) {
b.failf(cue.Value{}, "more than one value added for key %q", key)
}
}
b.singleFields.Set(key, v)
b.singleFields.setExpr(key, v)
}

func (b *builder) set(key string, v ast.Expr) {
Expand All @@ -1167,7 +1167,7 @@ func (b *builder) set(key string, v ast.Expr) {
b.current = &OrderedMap{}
b.allOf = append(b.allOf, (*ast.StructLit)(b.current))
}
b.current.Set(key, v)
b.current.setExpr(key, v)
}

func (b *builder) kv(key string, value ast.Expr) *ast.StructLit {
Expand Down Expand Up @@ -1208,14 +1208,14 @@ func (b *builder) finish() *ast.StructLit {
exprs = append(exprs, s)
}
t = &OrderedMap{}
t.Set("allOf", ast.NewList(exprs...))
t.setExpr("allOf", ast.NewList(exprs...))
}
if b.singleFields != nil {
b.singleFields.Elts = append(b.singleFields.Elts, t.Elts...)
t = b.singleFields
}
if b.deprecated {
t.Set("deprecated", ast.NewBool(true))
t.setExpr("deprecated", ast.NewBool(true))
}
setType(t, b)
sortSchema((*ast.StructLit)(t))
Expand Down
2 changes: 1 addition & 1 deletion encoding/openapi/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (b *builder) coreSchema() *ast.StructLit {
p := &OrderedMap{}
for _, k := range b.keys {
sub := b.properties[k]
p.Set(k, sub.coreSchemaWithName(cue.Str(k)))
p.setExpr(k, sub.coreSchemaWithName(cue.Str(k)))
}
if p.len() > 0 || b.items != nil {
b.setType("object", "")
Expand Down
4 changes: 2 additions & 2 deletions encoding/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ func (c *Config) compose(inst cue.InstanceOrValue, schemas *ast.StructLit) (x *a
)
} else {
m := (*OrderedMap)(info)
m.Set("title", ast.NewString(title))
m.Set("version", ast.NewString(version))
m.setExpr("title", ast.NewString(title))
m.setExpr("version", ast.NewString(version))
}

case *ast.StructLit:
Expand Down
22 changes: 0 additions & 22 deletions encoding/openapi/orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,6 @@ func (m *OrderedMap) find(key string) *ast.Field {
return nil
}

// 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. The value must be of type string, [ast.Expr], or [*OrderedMap].
//
// Deprecated: use cuelang.org/go/cue/ast to manipulate ASTs.
func (m *OrderedMap) Set(key string, x interface{}) {
switch x := x.(type) {
case *OrderedMap:
m.setExpr(key, (*ast.StructLit)(x))
case string:
m.setExpr(key, ast.NewString(x))
case ast.Expr:
m.setExpr(key, x)
default:
v, err := toCUE("Set", x)
if err != nil {
panic(err)
}
m.setExpr(key, v)
}
}

func (m *OrderedMap) setExpr(key string, expr ast.Expr) {
if f := m.find(key); f != nil {
f.Value = expr
Expand Down

0 comments on commit 3fb46c3

Please sign in to comment.