Skip to content

Commit

Permalink
Merge pull request #360 from fasaxc/sel-iface
Browse files Browse the repository at this point in the history
Introduce Labels interface
  • Loading branch information
robbrockbank authored Mar 10, 2017
2 parents 145b133 + 3428e33 commit 70c9896
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
51 changes: 35 additions & 16 deletions lib/selector/parser/ast.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2016 Tigera, Inc. All rights reserved.
// Copyright (c) 2016-2017 Tigera, Inc. All rights reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,8 +21,23 @@ import (
"github.com/projectcalico/libcalico-go/lib/hash"
)

type Labels interface {
Get(labelName string) (value string, present bool)
}

type MapAsLabels map[string]string

func (l MapAsLabels) Get(labelName string) (value string, present bool) {
value, present = l[labelName]
return
}

type Selector interface {
// Evaluate evaluates the selector against the given labels expressed as a concrete map.
Evaluate(labels map[string]string) bool
// EvaluateLabels evaluates the selector against the given labels expressed as an interface.
// This allows for labels that are calculated on the fly.
EvaluateLabels(labels Labels) bool
String() string
UniqueId() string
}
Expand All @@ -34,6 +49,10 @@ type selectorRoot struct {
}

func (sel selectorRoot) Evaluate(labels map[string]string) bool {
return sel.EvaluateLabels(MapAsLabels(labels))
}

func (sel selectorRoot) EvaluateLabels(labels Labels) bool {
return sel.root.Evaluate(labels)
}

Expand All @@ -57,7 +76,7 @@ func (sel selectorRoot) UniqueId() string {
var _ Selector = (*selectorRoot)(nil)

type node interface {
Evaluate(labels map[string]string) bool
Evaluate(labels Labels) bool
collectFragments(fragments []string) []string
}

Expand All @@ -66,8 +85,8 @@ type LabelEqValueNode struct {
Value string
}

func (node LabelEqValueNode) Evaluate(labels map[string]string) bool {
if val, ok := labels[node.LabelName]; ok {
func (node LabelEqValueNode) Evaluate(labels Labels) bool {
if val, ok := labels.Get(node.LabelName); ok {
return val == node.Value
} else {
return false
Expand All @@ -89,8 +108,8 @@ type LabelInSetNode struct {
Value StringSet
}

func (node LabelInSetNode) Evaluate(labels map[string]string) bool {
if val, ok := labels[node.LabelName]; ok {
func (node LabelInSetNode) Evaluate(labels Labels) bool {
if val, ok := labels.Get(node.LabelName); ok {
return node.Value.Contains(val)
} else {
return false
Expand All @@ -106,8 +125,8 @@ type LabelNotInSetNode struct {
Value StringSet
}

func (node LabelNotInSetNode) Evaluate(labels map[string]string) bool {
if val, ok := labels[node.LabelName]; ok {
func (node LabelNotInSetNode) Evaluate(labels Labels) bool {
if val, ok := labels.Get(node.LabelName); ok {
return !node.Value.Contains(val)
} else {
return true
Expand Down Expand Up @@ -146,8 +165,8 @@ type LabelNeValueNode struct {
Value string
}

func (node LabelNeValueNode) Evaluate(labels map[string]string) bool {
if val, ok := labels[node.LabelName]; ok {
func (node LabelNeValueNode) Evaluate(labels Labels) bool {
if val, ok := labels.Get(node.LabelName); ok {
return val != node.Value
} else {
return true
Expand All @@ -168,8 +187,8 @@ type HasNode struct {
LabelName string
}

func (node HasNode) Evaluate(labels map[string]string) bool {
if _, ok := labels[node.LabelName]; ok {
func (node HasNode) Evaluate(labels Labels) bool {
if _, ok := labels.Get(node.LabelName); ok {
return true
} else {
return false
Expand All @@ -184,7 +203,7 @@ type NotNode struct {
Operand node
}

func (node NotNode) Evaluate(labels map[string]string) bool {
func (node NotNode) Evaluate(labels Labels) bool {
return !node.Operand.Evaluate(labels)
}

Expand All @@ -197,7 +216,7 @@ type AndNode struct {
Operands []node
}

func (node AndNode) Evaluate(labels map[string]string) bool {
func (node AndNode) Evaluate(labels Labels) bool {
for _, operand := range node.Operands {
if !operand.Evaluate(labels) {
return false
Expand All @@ -221,7 +240,7 @@ type OrNode struct {
Operands []node
}

func (node OrNode) Evaluate(labels map[string]string) bool {
func (node OrNode) Evaluate(labels Labels) bool {
for _, operand := range node.Operands {
if operand.Evaluate(labels) {
return true
Expand All @@ -244,7 +263,7 @@ func (node OrNode) collectFragments(fragments []string) []string {
type AllNode struct {
}

func (node AllNode) Evaluate(labels map[string]string) bool {
func (node AllNode) Evaluate(labels Labels) bool {
return true
}

Expand Down
3 changes: 2 additions & 1 deletion lib/selector/selector.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2016 Tigera, Inc. All rights reserved.
// Copyright (c) 2016-2017 Tigera, Inc. All rights reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@ import "github.com/projectcalico/libcalico-go/lib/selector/parser"

type Selector interface {
Evaluate(labels map[string]string) bool
EvaluateLabels(labels parser.Labels) bool
String() string
UniqueId() string
}
Expand Down

0 comments on commit 70c9896

Please sign in to comment.