Passed
Push — master ( ad04e3...c5b8de )
by eval
01:47
created

cloudwatchlogs.QueryResults.filterValue   A

Complexity

Conditions 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nop 1
dl 0
loc 15
rs 9.3333
c 0
b 0
f 0
1
package cloudwatchlogs
2
3
import (
4
	"strings"
5
6
	SDK "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
7
)
8
9
// QueryResults contains results of GetQueryREsults.
10
type QueryResults struct {
11
	List [][]ResultField
12
}
13
14
func NewQueryResults(list [][]SDK.ResultField) QueryResults {
0 ignored issues
show
introduced by
exported function NewQueryResults should have comment or be unexported
Loading history...
15
	result := make([][]ResultField, len(list))
16
	for i, v := range list {
17
		result[i] = NewResultFieldList(v)
18
	}
19
	return QueryResults{
20
		List: result,
21
	}
22
}
23
24
// FilterFieldEqual filters the result whose field equals given string.
25
func (r QueryResults) FilterFieldEqual(s string) [][]ResultField {
26
	return r.filterField(func(v string) bool {
27
		return v == s
28
	})
29
}
30
31
// FilterFieldContains filters the result whose field equals given string.
32
func (r QueryResults) FilterFieldContains(s string) [][]ResultField {
33
	return r.filterField(func(v string) bool {
34
		return strings.Contains(v, s)
35
	})
36
}
37
38
// FilterValueEqual filters the result whose value equals given string.
39
func (r QueryResults) FilterValueEqual(s string) [][]ResultField {
40
	return r.filterValue(func(v string) bool {
41
		return v == s
42
	})
43
}
44
45
// FilterValueContains filters the result whose value equals given string.
46
func (r QueryResults) FilterValueContains(s string) [][]ResultField {
47
	return r.filterValue(func(v string) bool {
48
		return strings.Contains(v, s)
49
	})
50
}
51
52
func (r QueryResults) filterField(fn func(string) bool) [][]ResultField {
53
	result := make([][]ResultField, 0, len(r.List))
54
	for _, vv := range r.List {
55
		has := false
56
		for _, v := range vv {
57
			if fn(v.Field) {
58
				has = true
59
				break
60
			}
61
		}
62
		if has {
63
			result = append(result, vv)
64
		}
65
	}
66
	return result
67
}
68
69
func (r QueryResults) filterValue(fn func(string) bool) [][]ResultField {
70
	result := make([][]ResultField, 0, len(r.List))
71
	for _, vv := range r.List {
72
		has := false
73
		for _, v := range vv {
74
			if fn(v.Value) {
75
				has = true
76
				break
77
			}
78
		}
79
		if has {
80
			result = append(result, vv)
81
		}
82
	}
83
	return result
84
}
85
86
type ResultField struct {
0 ignored issues
show
introduced by
exported type ResultField should have comment or be unexported
Loading history...
87
	Field string
88
	Value string
89
}
90
91
func NewResultFieldList(list []SDK.ResultField) []ResultField {
0 ignored issues
show
introduced by
exported function NewResultFieldList should have comment or be unexported
Loading history...
92
	result := make([]ResultField, len(list))
93
	for i, v := range list {
94
		result[i] = NewResultField(v)
95
	}
96
	return result
97
}
98
99
func NewResultField(o SDK.ResultField) ResultField {
0 ignored issues
show
introduced by
exported function NewResultField should have comment or be unexported
Loading history...
100
	r := ResultField{}
101
	if o.Field != nil {
102
		r.Field = *o.Field
103
	}
104
	if o.Value != nil {
105
		r.Value = *o.Value
106
	}
107
	return r
108
}
109