This repository has been archived by the owner on Nov 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cue: support optional field lookup in LookupPath
This is done by allowing Selectors to be converted to an optional form (if they aren't already). It also supports the use of the Selectors AnyIndex and AnyString for getting `T` for `[string]: T` and `[...T]`. This also allows deprecating Elem and Template, which will be done in a separate CL. Change-Id: I6074382f12259c3dd87557471d80f82720a84779 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9347 Reviewed-by: CUE cueckoo <cueckoo@gmail.com> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
- Loading branch information
Showing
5 changed files
with
194 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2021 CUE Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cue_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"cuelang.org/go/cue" | ||
"cuelang.org/go/internal/diff" | ||
) | ||
|
||
func TestLookupPath(t *testing.T) { | ||
r := &cue.Runtime{} | ||
|
||
testCases := []struct { | ||
in string | ||
path cue.Path | ||
out string `test:"update"` // :nerdSnipe: | ||
notExist bool `test:"update"` // :nerdSnipe: | ||
}{{ | ||
in: ` | ||
[Name=string]: { a: Name } | ||
`, | ||
path: cue.MakePath(cue.Str("a")), | ||
notExist: true, | ||
}, { | ||
in: ` | ||
#V: { | ||
x: int | ||
} | ||
#X: { | ||
[string]: int64 | ||
} & #V | ||
v: #X | ||
`, | ||
path: cue.ParsePath("v.x"), | ||
out: `int64`, | ||
}, { | ||
in: ` | ||
a: [...int] | ||
`, | ||
path: cue.MakePath(cue.Str("a"), cue.AnyIndex), | ||
out: `int`, | ||
}, { | ||
in: ` | ||
[Name=string]: { a: Name } | ||
`, | ||
path: cue.MakePath(cue.AnyString, cue.Str("a")), | ||
out: `string`, | ||
}, { | ||
in: ` | ||
[Name=string]: { a: Name } | ||
`, | ||
path: cue.MakePath(cue.Str("b").Optional(), cue.Str("a")), | ||
out: `"b"`, | ||
}, { | ||
in: ` | ||
[Name=string]: { a: Name } | ||
`, | ||
path: cue.MakePath(cue.AnyString), | ||
out: `{a: string}`, | ||
}, { | ||
in: ` | ||
a: [Foo=string]: [Bar=string]: { b: Foo+Bar } | ||
`, | ||
path: cue.MakePath(cue.Str("a"), cue.Str("b"), cue.Str("c")).Optional(), | ||
out: `{b: "bc"}`, | ||
}, { | ||
in: ` | ||
a: [Foo=string]: b: [Bar=string]: { c: Foo } | ||
a: foo: b: [Bar=string]: { d: Bar } | ||
`, | ||
path: cue.MakePath(cue.Str("a"), cue.Str("foo"), cue.Str("b"), cue.AnyString), | ||
out: `{c: "foo", d: string}`, | ||
}, { | ||
in: ` | ||
[Name=string]: { a: Name } | ||
`, | ||
path: cue.MakePath(cue.Str("a")), | ||
notExist: true, | ||
}} | ||
for _, tc := range testCases { | ||
t.Run(tc.path.String(), func(t *testing.T) { | ||
v := compileT(t, r, tc.in) | ||
|
||
v = v.LookupPath(tc.path) | ||
|
||
if exists := v.Exists(); exists != !tc.notExist { | ||
t.Fatalf("exists: got %v; want: %v", exists, !tc.notExist) | ||
} else if !exists { | ||
return | ||
} | ||
|
||
w := compileT(t, r, tc.out) | ||
|
||
if k, d := diff.Diff(v, w); k != diff.Identity { | ||
b := &bytes.Buffer{} | ||
diff.Print(b, d) | ||
t.Error(b) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func compileT(t *testing.T, r *cue.Runtime, s string) cue.Value { | ||
t.Helper() | ||
inst, err := r.Compile("", s) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return inst.Value() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters