Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 1ccbefd

Browse files
committed
consistent method ordering and comments in expressions
1 parent 32501d9 commit 1ccbefd

13 files changed

+162
-162
lines changed

expr/tagquery/expression.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ func (e Expressions) Strings() []string {
9090
}
9191

9292
type Expression interface {
93-
// GetMetricDefinitionFilter returns a MetricDefinitionFilter. It takes a metric definition, looks
94-
// at its tags and returns a decision regarding this query expression applied to its tags.
95-
GetMetricDefinitionFilter() MetricDefinitionFilter
96-
9793
// GetDefaultDecision defines what decision should be made if the filter has not come to a conclusive
9894
// decision based on a single index. When looking at more than one tag index in order of decreasing
9995
// priority to decide whether a metric should be part of the final result set, some operators and metric
@@ -147,18 +143,29 @@ type Expression interface {
147143
// GetOperator returns the operator of this expression
148144
GetOperator() ExpressionOperator
149145

150-
// FilterValues takes a map that's indexed by strings and applies this expression's criteria to
151-
// each of the strings, then it returns the strings that have matched
152-
// In case of expressions that get applied to tags, the first level map of the metric tag index
153-
// or meta tag index can get passed into this function, otherwise the second level under the key
154-
// returned by GetKey()
155-
ValuePasses(string) bool
156-
157146
// HasRe indicates whether the evaluation of this expression involves regular expressions
158147
HasRe() bool
159148

160-
RequiresNonEmptyValue() bool
149+
// OperatesOnTag returns true if this expression operators on the tag keys,
150+
// or false if it operates on the values
161151
OperatesOnTag() bool
152+
153+
// RequiresNonEmptyValue returns boolean indicating whether this expression requires a non-empty
154+
// value. Every query must have at least one expression requiring a non-empty value, otherwise
155+
// the query is considered invalid
156+
RequiresNonEmptyValue() bool
157+
158+
// ValuePasses takes a string which should either be a tag key or value depending on the return
159+
// value of OperatesOnTag(), then it returns a bool to indicate whether the given value satisfies
160+
// this expression
161+
ValuePasses(string) bool
162+
163+
// GetMetricDefinitionFilter returns a MetricDefinitionFilter
164+
// The MetricDefinitionFilter takes a metric definition, looks at its tags and returns a decision
165+
// regarding this query expression applied to its tags
166+
GetMetricDefinitionFilter() MetricDefinitionFilter
167+
168+
// StringIntoBuilder takes a builder and writes a string representation of this expression into it
162169
StringIntoBuilder(builder *strings.Builder)
163170
}
164171

expr/tagquery/expression_common.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ func (e *expressionCommon) GetValue() string {
1515
return e.value
1616
}
1717

18-
func (e *expressionCommon) RequiresNonEmptyValue() bool {
19-
// by default assume true, unless a concrete type overrides this method
20-
return true
18+
func (e *expressionCommon) HasRe() bool {
19+
// by default assume false, unless a concrete type overrides this method
20+
return false
2121
}
2222

2323
func (e *expressionCommon) OperatesOnTag() bool {
2424
// by default assume false, unless a concrete type overrides this method
2525
return false
2626
}
2727

28-
func (e *expressionCommon) HasRe() bool {
29-
// by default assume false, unless a concrete type overrides this method
30-
return false
28+
func (e *expressionCommon) RequiresNonEmptyValue() bool {
29+
// by default assume true, unless a concrete type overrides this method
30+
return true
3131
}
3232

3333
// expressionCommonRe is an extended version of expressionCommon with additional

expr/tagquery/expression_equal.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ type expressionEqual struct {
88
expressionCommon
99
}
1010

11+
func (e *expressionEqual) GetDefaultDecision() FilterDecision {
12+
return Fail
13+
}
14+
1115
func (e *expressionEqual) GetOperator() ExpressionOperator {
1216
return EQUAL
1317
}
@@ -16,16 +20,6 @@ func (e *expressionEqual) ValuePasses(value string) bool {
1620
return value == e.value
1721
}
1822

19-
func (e *expressionEqual) GetDefaultDecision() FilterDecision {
20-
return Fail
21-
}
22-
23-
func (e *expressionEqual) StringIntoBuilder(builder *strings.Builder) {
24-
builder.WriteString(e.key)
25-
builder.WriteString("=")
26-
builder.WriteString(e.value)
27-
}
28-
2923
func (e *expressionEqual) GetMetricDefinitionFilter() MetricDefinitionFilter {
3024
if e.key == "name" {
3125
if e.value == "" {
@@ -58,3 +52,9 @@ func (e *expressionEqual) GetMetricDefinitionFilter() MetricDefinitionFilter {
5852
return None
5953
}
6054
}
55+
56+
func (e *expressionEqual) StringIntoBuilder(builder *strings.Builder) {
57+
builder.WriteString(e.key)
58+
builder.WriteString("=")
59+
builder.WriteString(e.value)
60+
}

expr/tagquery/expression_has_tag.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,20 @@ type expressionHasTag struct {
88
expressionCommon
99
}
1010

11-
func (e *expressionHasTag) GetOperator() ExpressionOperator {
12-
return HAS_TAG
13-
}
14-
15-
func (e *expressionHasTag) ValuePasses(value string) bool {
16-
return value == e.key
17-
}
18-
1911
func (e *expressionHasTag) GetDefaultDecision() FilterDecision {
2012
return Fail
2113
}
2214

15+
func (e *expressionHasTag) GetOperator() ExpressionOperator {
16+
return HAS_TAG
17+
}
18+
2319
func (e *expressionHasTag) OperatesOnTag() bool {
2420
return true
2521
}
2622

27-
func (e *expressionHasTag) StringIntoBuilder(builder *strings.Builder) {
28-
builder.WriteString(e.key)
29-
builder.WriteString("!=")
23+
func (e *expressionHasTag) ValuePasses(value string) bool {
24+
return value == e.key
3025
}
3126

3227
func (e *expressionHasTag) GetMetricDefinitionFilter() MetricDefinitionFilter {
@@ -45,3 +40,8 @@ func (e *expressionHasTag) GetMetricDefinitionFilter() MetricDefinitionFilter {
4540
return None
4641
}
4742
}
43+
44+
func (e *expressionHasTag) StringIntoBuilder(builder *strings.Builder) {
45+
builder.WriteString(e.key)
46+
builder.WriteString("!=")
47+
}

expr/tagquery/expression_match.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ type expressionMatch struct {
1010
expressionCommonRe
1111
}
1212

13-
func (e *expressionMatch) GetOperator() ExpressionOperator {
14-
return MATCH
15-
}
16-
17-
func (e *expressionMatch) HasRe() bool {
18-
return true
19-
}
20-
21-
func (e *expressionMatch) ValuePasses(value string) bool {
22-
return e.valueRe.MatchString(value)
23-
}
24-
2513
func (e *expressionMatch) GetDefaultDecision() FilterDecision {
2614
// if the pattern matches "" (f.e. "tag=~.*) then a metric which
2715
// does not have the tag "tag" at all should also be part of the
@@ -35,16 +23,22 @@ func (e *expressionMatch) GetDefaultDecision() FilterDecision {
3523
return Fail
3624
}
3725

38-
func (e *expressionMatch) StringIntoBuilder(builder *strings.Builder) {
39-
builder.WriteString(e.key)
40-
builder.WriteString("=~")
41-
builder.WriteString(e.value)
26+
func (e *expressionMatch) GetOperator() ExpressionOperator {
27+
return MATCH
28+
}
29+
30+
func (e *expressionMatch) HasRe() bool {
31+
return true
4232
}
4333

4434
func (e *expressionMatch) RequiresNonEmptyValue() bool {
4535
return !e.matchesEmpty
4636
}
4737

38+
func (e *expressionMatch) ValuePasses(value string) bool {
39+
return e.valueRe.MatchString(value)
40+
}
41+
4842
func (e *expressionMatch) GetMetricDefinitionFilter() MetricDefinitionFilter {
4943
if e.key == "name" {
5044
if e.value == "" {
@@ -105,3 +99,9 @@ func (e *expressionMatch) GetMetricDefinitionFilter() MetricDefinitionFilter {
10599
return None
106100
}
107101
}
102+
103+
func (e *expressionMatch) StringIntoBuilder(builder *strings.Builder) {
104+
builder.WriteString(e.key)
105+
builder.WriteString("=~")
106+
builder.WriteString(e.value)
107+
}

expr/tagquery/expression_match_all.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ type expressionMatchAll struct {
1010
originalOperator ExpressionOperator
1111
}
1212

13+
func (e *expressionMatchAll) GetDefaultDecision() FilterDecision {
14+
return Pass
15+
}
16+
1317
func (e *expressionMatchAll) GetKey() string {
1418
return e.key
1519
}
@@ -18,36 +22,28 @@ func (e *expressionMatchAll) GetValue() string {
1822
return e.value
1923
}
2024

21-
func (e *expressionMatchAll) RequiresNonEmptyValue() bool {
22-
return false
23-
}
24-
25-
func (e *expressionMatchAll) OperatesOnTag() bool {
26-
return false
25+
func (e *expressionMatchAll) GetOperator() ExpressionOperator {
26+
return MATCH_ALL
2727
}
2828

2929
func (e *expressionMatchAll) HasRe() bool {
3030
return false
3131
}
3232

33-
func (e *expressionMatchAll) GetOperator() ExpressionOperator {
34-
return MATCH_ALL
33+
func (e *expressionMatchAll) RequiresNonEmptyValue() bool {
34+
return false
3535
}
3636

3737
func (e *expressionMatchAll) ValuePasses(value string) bool {
3838
return true
3939
}
4040

41-
func (e *expressionMatchAll) GetDefaultDecision() FilterDecision {
42-
return Pass
41+
func (e *expressionMatchAll) GetMetricDefinitionFilter() MetricDefinitionFilter {
42+
return func(_ string, _ []string) FilterDecision { return Pass }
4343
}
4444

4545
func (e *expressionMatchAll) StringIntoBuilder(builder *strings.Builder) {
4646
builder.WriteString(e.key)
4747
e.originalOperator.StringIntoBuilder(builder)
4848
builder.WriteString(e.value)
4949
}
50-
51-
func (e *expressionMatchAll) GetMetricDefinitionFilter() MetricDefinitionFilter {
52-
return func(_ string, _ []string) FilterDecision { return Pass }
53-
}

expr/tagquery/expression_match_none.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ type expressionMatchNone struct {
1010
originalOperator ExpressionOperator
1111
}
1212

13+
func (e *expressionMatchNone) GetDefaultDecision() FilterDecision {
14+
return Fail
15+
}
16+
1317
func (e *expressionMatchNone) GetKey() string {
1418
return e.key
1519
}
@@ -18,35 +22,28 @@ func (e *expressionMatchNone) GetValue() string {
1822
return e.value
1923
}
2024

21-
func (e *expressionMatchNone) RequiresNonEmptyValue() bool {
22-
return true
23-
}
24-
25-
func (e *expressionMatchNone) OperatesOnTag() bool {
26-
return false
25+
func (e *expressionMatchNone) GetOperator() ExpressionOperator {
26+
return MATCH_NONE
2727
}
2828

2929
func (e *expressionMatchNone) HasRe() bool {
3030
return false
3131
}
32-
func (e *expressionMatchNone) GetOperator() ExpressionOperator {
33-
return MATCH_NONE
32+
33+
func (e *expressionMatchNone) RequiresNonEmptyValue() bool {
34+
return true
3435
}
3536

3637
func (e *expressionMatchNone) ValuePasses(value string) bool {
3738
return false
3839
}
3940

40-
func (e *expressionMatchNone) GetDefaultDecision() FilterDecision {
41-
return Fail
41+
func (e *expressionMatchNone) GetMetricDefinitionFilter() MetricDefinitionFilter {
42+
return func(_ string, _ []string) FilterDecision { return Fail }
4243
}
4344

4445
func (e *expressionMatchNone) StringIntoBuilder(builder *strings.Builder) {
4546
builder.WriteString(e.key)
4647
e.originalOperator.StringIntoBuilder(builder)
4748
builder.WriteString(e.value)
4849
}
49-
50-
func (e *expressionMatchNone) GetMetricDefinitionFilter() MetricDefinitionFilter {
51-
return func(_ string, _ []string) FilterDecision { return Fail }
52-
}

expr/tagquery/expression_match_tag.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ type expressionMatchTag struct {
1010
expressionCommonRe
1111
}
1212

13+
func (e *expressionMatchTag) GetDefaultDecision() FilterDecision {
14+
return Fail
15+
}
16+
1317
func (e *expressionMatchTag) GetOperator() ExpressionOperator {
1418
return MATCH_TAG
1519
}
@@ -18,14 +22,6 @@ func (e *expressionMatchTag) HasRe() bool {
1822
return true
1923
}
2024

21-
func (e *expressionMatchTag) ValuePasses(tag string) bool {
22-
return e.valueRe.MatchString(tag)
23-
}
24-
25-
func (e *expressionMatchTag) GetDefaultDecision() FilterDecision {
26-
return Fail
27-
}
28-
2925
func (e *expressionMatchTag) OperatesOnTag() bool {
3026
return true
3127
}
@@ -34,9 +30,8 @@ func (e *expressionMatchTag) RequiresNonEmptyValue() bool {
3430
return !e.matchesEmpty
3531
}
3632

37-
func (e *expressionMatchTag) StringIntoBuilder(builder *strings.Builder) {
38-
builder.WriteString("__tag=~")
39-
builder.WriteString(e.value)
33+
func (e *expressionMatchTag) ValuePasses(tag string) bool {
34+
return e.valueRe.MatchString(tag)
4035
}
4136

4237
func (e *expressionMatchTag) GetMetricDefinitionFilter() MetricDefinitionFilter {
@@ -82,3 +77,8 @@ func (e *expressionMatchTag) GetMetricDefinitionFilter() MetricDefinitionFilter
8277
return None
8378
}
8479
}
80+
81+
func (e *expressionMatchTag) StringIntoBuilder(builder *strings.Builder) {
82+
builder.WriteString("__tag=~")
83+
builder.WriteString(e.value)
84+
}

0 commit comments

Comments
 (0)