Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Optional() method to MatchQuery #105

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions apstra/query_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,17 @@ func (o *QEElement) getLast() *QEElement {
func (o *QEElement) String() string {
attrsSB := strings.Builder{}

// add first attribute to string builder without leading separator
if len(o.attributes) > 0 {
// add first attribute to string builder without leading separator
attrsSB.WriteString(o.attributes[0].String())
}

// remaining attributes added with leading separator
for _, a := range o.attributes[1:] {
attrsSB.WriteString(qEElementAttributeSep)
attrsSB.WriteString(a.String())
// remaining attributes added with leading separator
for _, a := range o.attributes[1:] {
attrsSB.WriteString(qEElementAttributeSep)
attrsSB.WriteString(a.String())
}
}

return fmt.Sprintf("%s(%s)", o.qeeType, attrsSB.String())
}

Expand Down Expand Up @@ -168,6 +169,7 @@ type PathQuery struct {
blueprintId ObjectId
blueprintType BlueprintType
where []string
optional bool
}

func (o *PathQuery) getBlueprintType() BlueprintType {
Expand Down Expand Up @@ -209,6 +211,11 @@ func (o *PathQuery) String() string {
for _, where := range o.where {
sb.WriteString(".where(" + where + ")")
}

if o.optional {
return "optional(" + sb.String() + ")"
}

return sb.String()
}

Expand Down Expand Up @@ -371,3 +378,10 @@ func (o *MatchQuery) Match(q *PathQuery) *MatchQuery {
o.match = append(o.match, *q)
return o
}

func (o *MatchQuery) Optional(q *PathQuery) *MatchQuery {
optional := *q
optional.optional = true
o.match = append(o.match, optional)
return o
}
30 changes: 30 additions & 0 deletions apstra/query_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,33 @@ func TestMatchQueryWhere(t *testing.T) {
}
}
}

func TestQueryMatchOptional(t *testing.T) {
expected := "" +
"match(" +
"" + "node(type='system',system_type='switch').out().node(type='interface').out().node(type='link',name='n_link')," +
"" + "optional(" +
"" + "" + "node(type='link',name='n_link').in_().node(type='tag',name='n_tag')" +
"" + ")" +
")"
q1 := new(PathQuery).
Node([]QEEAttribute{NodeTypeSystem.QEEAttribute(), {Key: "system_type", Value: QEStringVal("switch")}}).
Out([]QEEAttribute{}).
Node([]QEEAttribute{NodeTypeInterface.QEEAttribute()}).
Out([]QEEAttribute{}).
Node([]QEEAttribute{NodeTypeLink.QEEAttribute(), {Key: "name", Value: QEStringVal("n_link")}})

q2 := new(PathQuery).
Node([]QEEAttribute{NodeTypeLink.QEEAttribute(), {Key: "name", Value: QEStringVal("n_link")}}).
In([]QEEAttribute{}).
Node([]QEEAttribute{NodeTypeTag.QEEAttribute(), {Key: "name", Value: QEStringVal("n_tag")}})

result := new(MatchQuery).
Match(q1).
Optional(q2).
String()

if expected != result {
t.Fatalf("expected: %q, got %q", expected, result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ func TestCtLayout(t *testing.T) {
SessionAddressingIpv4: true,
},
Subpolicies: []*ConnectivityTemplatePrimitive{
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{&rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{&rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{RpToAttach: &rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{RpToAttach: &rp.Id}},
},
},
{
Expand All @@ -376,8 +376,8 @@ func TestCtLayout(t *testing.T) {
SessionAddressingIpv6: true,
},
Subpolicies: []*ConnectivityTemplatePrimitive{
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{&rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{&rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{RpToAttach: &rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{RpToAttach: &rp.Id}},
},
},
{
Expand Down Expand Up @@ -413,8 +413,8 @@ func TestCtLayout(t *testing.T) {
SessionAddressingIpv4: true,
},
Subpolicies: []*ConnectivityTemplatePrimitive{
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{&rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{&rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{RpToAttach: &rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{RpToAttach: &rp.Id}},
},
},
{
Expand All @@ -423,8 +423,8 @@ func TestCtLayout(t *testing.T) {
SessionAddressingIpv6: true,
},
Subpolicies: []*ConnectivityTemplatePrimitive{
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{&rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{&rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{RpToAttach: &rp.Id}},
{Attributes: &ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy{RpToAttach: &rp.Id}},
},
},
{
Expand Down