Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: add Value.Equals method
Browse files Browse the repository at this point in the history
Also changed typos for != support for numbers.

Change-Id: I8bc34b50b7c87b31146cd89ea683a4182039b4e1
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2641
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jul 26, 2019
1 parent b05c768 commit 5ba7174
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 6 deletions.
12 changes: 12 additions & 0 deletions cue/types.go
Expand Up @@ -1165,6 +1165,18 @@ func (v Value) Unify(w Value) Value {
return u
}

// Equals reports whether two values are equal.
// The result is undefined for incomplete values.
func (v Value) Equals(other Value) bool {
if v.path == nil || other.path == nil {
return false
}
x := v.path.val()
y := other.path.val()
// TODO: improve upon this highly inefficient implementation.
return subsumes(v.ctx(), x, y, 0) && subsumes(v.ctx(), y, x, 0)
}

// Format prints a debug version of a value.
func (v Value) Format(state fmt.State, verb rune) {
ctx := v.ctx()
Expand Down
53 changes: 53 additions & 0 deletions cue/types_test.go
Expand Up @@ -921,6 +921,59 @@ func TestUnify(t *testing.T) {
}
}

func TestEquals(t *testing.T) {
testCases := []struct {
a, b string
want bool
}{{
`4`, `4`, true,
}, {
`"str"`, `2`, false,
}, {
`2`, `3`, false,
}, {
`[1]`, `[3]`, false,
}, {
`[]`, `[]`, true,
}, {
`{
a: b,
b: a,
}`,
`{
a: b,
b: a,
}`,
true,
}, {
`{
a: "foo",
b: "bar",
}`,
`{
a: "foo",
}`,
false,
}}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
var r Runtime
a, err := r.Compile("a", tc.a)
if err != nil {
t.Fatal(err)
}
b, err := r.Compile("b", tc.b)
if err != nil {
t.Fatal(err)
}
got := a.Value().Equals(b.Value())
if got != tc.want {
t.Errorf("got %v; want %v", got, tc.want)
}
})
}
}

func TestDecode(t *testing.T) {
type fields struct {
A int `json:"A"`
Expand Down
8 changes: 2 additions & 6 deletions encoding/openapi/build.go
Expand Up @@ -576,10 +576,6 @@ func (b *builder) number(v cue.Value) {
// Type may be number of float.

switch op, a := v.Expr(); op {
// TODO: support the following JSON schema constraints
// - multipleOf
// setIntConstraint(t, "multipleOf", a)

case cue.LessThanOp:
b.setFilter("Schema", "exclusiveMaximum", b.big(a[0]))

Expand All @@ -595,8 +591,8 @@ func (b *builder) number(v cue.Value) {
case cue.NotEqualOp:
i := b.big(a[0])
b.setNot("allOff", []*oaSchema{
b.kv("minItems", i),
b.kv("maxItems", i),
b.kv("minimum", i),
b.kv("maximum", i),
})

case cue.CallOp:
Expand Down
2 changes: 2 additions & 0 deletions encoding/openapi/testdata/nums.cue
@@ -1,3 +1,5 @@
import "math"

mul: math.MultipleOf(5)

neq: !=4
16 changes: 16 additions & 0 deletions encoding/openapi/testdata/nums.json
Expand Up @@ -6,6 +6,22 @@
"mul": {
"type": "number",
"multipleOf": 5
},
"neq": {
"not": {
"type": "number",
"allOff": [
{
"type": "number",
"minimum": 4
},
{
"type": "number",
"maximum": 4
}
]
},
"type": "number"
}
}
}
Expand Down

0 comments on commit 5ba7174

Please sign in to comment.