Skip to content

Commit e339345

Browse files
committed
encoding/openapi: drop support for Config.ReferenceFunc
It has been deprecated in favor of Config.NameFunc since https://cuelang.org/cl/542287 in August 2022. Since it has been two years with the deprecation notice, and switching from ReferenceFunc to NameFunc with feature parity is possible, and we want to transition away from cue.Instance, remove the option and tests entirely. Note that the tests using ReferenceFunc already have variants without the deprecated option. We can simplify the tests a bit too to only use the cue.Value entrypoint now, which gives us equal coverage. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Ic90024efd7bb771077e1bd6e5862f76de133437e Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201768 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 1b45c99 commit e339345

File tree

3 files changed

+11
-117
lines changed

3 files changed

+11
-117
lines changed

encoding/openapi/build.go

-33
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"cuelang.org/go/cue/token"
2929
"cuelang.org/go/internal"
3030
"cuelang.org/go/internal/core/adt"
31-
internalvalue "cuelang.org/go/internal/value"
3231
)
3332

3433
type buildContext struct {
@@ -77,7 +76,6 @@ type typeFunc func(b *builder, a cue.Value)
7776

7877
func schemas(g *Generator, inst cue.InstanceOrValue) (schemas *ast.StructLit, err error) {
7978
val := inst.Value()
80-
_, isInstance := inst.(*cue.Instance)
8179
var fieldFilter *regexp.Regexp
8280
if g.FieldFilter != "" {
8381
fieldFilter, err = regexp.Compile(g.FieldFilter)
@@ -111,37 +109,6 @@ func schemas(g *Generator, inst cue.InstanceOrValue) (schemas *ast.StructLit, er
111109
externalRefs: map[string]*externalType{},
112110
fieldFilter: fieldFilter,
113111
}
114-
if g.ReferenceFunc != nil {
115-
if !isInstance {
116-
panic("cannot use ReferenceFunc along with cue.Value")
117-
}
118-
if g.NameFunc != nil {
119-
panic("cannot specify both ReferenceFunc and NameFunc")
120-
}
121-
122-
c.nameFunc = func(val cue.Value, path cue.Path) string {
123-
sels := path.Selectors()
124-
labels := make([]string, len(sels))
125-
for i, sel := range sels {
126-
labels[i] = selectorLabel(sel) // TODO this is arguably incorrect.
127-
}
128-
inst, ok := c.imports[val]
129-
if !ok {
130-
r, n := internalvalue.ToInternal(val)
131-
buildInst := r.GetInstanceFromNode(n)
132-
var err error
133-
inst, err = (*cue.Runtime)(r).Build(buildInst)
134-
if err != nil {
135-
panic("cannot build instance from value")
136-
}
137-
if c.imports == nil {
138-
c.imports = make(map[cue.Value]*cue.Instance)
139-
}
140-
c.imports[val] = inst
141-
}
142-
return g.ReferenceFunc(inst, labels)
143-
}
144-
}
145112

146113
switch g.Version {
147114
case "3.0.0":

encoding/openapi/openapi.go

-10
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,6 @@ type Config struct {
3636
// Info may be a *ast.StructLit or any type that marshals to JSON.
3737
Info interface{}
3838

39-
// ReferenceFunc allows users to specify an alternative representation
40-
// for references. An empty string tells the generator to expand the type
41-
// in place and, if applicable, not generate a schema for that entity.
42-
//
43-
// If this field is non-nil and a cue.Value is passed as the InstanceOrValue,
44-
// there will be a panic.
45-
//
46-
// Deprecated: use NameFunc instead.
47-
ReferenceFunc func(inst *cue.Instance, path []string) string
48-
4939
// NameFunc allows users to specify an alternative representation
5040
// for references. It is called with the value passed to the top level
5141
// method or function and the path to the entity being generated.

encoding/openapi/openapi_test.go

+11-74
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/google/go-cmp/cmp"
2626

2727
"cuelang.org/go/cue"
28-
"cuelang.org/go/cue/build"
2928
"cuelang.org/go/cue/cuecontext"
3029
"cuelang.org/go/cue/errors"
3130
"cuelang.org/go/cue/load"
@@ -42,38 +41,20 @@ func TestParseDefinitions(t *testing.T) {
4241
resolveRefs := &openapi.Config{Info: info, ExpandReferences: true}
4342

4443
testCases := []struct {
45-
in, out string
46-
variant string
47-
instanceOnly bool
48-
valueOnly bool
49-
config *openapi.Config
50-
err string
44+
in, out string
45+
variant string
46+
config *openapi.Config
47+
err string
5148
}{{
5249
in: "structural.cue",
5350
out: "structural.json",
5451
config: resolveRefs,
5552
}, {
56-
in: "structural.cue",
57-
variant: "+ReferenceFunc",
58-
out: "structural.json",
59-
instanceOnly: true,
53+
in: "protobuf.cue",
54+
out: "protobuf.json",
6055
config: &openapi.Config{
6156
Info: info,
6257
ExpandReferences: true,
63-
ReferenceFunc: func(v *cue.Instance, path []string) string {
64-
return strings.Join(path, "_")
65-
},
66-
},
67-
}, {
68-
in: "protobuf.cue",
69-
out: "protobuf.json",
70-
instanceOnly: true,
71-
config: &openapi.Config{
72-
Info: info,
73-
ExpandReferences: true,
74-
ReferenceFunc: func(p *cue.Instance, path []string) string {
75-
return strings.Join(path, ".")
76-
},
7758
},
7859
}, {
7960
in: "nested.cue",
@@ -160,20 +141,6 @@ func TestParseDefinitions(t *testing.T) {
160141
return "Randomly picked description from a set of size one."
161142
},
162143
},
163-
}, {
164-
in: "oneof.cue",
165-
out: "oneof-funcs.json",
166-
variant: "+ReferenceFunc",
167-
instanceOnly: true,
168-
config: &openapi.Config{
169-
Info: info,
170-
ReferenceFunc: func(v *cue.Instance, path []string) string {
171-
return strings.ToUpper(strings.Join(path, "_"))
172-
},
173-
DescriptionFunc: func(v cue.Value) string {
174-
return "Randomly picked description from a set of size one."
175-
},
176-
},
177144
}, {
178145
in: "refs.cue",
179146
out: "refs.json",
@@ -187,21 +154,6 @@ func TestParseDefinitions(t *testing.T) {
187154
return strings.TrimPrefix(path.String(), "#")
188155
},
189156
},
190-
}, {
191-
in: "refs.cue",
192-
out: "refs.json",
193-
variant: "+ReferenceFunc",
194-
instanceOnly: true,
195-
config: &openapi.Config{
196-
Info: info,
197-
ReferenceFunc: func(v *cue.Instance, path []string) string {
198-
switch {
199-
case strings.HasPrefix(path[0], "Excluded"):
200-
return ""
201-
}
202-
return strings.Join(path, ".")
203-
},
204-
},
205157
}, {
206158
in: "issue131.cue",
207159
out: "issue131.json",
@@ -268,27 +220,12 @@ func TestParseDefinitions(t *testing.T) {
268220
inst := load.Instances([]string{filename}, &load.Config{
269221
Dir: "./testdata",
270222
})[0]
271-
if !tc.valueOnly {
272-
t.Run("Instance", func(t *testing.T) {
273-
// Old style call, with *cue.Instance.
274-
inst := cue.Build([]*build.Instance{inst})[0]
275-
if inst.Err != nil {
276-
t.Fatal(errors.Details(inst.Err, nil))
277-
}
278-
run(t, inst)
279-
})
280-
}
281-
if !tc.instanceOnly {
282-
t.Run("Value", func(t *testing.T) {
283-
// New style call, with cue.Value
284-
ctx := cuecontext.New()
285-
v := ctx.BuildInstance(inst)
286-
if err := v.Err(); err != nil {
287-
t.Fatal(errors.Details(err, nil))
288-
}
289-
run(t, v)
290-
})
223+
ctx := cuecontext.New()
224+
v := ctx.BuildInstance(inst)
225+
if err := v.Err(); err != nil {
226+
t.Fatal(errors.Details(err, nil))
291227
}
228+
run(t, v)
292229
})
293230
}
294231
}

0 commit comments

Comments
 (0)