this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

all: remove last unnecessary uses of cue.Runtime

None of these affect exported API, so they are easy to replace
without breaking any users or changing any behavior.

Note that cue/path_test.go needs to move to an external test package
as otherwise importing cuecontext results in an import cycle.

This resolves 18 staticcheck deprecation warnings.

For #2480.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I3757aecb2918482bfa90dbd851faba46ec1d936e
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199863
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Matthew Sackman <matthew@cue.works>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+94 -115
+62 -61
cue/path_test.go
··· 12 12 // See the License for the specific language governing permissions and 13 13 // limitations under the License. 14 14 15 - package cue 15 + package cue_test 16 16 17 17 import ( 18 18 "fmt" 19 19 "testing" 20 + 21 + "cuelang.org/go/cue" 22 + "cuelang.org/go/cue/cuecontext" 20 23 ) 21 24 22 25 func TestPaths(t *testing.T) { 23 - var r Runtime 24 - inst, _ := r.Compile("", ` 26 + val := cuecontext.New().CompileString(` 25 27 #Foo: a: b: 1 26 28 "#Foo": c: d: 2 27 29 _foo: b: 5 ··· 36 38 x: y: X.a 37 39 `) 38 40 testCases := []struct { 39 - path Path 41 + path cue.Path 40 42 out string 41 43 str string 42 44 err bool 43 45 }{{ 44 - path: MakePath(Str("list"), AnyIndex), 46 + path: cue.MakePath(cue.Str("list"), cue.AnyIndex), 45 47 out: "int", 46 48 str: "list.[_]", 47 49 }, { 48 50 49 - path: MakePath(Def("#Foo"), Str("a"), Str("b")), 51 + path: cue.MakePath(cue.Def("#Foo"), cue.Str("a"), cue.Str("b")), 50 52 out: "1", 51 53 str: "#Foo.a.b", 52 54 }, { 53 - path: ParsePath(`#Foo.a.b`), 55 + path: cue.ParsePath(`#Foo.a.b`), 54 56 out: "1", 55 57 str: "#Foo.a.b", 56 58 }, { 57 - path: ParsePath(`"#Foo".c.d`), 59 + path: cue.ParsePath(`"#Foo".c.d`), 58 60 out: "2", 59 61 str: `"#Foo".c.d`, 60 62 }, { 61 63 // fallback Def(Foo) -> Def(#Foo) 62 - path: MakePath(Def("Foo"), Str("a"), Str("b")), 64 + path: cue.MakePath(cue.Def("Foo"), cue.Str("a"), cue.Str("b")), 63 65 out: "1", 64 66 str: "#Foo.a.b", 65 67 }, { 66 - path: MakePath(Str("b"), Index(2)), 68 + path: cue.MakePath(cue.Str("b"), cue.Index(2)), 67 69 out: "6", 68 70 str: "b[2]", // #Foo.b.2 69 71 }, { 70 - path: MakePath(Str("c"), Str("#Foo")), 72 + path: cue.MakePath(cue.Str("c"), cue.Str("#Foo")), 71 73 out: "7", 72 74 str: `c."#Foo"`, 73 75 }, { 74 - path: MakePath(Hid("_foo", "_"), Str("b")), 76 + path: cue.MakePath(cue.Hid("_foo", "_"), cue.Str("b")), 75 77 out: "5", 76 78 str: `_foo.b`, 77 79 }, { 78 - path: ParsePath("#Foo.a.b"), 80 + path: cue.ParsePath("#Foo.a.b"), 79 81 str: "#Foo.a.b", 80 82 out: "1", 81 83 }, { 82 - path: ParsePath("#Foo.a.c"), 84 + path: cue.ParsePath("#Foo.a.c"), 83 85 str: "#Foo.a.c", 84 86 out: `_|_ // field not found: c`, 85 87 }, { 86 - path: ParsePath(`b[2]`), 88 + path: cue.ParsePath(`b[2]`), 87 89 str: `b[2]`, 88 90 out: "6", 89 91 }, { 90 - path: ParsePath(`c."#Foo"`), 92 + path: cue.ParsePath(`c."#Foo"`), 91 93 str: `c."#Foo"`, 92 94 out: "7", 93 95 }, { 94 - path: ParsePath("foo._foo"), 96 + path: cue.ParsePath("foo._foo"), 95 97 str: "_|_", 96 98 err: true, 97 99 out: `_|_ // invalid path: hidden label _foo not allowed`, 98 100 }, { 99 - path: ParsePath(`c."#Foo`), 101 + path: cue.ParsePath(`c."#Foo`), 100 102 str: "_|_", 101 103 err: true, 102 104 out: `_|_ // string literal not terminated`, 103 105 }, { 104 - path: ParsePath(`b[a]`), 106 + path: cue.ParsePath(`b[a]`), 105 107 str: "_|_", 106 108 err: true, 107 109 out: `_|_ // non-constant expression a`, 108 110 }, { 109 - path: ParsePath(`b['1']`), 111 + path: cue.ParsePath(`b['1']`), 110 112 str: "_|_", 111 113 err: true, 112 114 out: `_|_ // invalid string index '1'`, 113 115 }, { 114 - path: ParsePath(`b[3T]`), 116 + path: cue.ParsePath(`b[3T]`), 115 117 str: "_|_", 116 118 err: true, 117 119 out: `_|_ // int label out of range (3000000000000 not >=0 and <= 268435454)`, 118 120 }, { 119 - path: ParsePath(`b[3.3]`), 121 + path: cue.ParsePath(`b[3.3]`), 120 122 str: "_|_", 121 123 err: true, 122 124 out: `_|_ // invalid literal 3.3`, 123 125 }, { 124 - path: MakePath(Str("map"), AnyString), 126 + path: cue.MakePath(cue.Str("map"), cue.AnyString), 125 127 out: "int", 126 128 str: "map.[_]", 127 129 }, { 128 - path: MakePath(Str("list"), AnyIndex), 130 + path: cue.MakePath(cue.Str("list"), cue.AnyIndex), 129 131 out: "int", 130 132 str: "list.[_]", 131 133 }, { 132 - path: ParsePath("x.y"), 134 + path: cue.ParsePath("x.y"), 133 135 out: "{\n\tb: 0\n}", 134 136 str: "x.y", 135 137 }, { 136 - path: ParsePath("x.y.b"), 138 + path: cue.ParsePath("x.y.b"), 137 139 out: "0", 138 140 str: "x.y.b", 139 141 }} 140 142 141 - v := inst.Value() 142 143 for _, tc := range testCases { 143 144 t.Run(tc.str, func(t *testing.T) { 144 145 if gotErr := tc.path.Err() != nil; gotErr != tc.err { 145 146 t.Errorf("error: got %v; want %v", gotErr, tc.err) 146 147 } 147 148 148 - w := v.LookupPath(tc.path) 149 + w := val.LookupPath(tc.path) 149 150 150 151 if got := fmt.Sprint(w); got != tc.out { 151 152 t.Errorf("Value: got %v; want %v", got, tc.out) ··· 167 168 } 168 169 169 170 var selectorTests = []struct { 170 - sel Selector 171 - stype SelectorType 171 + sel cue.Selector 172 + stype cue.SelectorType 172 173 string string 173 174 unquoted string 174 175 index int ··· 178 179 isString bool 179 180 pkgPath string 180 181 }{{ 181 - sel: Str("foo"), 182 - stype: StringLabel, 182 + sel: cue.Str("foo"), 183 + stype: cue.StringLabel, 183 184 string: "foo", 184 185 unquoted: "foo", 185 186 isString: true, 186 187 }, { 187 - sel: Str("_foo"), 188 - stype: StringLabel, 188 + sel: cue.Str("_foo"), 189 + stype: cue.StringLabel, 189 190 string: `"_foo"`, 190 191 unquoted: "_foo", 191 192 isString: true, 192 193 }, { 193 - sel: Str(`a "b`), 194 - stype: StringLabel, 194 + sel: cue.Str(`a "b`), 195 + stype: cue.StringLabel, 195 196 string: `"a \"b"`, 196 197 unquoted: `a "b`, 197 198 isString: true, 198 199 }, { 199 - sel: Index(5), 200 - stype: IndexLabel, 200 + sel: cue.Index(5), 201 + stype: cue.IndexLabel, 201 202 string: "5", 202 203 index: 5, 203 204 }, { 204 - sel: Def("foo"), 205 - stype: DefinitionLabel, 205 + sel: cue.Def("foo"), 206 + stype: cue.DefinitionLabel, 206 207 string: "#foo", 207 208 isDefinition: true, 208 209 }, { 209 - sel: Str("foo").Optional(), 210 - stype: StringLabel | OptionalConstraint, 210 + sel: cue.Str("foo").Optional(), 211 + stype: cue.StringLabel | cue.OptionalConstraint, 211 212 string: "foo?", 212 213 unquoted: "foo", 213 214 isString: true, 214 215 isConstraint: true, 215 216 }, { 216 - sel: Str("foo").Required(), 217 - stype: StringLabel | RequiredConstraint, 217 + sel: cue.Str("foo").Required(), 218 + stype: cue.StringLabel | cue.RequiredConstraint, 218 219 string: "foo!", 219 220 unquoted: "foo", 220 221 isString: true, 221 222 isConstraint: true, 222 223 }, { 223 - sel: Def("foo").Required().Optional(), 224 - stype: DefinitionLabel | OptionalConstraint, 224 + sel: cue.Def("foo").Required().Optional(), 225 + stype: cue.DefinitionLabel | cue.OptionalConstraint, 225 226 string: "#foo?", 226 227 isDefinition: true, 227 228 isConstraint: true, 228 229 }, { 229 - sel: Def("foo").Optional().Required(), 230 - stype: DefinitionLabel | RequiredConstraint, 230 + sel: cue.Def("foo").Optional().Required(), 231 + stype: cue.DefinitionLabel | cue.RequiredConstraint, 231 232 string: "#foo!", 232 233 isDefinition: true, 233 234 isConstraint: true, 234 235 }, { 235 - sel: AnyString, 236 - stype: StringLabel | PatternConstraint, 236 + sel: cue.AnyString, 237 + stype: cue.StringLabel | cue.PatternConstraint, 237 238 string: "[_]", 238 239 isConstraint: true, 239 240 }, { 240 - sel: AnyIndex, 241 - stype: IndexLabel | PatternConstraint, 241 + sel: cue.AnyIndex, 242 + stype: cue.IndexLabel | cue.PatternConstraint, 242 243 string: "[_]", 243 244 isConstraint: true, 244 245 }, { 245 - sel: Hid("_foo", "example.com"), 246 - stype: HiddenLabel, 246 + sel: cue.Hid("_foo", "example.com"), 247 + stype: cue.HiddenLabel, 247 248 string: "_foo", 248 249 isHidden: true, 249 250 pkgPath: "example.com", 250 251 }, { 251 - sel: Hid("_#foo", "example.com"), 252 - stype: HiddenDefinitionLabel, 252 + sel: cue.Hid("_#foo", "example.com"), 253 + stype: cue.HiddenDefinitionLabel, 253 254 string: "_#foo", 254 255 isHidden: true, 255 256 isDefinition: true, ··· 275 276 t.Errorf("unexpected sel.Unquoted result; got %q want %q", got, want) 276 277 } 277 278 } 278 - if sel.Type() != IndexLabel { 279 + if sel.Type() != cue.IndexLabel { 279 280 checkPanic(t, "Index called on non-index selector", func() { 280 281 sel.Index() 281 282 }) ··· 304 305 } 305 306 306 307 func TestSelectorTypeString(t *testing.T) { 307 - if got, want := InvalidSelectorType.String(), "NoLabels"; got != want { 308 + if got, want := cue.InvalidSelectorType.String(), "NoLabels"; got != want { 308 309 t.Errorf("unexpected SelectorType.String result; got %q want %q", got, want) 309 310 } 310 - if got, want := PatternConstraint.String(), "PatternConstraint"; got != want { 311 + if got, want := cue.PatternConstraint.String(), "PatternConstraint"; got != want { 311 312 t.Errorf("unexpected SelectorType.String result; got %q want %q", got, want) 312 313 } 313 - if got, want := (StringLabel | OptionalConstraint).String(), "StringLabel|OptionalConstraint"; got != want { 314 + if got, want := (cue.StringLabel | cue.OptionalConstraint).String(), "StringLabel|OptionalConstraint"; got != want { 314 315 t.Errorf("unexpected SelectorType.String result; got %q want %q", got, want) 315 316 } 316 - if got, want := SelectorType(255).String(), "StringLabel|IndexLabel|DefinitionLabel|HiddenLabel|HiddenDefinitionLabel|OptionalConstraint|RequiredConstraint|PatternConstraint"; got != want { 317 + if got, want := cue.SelectorType(255).String(), "StringLabel|IndexLabel|DefinitionLabel|HiddenLabel|HiddenDefinitionLabel|OptionalConstraint|RequiredConstraint|PatternConstraint"; got != want { 317 318 t.Errorf("unexpected SelectorType.String result; got %q want %q", got, want) 318 319 } 319 320 }
+6 -8
encoding/openapi/openapi_test.go
··· 310 310 311 311 // TODO: move OpenAPI testing to txtar and allow errors. 312 312 func TestIssue1234(t *testing.T) { 313 - var r cue.Runtime 314 - inst, err := r.Compile("test", ` 313 + val := cuecontext.New().CompileString(` 315 314 #Test: or([]) 316 315 317 316 `) 318 - if err != nil { 317 + if err := val.Err(); err != nil { 319 318 t.Fatal(err) 320 319 } 321 320 322 - _, err = openapi.Gen(inst, &openapi.Config{}) 321 + _, err := openapi.Gen(val, &openapi.Config{}) 323 322 if err == nil { 324 323 t.Fatal("expected error") 325 324 } ··· 329 328 func TestX(t *testing.T) { 330 329 t.Skip() 331 330 332 - var r cue.Runtime 333 - inst, err := r.Compile("test", ` 331 + val := cuecontext.New().CompileString(` 334 332 `) 335 - if err != nil { 333 + if err := val.Err(); err != nil { 336 334 t.Fatal(err) 337 335 } 338 336 339 - b, err := openapi.Gen(inst, &openapi.Config{ 337 + b, err := openapi.Gen(val, &openapi.Config{ 340 338 // ExpandReferences: true, 341 339 }) 342 340 if err != nil {
+6 -9
encoding/protobuf/jsonpb/decoder_test.go
··· 21 21 "cuelang.org/go/cue" 22 22 "cuelang.org/go/cue/ast" 23 23 "cuelang.org/go/cue/ast/astutil" 24 + "cuelang.org/go/cue/cuecontext" 24 25 "cuelang.org/go/cue/errors" 25 26 "cuelang.org/go/cue/format" 26 27 "cuelang.org/go/cue/parser" ··· 36 37 Name: "jsonpb", 37 38 } 38 39 39 - r := cue.Runtime{} 40 - 41 40 test.Run(t, func(t *cuetxtar.Test) { 42 41 // TODO: use high-level API. 43 42 ··· 47 46 for _, f := range t.Archive.Files { 48 47 switch { 49 48 case f.Name == "schema.cue": 50 - inst, err := r.Compile(f.Name, f.Data) 51 - if err != nil { 49 + schema = t.CueContext().CompileBytes(f.Data, cue.Filename(f.Name)) 50 + if err := schema.Err(); err != nil { 52 51 t.WriteErrors(errors.Promote(err, "test")) 53 52 return 54 53 } 55 - schema = inst.Value() 56 54 continue 57 55 58 56 case strings.HasPrefix(f.Name, "out/"): ··· 109 107 if strings.TrimSpace(data) == "" { 110 108 t.Skip() 111 109 } 112 - var r cue.Runtime 113 - inst, err := r.Compile("schema", schema) 114 - if err != nil { 110 + val := cuecontext.New().CompileString(schema) 111 + if err := val.Err(); err != nil { 115 112 t.Fatal(err) 116 113 } 117 114 ··· 120 117 t.Fatal(err) 121 118 } 122 119 123 - if err := jsonpb.NewDecoder(inst.Value()).RewriteFile(file); err != nil { 120 + if err := jsonpb.NewDecoder(val).RewriteFile(file); err != nil { 124 121 t.Fatal(err) 125 122 } 126 123
+7 -12
encoding/protobuf/jsonpb/encoder_test.go
··· 32 32 Name: "jsonpb", 33 33 } 34 34 35 - r := cue.Runtime{} 36 - 37 35 test.Run(t, func(t *cuetxtar.Test) { 38 36 // TODO: use high-level API. 39 37 ··· 41 39 var file *ast.File 42 40 43 41 for _, f := range t.Archive.Files { 44 - switch { 45 - case f.Name == "schema.cue": 46 - inst, err := r.Compile(f.Name, f.Data) 47 - if err != nil { 42 + switch f.Name { 43 + case "schema.cue": 44 + schema = t.CueContext().CompileBytes(f.Data) 45 + if err := schema.Err(); err != nil { 48 46 t.WriteErrors(errors.Promote(err, "test")) 49 47 return 50 48 } 51 - schema = inst.Value() 52 - 53 - case f.Name == "value.cue": 49 + case "value.cue": 54 50 f, err := parser.ParseFile(f.Name, f.Data, parser.ParseComments) 55 51 if err != nil { 56 52 t.WriteErrors(errors.Promote(err, "test")) ··· 61 57 } 62 58 63 59 if !schema.Exists() { 64 - inst, err := r.CompileFile(file) 65 - if err != nil { 60 + schema = t.CueContext().BuildFile(file) 61 + if err := schema.Err(); err != nil { 66 62 t.WriteErrors(errors.Promote(err, "test")) 67 63 } 68 - schema = inst.Value() 69 64 } 70 65 71 66 err := jsonpb.NewEncoder(schema).RewriteFile(file)
+2 -5
encoding/protobuf/textproto/decoder_test.go
··· 32 32 Name: "decode", 33 33 } 34 34 35 - r := cue.Runtime{} 36 - 37 35 d := textproto.NewDecoder() 38 36 39 37 test.Run(t, func(t *cuetxtar.Test) { ··· 46 44 for _, f := range t.Archive.Files { 47 45 switch { 48 46 case strings.HasSuffix(f.Name, ".cue"): 49 - inst, err := r.Compile(f.Name, f.Data) 50 - if err != nil { 47 + schema = t.CueContext().CompileBytes(f.Data) 48 + if err := schema.Err(); err != nil { 51 49 t.WriteErrors(errors.Promote(err, "test")) 52 50 return 53 51 } 54 - schema = inst.Value() 55 52 56 53 case strings.HasSuffix(f.Name, ".textproto"): 57 54 filename = f.Name
+4 -6
encoding/protobuf/textproto/encoder_test.go
··· 30 30 Name: "encode", 31 31 } 32 32 33 - r := cue.Runtime{} 34 - 35 33 test.Run(t, func(t *cuetxtar.Test) { 36 34 // TODO: use high-level API. 37 35 ··· 40 38 for _, f := range t.Archive.Files { 41 39 switch { 42 40 case strings.HasSuffix(f.Name, ".cue"): 43 - inst, err := r.Compile(f.Name, f.Data) 44 - if err != nil { 41 + val := t.CueContext().CompileBytes(f.Data) 42 + if err := val.Err(); err != nil { 45 43 t.WriteErrors(errors.Promote(err, "test")) 46 44 return 47 45 } 48 46 switch f.Name { 49 47 case "schema.cue": 50 - schema = inst.Value() 48 + schema = val 51 49 case "value.cue": 52 - value = inst.Value() 50 + value = val 53 51 } 54 52 } 55 53 }
+6 -12
encoding/yaml/yaml_test.go
··· 18 18 "strings" 19 19 "testing" 20 20 21 - "cuelang.org/go/cue" 22 - "cuelang.org/go/cue/ast" 23 21 "cuelang.org/go/cue/cuecontext" 24 22 "cuelang.org/go/cue/format" 25 23 ) ··· 98 96 }, null]`, 99 97 isStream: true, 100 98 }} 101 - r := &cue.Runtime{} 99 + ctx := cuecontext.New() 102 100 for _, tc := range testCases { 103 101 t.Run(tc.name, func(t *testing.T) { 104 102 f, err := Extract(tc.name, tc.yaml) ··· 110 108 t.Errorf("Extract:\ngot %q\nwant %q", got, tc.want) 111 109 } 112 110 113 - inst, err := Decode(r, tc.name, tc.yaml) 111 + file, err := Extract(tc.name, tc.yaml) 114 112 if err != nil { 115 113 t.Fatal(err) 116 114 } 117 - n := inst.Value().Syntax() 118 - if s, ok := n.(*ast.StructLit); ok { 119 - n = &ast.File{Decls: s.Elts} 120 - } 121 - b, _ = format.Node(n) 115 + b, _ = format.Node(file) 122 116 if got := strings.TrimSpace(string(b)); got != tc.want { 123 117 t.Errorf("Decode:\ngot %q\nwant %q", got, tc.want) 124 118 } ··· 128 122 yamlOut = tc.yamlOut 129 123 } 130 124 131 - inst, _ = r.Compile(tc.name, tc.want) 125 + wantVal := ctx.CompileString(tc.want) 132 126 if !tc.isStream { 133 - b, err = Encode(inst.Value()) 127 + b, err = Encode(wantVal) 134 128 if err != nil { 135 129 t.Error(err) 136 130 } ··· 138 132 t.Errorf("Encode:\ngot %q\nwant %q", got, yamlOut) 139 133 } 140 134 } else { 141 - iter, _ := inst.Value().List() 135 + iter, _ := wantVal.List() 142 136 b, err := EncodeStream(iter) 143 137 if err != nil { 144 138 t.Error(err)
+1 -2
pkg/encoding/json/manual.go
··· 98 98 99 99 // UnmarshalStream parses the JSON to a CUE instance. 100 100 func UnmarshalStream(data []byte) (ast.Expr, error) { 101 - var r cue.Runtime 102 - d := cuejson.NewDecoder(&r, "", bytes.NewReader(data)) 101 + d := cuejson.NewDecoder(nil, "", bytes.NewReader(data)) 103 102 104 103 a := []ast.Expr{} 105 104 for {