-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
93 lines (80 loc) · 2.49 KB
/
type.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
package internal
import (
"fmt"
"go.starlark.net/starlark"
)
// FloatOrInt is an Unpacker that converts a Starlark int or float to Go's float64.
type FloatOrInt float64
// Unpack implements Unpacker.
func (p *FloatOrInt) Unpack(v starlark.Value) error {
switch v := v.(type) {
case starlark.Int:
*p = FloatOrInt(v.Float())
return nil
case starlark.Float:
*p = FloatOrInt(v)
return nil
}
return fmt.Errorf("got %s, want float or int", v.Type())
}
// StringOrBytes is an Unpacker that converts a Starlark string or bytes to Go's string.
// It works because Go Starlark strings and bytes are both represented as Go strings.
type StringOrBytes string
// Unpack implements Unpacker.
func (p *StringOrBytes) Unpack(v starlark.Value) error {
switch v := v.(type) {
case starlark.String:
*p = StringOrBytes(v)
return nil
case starlark.Bytes:
*p = StringOrBytes(v)
return nil
}
return fmt.Errorf("got %s, want string or bytes", v.Type())
}
// GoString returns the Go string representation of the StringOrBytes.
func (p StringOrBytes) GoString() string {
return string(p)
}
// StarlarkString returns the Starlark string representation of the StringOrBytes.
func (p StringOrBytes) StarlarkString() starlark.String {
return starlark.String(p)
}
// NumericValue holds a Starlark numeric value and tracks its type.
// It can represent an integer or a float, and it prefers integers over floats.
type NumericValue struct {
intValue starlark.Int
floatValue starlark.Float
hasFloat bool
}
// NewNumericValue creates and returns a new NumericValue.
func NewNumericValue() *NumericValue {
return &NumericValue{intValue: starlark.MakeInt(0), floatValue: starlark.Float(0)}
}
// Add takes a Starlark Value and adds it to the NumericValue.
// It returns an error if the given value is neither an int nor a float.
func (n *NumericValue) Add(value starlark.Value) error {
switch value := value.(type) {
case starlark.Int:
n.intValue = n.intValue.Add(value)
case starlark.Float:
n.floatValue += value
n.hasFloat = true
case nil:
// do nothing
default:
return fmt.Errorf("unsupported type: %s, expected float or int", value.Type())
}
return nil
}
// AsFloat returns the float representation of the NumericValue.
func (n *NumericValue) AsFloat() float64 {
return float64(n.floatValue + n.intValue.Float())
}
// Value returns the Starlark Value representation of the NumericValue.
func (n *NumericValue) Value() starlark.Value {
if n.hasFloat {
return starlark.Float(n.AsFloat())
}
return n.intValue
}