forked from knative/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basictypekindnode.go
95 lines (79 loc) · 2.56 KB
/
basictypekindnode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package resourcetree
import (
"fmt"
"reflect"
"strconv"
"k8s.io/apimachinery/pkg/util/sets"
)
// BasicTypeKindNode represents resource tree node of basic types like int, float, etc.
type BasicTypeKindNode struct {
NodeData
values sets.String // Values seen for this node. Useful for enum types.
possibleEnum bool // Flag to indicate if this is a possible enum.
}
// GetData returns node data
func (b *BasicTypeKindNode) GetData() NodeData {
return b.NodeData
}
func (b *BasicTypeKindNode) initialize(field string, parent NodeInterface, t reflect.Type, rt *ResourceTree) {
b.NodeData.initialize(field, parent, t, rt)
b.values = sets.String{}
b.NodeData.LeafNode = true
}
func (b *BasicTypeKindNode) buildChildNodes(t reflect.Type) {
// Treating bools as possible enums to support tighter coverage information.
if t.Name() != t.Kind().String() || b.FieldType.Kind() == reflect.Bool {
b.possibleEnum = true
}
}
func (b *BasicTypeKindNode) updateCoverage(v reflect.Value) {
if value := b.string(v); len(value) > 0 {
if b.possibleEnum || b.FieldType.Kind() == reflect.Bool {
b.values.Insert(value)
}
b.Covered = true
}
}
// no-op as the coverage is calculated as field coverage in parent node.
func (b *BasicTypeKindNode) buildCoverageData(coverageHelper coverageDataHelper) {}
func (b *BasicTypeKindNode) string(v reflect.Value) string {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.Int() != 0 {
return strconv.Itoa(int(v.Int()))
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if v.Uint() != 0 {
return strconv.FormatUint(v.Uint(), 10)
}
case reflect.Float32, reflect.Float64:
if v.Float() != 0 {
return fmt.Sprintf("%f", v.Float())
}
case reflect.String:
if v.Len() != 0 {
return v.String()
}
case reflect.Bool:
return strconv.FormatBool(v.Bool())
}
return ""
}
func (b *BasicTypeKindNode) getValues() sets.String {
if b.possibleEnum {
return b.values
}
return nil
}