-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
122 lines (106 loc) · 3.21 KB
/
result.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2017 Istio 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 interpreter
import (
"fmt"
"math"
"time"
"istio.io/istio/mixer/pkg/attribute"
"istio.io/istio/mixer/pkg/il"
"istio.io/istio/pkg/log"
)
// Result contains the result of an evaluation performed by the interpreter.
type Result struct {
vs string
vi interface{}
t il.Type
v1 uint32
v2 uint32
}
// Type returns the underlying type of the value in the Result.
func (r Result) Type() il.Type {
return r.t
}
// AsBool returns the value contained in the result as a bool. If the underlying result is not bool,
// it panics.
func (r Result) AsBool() bool {
if r.t != il.Bool {
panic("interpreter.Result: result is not bool")
}
if r.v1 == 0 {
return false
}
return true
}
// AsString returns the value contained in the result as a string. Unlike other methods, it does not
// panic if the underlying value is not string and returns the string version of the data.
func (r Result) AsString() string {
if r.t != il.String {
log.Infof("result.AsString converting to string from type: '%v'", r.t)
return fmt.Sprintf("%v", r.AsInterface())
}
return r.vs
}
// AsInteger returns the value contained in the result as an integer. If the underlying result is not
// integer, it panics.
func (r Result) AsInteger() int64 {
if r.t != il.Integer {
panic("interpreter.Result: result is not integer")
}
return int64(r.v1) + int64(r.v2)<<32
}
// AsDouble returns the value contained in the result as a double. If the underlying result is not
// double, it panics.
func (r Result) AsDouble() float64 {
if r.t != il.Double {
panic("interpreter.Result: result is not double")
}
var t = uint64(r.v1) + uint64(r.v2)<<32
return math.Float64frombits(t)
}
// AsDuration returns the value contained in the result as time.Duration. If the underlying result is
// not a duration, it panics.
func (r Result) AsDuration() time.Duration {
if r.t != il.Duration {
panic("interpreter.Result: result is not Duration")
}
return time.Duration(int64(r.v1) + int64(r.v2)<<32)
}
// AsInterface returns the value contained in the result as an interface{}.
func (r Result) AsInterface() interface{} {
switch r.t {
case il.Bool:
return r.AsBool()
case il.String:
return r.AsString()
case il.Integer:
return r.AsInteger()
case il.Double:
return r.AsDouble()
case il.Duration:
return r.AsDuration()
case il.Void:
return nil
case il.Interface:
// TODO: currently byte[] can only be net.IP address type, so we can
// safely do this. Ideally we want to have a better fix.
if attribute.CheckType(r.vi) {
return r.vi
}
fallthrough
default:
log.Warnf("interpreter.Result: Unknown type encountered. Returning nil. type: '%v'", r.t)
return nil
}
}