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

Commit

Permalink
cue: keep sane references for embedded disjunctions in Expr
Browse files Browse the repository at this point in the history
In Expr, simulate a struct with just the fields so that embedded
disjunctions with references to such fields will direct to the unmerged
values of these references.

This also addes OpenAPI tests, which relies on this functionality.

Change-Id: I5d127fee1389a88a45f3705ad1a0c929ee1c2bb0
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9881
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
mpvl authored and myitcv committed May 21, 2021
1 parent b39a2d0 commit 37bf801
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 18 deletions.
58 changes: 40 additions & 18 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2383,38 +2383,34 @@ func (v Value) Expr() (Op, []Value) {
op = CallOp

case *adt.StructLit:
// Simulate old embeddings.
envEmbed := &adt.Environment{
Up: env,
Vertex: v.v,
}
hasEmbed := false
fields := []adt.Decl{}
ctx := v.ctx()
for _, d := range x.Decls {
switch x := d.(type) {
switch d.(type) {
default:
fields = append(fields, d)
case adt.Value:
fields = append(fields, d)
case adt.Expr:
// embedding
n := &adt.Vertex{Label: v.v.Label}
c := adt.MakeRootConjunct(envEmbed, x)
n.AddConjunct(c)
n.Finalize(ctx)
n.Parent = v.v.Parent
a = append(a, makeValue(v.idx, n, v.parent_))
hasEmbed = true
}
}
if len(a) == 0 {

if !hasEmbed {
a = append(a, v)
break
}

ctx := v.ctx()

n := v.v

if len(fields) > 0 {
n := &adt.Vertex{
Label: v.v.Label,
n = &adt.Vertex{
Parent: v.v.Parent,
Label: v.v.Label,
}

s := &adt.StructLit{}
if k := v.v.Kind(); k != adt.StructKind && k != BottomKind {
// TODO: we should also add such a declaration for embeddings
Expand All @@ -2428,10 +2424,36 @@ func (v Value) Expr() (Op, []Value) {
n.AddConjunct(c)
n.Finalize(ctx)
n.Parent = v.v.Parent
}

// Simulate old embeddings.
envEmbed := &adt.Environment{
Up: env,
Vertex: n,
}

for _, d := range x.Decls {
switch x := d.(type) {
case adt.Value:
case adt.Expr:
// embedding
n := &adt.Vertex{Label: v.v.Label}
c := adt.MakeRootConjunct(envEmbed, x)
n.AddConjunct(c)
n.Finalize(ctx)
n.Parent = v.v.Parent
a = append(a, makeValue(v.idx, n, v.parent_))
}
}

// Could be done earlier, but keep struct with fields at end.
if len(fields) > 0 {
a = append(a, makeValue(v.idx, n, v.parent_))
}

op = adt.AndOp
if len(a) > 1 {
op = adt.AndOp
}

default:
a = append(a, v)
Expand Down
3 changes: 3 additions & 0 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3343,6 +3343,9 @@ func TestExpr(t *testing.T) {
// Don't split of concrete values.
input: `v: { "foo", #def: 1 }`,
want: `{"foo",#def:1}`,
}, {
input: `v: { {} | { a: #A, b: #B}, #A: {} | { c: int} }, #B: int | bool`,
want: `&(|({} {a:#A,b:#B}) {#A:({}|{c:int})})`,
}, {
input: `v: { {c: a}, b: a }, a: int`,
want: `&({c:a} {b:a})`,
Expand Down
8 changes: 8 additions & 0 deletions encoding/openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ func TestParseDefinitions(t *testing.T) {
in: "openapi.cue",
out: "openapi-norefs.json",
config: resolveRefs,
}, {
in: "embed.cue",
out: "embed.json",
config: defaultConfig,
}, {
in: "embed.cue",
out: "embed-norefs.json",
config: resolveRefs,
}, {
in: "oneof.cue",
out: "oneof-funcs.json",
Expand Down
121 changes: 121 additions & 0 deletions encoding/openapi/testdata/embed-norefs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"openapi": "3.0.0",
"info": {
"title": "test",
"version": "v1"
},
"paths": {},
"components": {
"schemas": {
"Foo": {
"type": "string"
},
"LoadBalancerSettings": {
"type": "object",
"properties": {
"consistentHash": {
"type": "object",
"properties": {
"httpHeaderName": {
"type": "string"
}
}
},
"b": {
"type": "string"
}
},
"oneOf": [
{
"not": {
"anyOf": [
{
"required": [
"consistentHash",
"b"
],
"properties": {
"consistentHash": {
"oneOf": [
{
"not": {
"anyOf": [
{
"required": [
"httpHeaderName"
]
}
]
}
},
{
"required": [
"httpHeaderName"
]
}
]
}
}
}
]
}
},
{
"required": [
"consistentHash",
"b"
],
"properties": {
"consistentHash": {
"oneOf": [
{
"not": {
"anyOf": [
{
"required": [
"httpHeaderName"
]
}
]
}
},
{
"required": [
"httpHeaderName"
]
}
]
}
}
}
]
},
"LoadBalancerSettings.ConsistentHashLB": {
"type": "object",
"properties": {
"httpHeaderName": {
"type": "string"
}
},
"oneOf": [
{
"not": {
"anyOf": [
{
"required": [
"httpHeaderName"
]
}
]
}
},
{
"required": [
"httpHeaderName"
]
}
]
}
}
}
}
11 changes: 11 additions & 0 deletions encoding/openapi/testdata/embed.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#Foo: string

#LoadBalancerSettings: {
{} | {
consistentHash: #ConsistentHashLB
b: #Foo
}
#ConsistentHashLB: {} | {
httpHeaderName: string
}
}
85 changes: 85 additions & 0 deletions encoding/openapi/testdata/embed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"openapi": "3.0.0",
"info": {
"title": "Generated by cue.",
"version": "no version"
},
"paths": {},
"components": {
"schemas": {
"Foo": {
"type": "string"
},
"LoadBalancerSettings": {
"type": "object",
"oneOf": [
{
"not": {
"anyOf": [
{
"required": [
"consistentHash",
"b"
],
"properties": {
"consistentHash": {
"$ref": "#/components/schemas/LoadBalancerSettings.ConsistentHashLB"
},
"b": {
"$ref": "#/components/schemas/Foo"
}
}
}
]
}
},
{
"required": [
"consistentHash",
"b"
],
"properties": {
"consistentHash": {
"$ref": "#/components/schemas/LoadBalancerSettings.ConsistentHashLB"
},
"b": {
"$ref": "#/components/schemas/Foo"
}
}
}
]
},
"LoadBalancerSettings.ConsistentHashLB": {
"type": "object",
"oneOf": [
{
"not": {
"anyOf": [
{
"required": [
"httpHeaderName"
],
"properties": {
"httpHeaderName": {
"type": "string"
}
}
}
]
}
},
{
"required": [
"httpHeaderName"
],
"properties": {
"httpHeaderName": {
"type": "string"
}
}
}
]
}
}
}
}

0 comments on commit 37bf801

Please sign in to comment.